Problem Learning Classes

~ I have been programming with other 'noobish' interpreted languages for a few years now, like Just Basic, "GML (Game Maker Language)", JavaScript, etc. so I am familiar with the basics of programming. In other words, I'm used to variables, arrays, functions, etc. but this is the first real low-level language I've started learning seriously. And my first problem is with C++ Classes.


~ I just can't seem to write one, error-free, completely simple class on my own. I understand what classes are for, and why they work, I just don't know the syntax.


~ I've looked at several tutorials, video and documents, but I still don't understand how to define a class and create instances of the class 'object.' (I'm used to Game Maker by Mark Overmars, so I like to refer to classes as 'objects' and its children as 'instances').


~ Since I don't know exactly where to start, I will just make an attempt here, and hopefully someone can point out my most obvious mistakes and set me in the right direction.

~ Here, I will try to create a very simple CAT object, with 2 CAT instances; namely, Panther and Tiger: (I'm using Visual C++ Express 2008, if its relevant)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std; 

class CAT
     {
     private:
         int age;
     public:
         void set_age();
         int get_age();
      };

void CAT::set_age(int arg1)
     {
     age = arg1;
     }

int CAT::get_age()
     {
     return age;
     }


void main()
     {
     CAT Panther;
     Panther.set_age(35);
     CAT Tiger;
     Tiger.set_age(14);
 
     cout << "The cat, Panther, is " << Panther.get_age() << " years old. \n"
          << "The cat, Tiger, is " << Tiger.get_age() << " years old. \n";
     }
Last edited on
To be completely honest I don't see what problems there would be, excepting two: as I recall you need to prototype the function with the appropriate argument lists. So set_age's prototype on line 9 should have the int argument in there. I don't recall for sure but I am also reasonably sure that you need the return types in there, so on lines 13 and 18 you should change CAT::set_age to void CAT::set_age, and likewise copy the return type over for the second function. Changes in bold:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class CAT
     {
     private:
         int age;
     public:
         void set_age(int arg1);
         int get_age();
      };

void CAT::set_age(int arg1)
     {
     age = arg1;
     }

int CAT::get_age()
     {
     return age;
     }


And what problems exactly are you experiencing with the class syntax anyway?
Last edited on
Wow, fast and clear reply. Thanks!

So I've changed my code to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std; 

class CAT
     {
     private:
         int age;
     public:
         void set_age(int arg1);
         int get_age();
      };

void CAT::set_age(int arg1)
     {
     age = arg1;
     }

int CAT::get_age()
     {
     return age;
     }


void main()
     {
     CAT Panther;
     Panther.set_age(35);
     CAT Tiger;
     Tiger.set_age(14);
 
     cout << "The cat, Panther, is " << Panther.get_age() << " years old. \n"
          << "The cat, Tiger, is " << Tiger.get_age() << " years old. \n";
     }


And it results in:


The cat, Panther, is 35 years old.
The cat, Tiger, is 14 years old.
Press any key to continue...


Awesome. Now once I've practiced a bit with making my own classes until I get the hang of it, would it be a good decision if I were to go straight toward learning windows GUI programming? Would I be off to a decent start, or what else should I learn.

Also, one more unrelated question:
How do you initialise and assign a value to, a string variable? I haven't tried yet, but is it done like so:

1
2
char *userName;
userName = "Kaidz";


and can it be assigned a different value through a function like:

1
2
3
4
void changeName(char *arg1)
     {
     userName = *arg1;
     }


Remember I have no idea if this makes any sense, I just know I will need to deal with strings very soon. I am assuming that the '*' sign means that the variable will be an array of char values, with a length of the string passed to it that has the * prefix?
Last edited on
Strings... strings support a constructor to accept char* I believe. The example you provided however is a char [], or C-style string. I would recommend getting familiar with both c-style strings and the c++ string class, then sticking to the string class, which is more reliable. Here's an example:
1
2
3
4
5
6
7
8
9
include <string>
include <iostream>
using namespace std;

int main()
{
string myname("Kaidz") // Constructor for char*
cout << myname;
}


I am pretty sure that your example with username = "Kaidz" should work. Not quite so sure about the second one as that's pointer assignment and so it will just make it point to the same data.
I wouldn't worry too much about windows gui; I would focus on getting as much syntax as possible. Get accustomed to using classes, maybe some inheritance stuff as well. But hey, that's your call.
Thanks again! I don't think I'll bother myself to learn anything about C, I got my strings to work and I'm happy lol. Here is my new practice:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <string>
#include <iostream>
using namespace std;

class STUDENT
	{
	private:
		string strName, strEmail;
		unsigned int uiAge;
		unsigned long ulPhone;
	public:
		void set_name(string);
		void set_email(string);
		void set_age(unsigned int);
		void set_phone(unsigned long);

		string get_name();
		string get_email();
		unsigned int get_age();
		unsigned long get_phone();
	};

void STUDENT::set_name(string newName)
	{
	strName = newName;
	}
void STUDENT::set_email(string newEmail)
	{
	strEmail = newEmail;
	}
void STUDENT::set_age(unsigned int newAge)
	{
	uiAge = newAge;
	}
void STUDENT::set_phone(unsigned long newPhone)
	{
	ulPhone = newPhone;
	}
string STUDENT::get_name()
	{
	return strName;
	}
string STUDENT::get_email()
	{
	return strEmail;
	}
unsigned int STUDENT::get_age()
	{
	return uiAge;
	}
unsigned long STUDENT::get_phone()
	{
	return ulPhone;
	}

void main()
	{
	STUDENT Billy;
	Billy.set_name("Billy");
	Billy.set_email("billy@gmail.com");
	Billy.set_age(17);
	Billy.set_phone(3373313381);

	cout << "The student -Billy- has a first name of:\n" 
		<< Billy.get_name() << "\nYou can email him at:\n"
		<< Billy.get_email() << "\nHe is " << Billy.get_age() << 
		" years old.  His phone number is: " << Billy.get_phone() << "\n";
	}
		


It works. The first time I typed it out, the only errors were typos. I think I'm going to move on now to inheritance or something like that, but would you mind suggesting any improvements on my piece of code? Like, any way it can be made simpler or make more sense, or be written more concisely? Thanks.

P.S. Ugh, I got up to do something and tripped over my laptop's power cord, knocking it onto the floor. In the process, the battery pack fell out, the computer shut off, and my wireless mouse USB snapped in half. And I hate having to use the touch pad.

[BLEEP] omg.. not that anyone cares.. lmao....
Last edited on
After the whole classes study including inheritance, polymorfism, overloading... (don't worry, it's not as hard as it may sound), check out Templates (learning it myself right now, pretty cool).
You should also check out some dynamic data structures from STL and learn how to create your own.

Next don't underestimate the C part in C++, meaning learn pointers. You'll come into contact with them somehow sooner or later wether you want it or not.

Perhas the I/O system is something to take a look at. I assume you want to save something your programs creates eventually.
I will look into that. I did plan on looking up file writing and etc., if that is what you mean by I/O.

Btw I updated my previous code by creating my own constructor:

1
2
3
4
5
6
/*STUDENT Billy;
	Billy.set_name("Billy");
	Billy.set_email("billy@gmail.com");
	Billy.set_age(17);
	Billy.set_phone(3373313381);*/
	STUDENT Billy("Billy-Joe", "billyJ@gmail.com", 14, 3374590962);


It seems way more efficient to me :]

I/O here I come! :]]
Ah, yes, don't forget standard containers (vector, list, deque, map, set, even pair) and templates. Pointers can also come in surprisingly handy although arrays are somewhat outdated compared to the slew of standard containers. (I would rather focus on standard containers, although they may seem more complicated, than arrays and pointers.) But i agree; you just never know when the carried-over aspects of C in C++ can become interesting. File IO is also interesting and can come in handy but I'd say overloading is a fairly mid-learning concept in core C++.
Topic archived. No new replies allowed.