~ 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";
}
|