Ahhh yes i remember posting about the same problem.
Trust me in C++ classes are a must.
If you have learned about structs then you already knows what a class somewhat does.
But just incase you havnt ill take you over everything in classes (mostlyISH) because i got a lot of free time :-D.
Making a class is like filling out an index card. (structs are the same way up until the OOP part of it).
class Name
{
public:
char *firstname;
char *lastname;
char *eyecolor;
char *haircolor;
};
^^^ That is how you "prototype" (not sure if thats correct syntax so dont hold me to it)
a class.
So now you have a class that can hold information about someone.
To access that information you must istantiate an object from that class.
Example:
//type your prototyped class here just below
//the includes.
void main()
{
Name bill;
return;
}
bill is your object that is istantiated from the Name class.
Now bill holds each attribute within itself that Name does, its almost like making your own datatype.
If you want to imput data to bill you could do as follows:
void main()
{
Name bill;
bill.firstname = "William";
bill.lastname = "Wallis";
bill.eyecolor = "Hazel";
bill.haircolor = "Black";
return;
}
The (.) you use to access the attributes of bill is called the DOT operator.
Now if you wanted to output the values of bill to the screen you could do as follows:
void main()
{
Name bill;
bill.firstname = "William";
bill.lastname = "Wallis";
bill.eyecolor = "Hazel";
bill.haircolor = "Black";
cout << "First name is : " << bill.firstname <<endl;
cout << "Last name is : " << bill.lastname <<endl;
cout << "Eye color is : " << bill.eyecolor <<endl;
cout << "Hair color is : " << bill.haircolor <<endl;
return;
}
So once you have an object of your class you must you the DOT operator to access its members in anyway ( or the pointer operator (->)).
Anothing classes can do is hold functions within itself.
Having a class use its own function can be useful for Object Oriented Programming.
The basic idea of Object Oriented Programming is to make your code be very easy and fluent to understand. You may think this is a waste, but if you wanna get into MFC (I am right now and dont Listen to Raja lol) then your going to need to know all you can about OOP.
So lets say you wanted to print and input some values into some information in your class.
You could write a function that prints your information and that inputs the info into the datatypes.
EX:
class Name
{
public:
char *firstname;
char *lastname;
char *eyecolor;
char *haircolor;
void GetInfo();
void PrintInfo();
};
Name::GetInfo()
{
cout << "What is your first name? ";
cin >> firstname;
cout << "What is your last name? ";
cin >> lastname;
cout << "What color are your eyes? ";
cin >> eyecolor;
cout << "What color is your hair? ";
cin >> haircolor;
};
Name::PrintInfo()
{
cout <<"Your first name is " << firstname <<endl;
cout << "Your last name is " << lastname <<endl;
cout <<"Your eyes are " << eyecolor <<endl;
cout <<"Your hais is " << haircolor << endl;
};
void main()
{
Name bill;
bill.GetInfo();
bill.PrintInfo();
return;
}
You would want to do this so you can follow your code very easily.
Which brings me to the point of encapsulation.
Encapsulation is important because it keeps the code from being tampered with by unknown and outside objects.
Inheritance - In classes you can have a BASE class.
Sometimes youll have some code that youll want to reuse for a class, but then youll want to add on to it.
For example:
class Automobile
{
};
Every car has features on it that are manditory right?
Well then you could prototype automobile like so :
class Autmobile
{
public: <<< ill discuss this in a bit
char windows;
char *color;
char *engine;
};
Each automobile has windows, color, and an engine right? But what if you wanted perhaps a car with things that were only available to it, but it has all the assets any normal automobile has?
Thats what inheritance is for.
With inheritance you can DERIVE a class from Automobile and still be able to access and add onto your DRIVED class.
For example:
Class Taurus : public Autmobile
{
char CDplayer;
char LeatherSeats;
};
Now your class taurus has all the same attributes as an automobile (windows, color, and an engine) but it also has its own special attributes along with those (CDplayer and leather seating).
Im sorry but I got a lot of hw so ill post more later if you need it, also i here the sams book your reading for C++ isnt good at all, I learned from reading Visual C++ in 12 easy lessons :)
GIMPS