hey i just got back into c++ after some slacking off and sheer lazyness and have once again run into the same problem i had before and it was never clarified in my high school class(crap teacher who's idea of teaching was "hey harry i did this correctly right?" harry taught the class basically and was lazy about it so we got nowhere fast anyways) but yea and well no matter what i read i can never seem to understand classes and oop to well so any assistance on starting it off on the right foot would be appreciated i even read the tutorial here and like 3 or 4 different book explnanations but its like there is something i keep missing and im not sure what i kind of have the syntax down but not the logic so can anyone help? thanx

11-27-2003

Prelude

The concepts are actually pretty simple:

Classes - Classes are used to group together data and behavior. For example, the builtin int type knows how the data is stored for its variables, but also knows what operations can be performed on that data. Classes give us programmers the ability to write our own data types like that more easily. A class is like the int type and an object is like a variable of int.

Encapsulation - The concept of grouping together data and behavior that classes help to implement.

Inheritance - The ability of one class to extend another by being given data and behavior defined in another class. The new (child) class can also define its own data and behavior as well. For example, I inherited blond hair from my mother (data), but my foul temper is my own (behavior).

Polymorphism - Also called dynamic-binding, polymorphism is the ability of an object of a child class to act as if it were an object of its parent class. For example, if I had a class hierarchy:

Despite being a pointer to base, rd still prints the correct string. Dynamic-binding chose the proper class type. Without dynamic-binding (through the use of virtual functions), this wouldn't have worked:

That's basically all there is to it. Actually using object-orientation is more complex, but the underlying principles are relatively simple.

11-27-2003

ssjnamek

so in so many words let me see if i figured this out cant promise i did but im going to try:
classes allow programmers to create their own types such as int?
but my question is all the variable types of c++ seem to be able to be easily manipulated to allow for most problems to be solved you have numbers of all sorts and strings of all sorts which everything is a string and number right? so i think that is where i am confused what purpose does creating your own data type serve to me it makes sense just use what is already there or am i wrong and in day to day on the job programming people use classes constantly?

pointers you confused me there that was my other area i got confused in pointers that and some arrays but im trying to stick to the classes for the moment as i kinda understand those to some degree so unless it serves purpose to this ill deal with it later so as not to confuse myself.

now i have read classes allow to you make your own header files i read this ina book and it explained how so does that serve any real purpose cause i dont quite understand its purpose?

yes i kno im rather confused right now but hopefully that will chance soon as i really enjoy programming alright thanx

11-27-2003

Prelude

>it makes sense just use what is already
Yes, that's a good habit to get into. However, consider a balanced binary tree data structure. This is not a trivial data structure and many programmers just aren't up to the challenge of writing one of their own. While you can use the tools given to you by C++ to create one, it makes more sense to take all of that complexity and hide it in a data type. This makes using it much easier. Back to the int analogy, would you really want to hand code the underlying implementation of an int each time you want to use it? Of course not, that would make your job much harder and your code would be bound to the machine you wrote it on.

What classes do (ignoring inheritance for the moment) is hide complex logic so that:

1) The client of the class (you or I) doesn't have to understand or worry about the implementation. We just use the interface, which should be simple.

2) The implementation can be changed without affecting the interface such that if a class bound to a certain machine is ported to another, any client code will work properly without modification.

>in day to day on the job programming people use classes constantly?
Yes, we do. Many useful classes are written to make us more productive. A good example is a socket class. Writing networking code using sockets is tedious, so if all of the tediousness is hidden in a class so that I can simply say:

Code:

Socket another_computer ( address );

As opposed to about 80 or so lines of creating, opening, connecting and error checking manual socket code, this makes my job considerably easier and the code more transparent. Even if you don't know how to write low level socket code, you can still figure out what it going on when it is hidden in a class.

>now i have read classes allow to you make your own header files
You can make your own headers without classes, perhaps you misunderstood what was being said in the book.

11-27-2003

ssjnamek

i gather from this one handy feature of this is classes allow portability of programs from system to system?

so am i far off or closer to understand if i say a class can kinda be a function in the sense it creates code that can be referred to constantly without the ever so annoying and painful rewriting 1000x over? cept this time a class is a little more powerful? so my question now is when a class creates a data type i notice its still bound to in the creation the standard c++ data types jus they seem modified now i think i understand its purpose now but in the creation of the data types how do those new data types differ from current c++ ones cause to me they just seem c++ all over again just certain strings attached and they they perform a job and dont need to be rewritten over over? if that was confusing sorry heres an example:

ina game you might be making have a class for various moves you can do and how they might can change a very specific points in the game but they require the basic punch line of what was made in the class? like punch,kick, etc...? am i getting closer? sorry when i think this out i ramble alot im trying to simplify my thoughts alright but let me know if i got closer to this alright thanx

11-27-2003

Prelude

>so am i far off or closer to understand if i say a class can kinda be a function
Somewhat. Classes are similar to functions in that they offer a service to client code. They are more powerful than functions because they can maintain a state as well as perform a service. The only way to do this with functions is by using static variables, which is usually a bad idea on multi-threaded systems. Your analogy is sound, but the concept of a class is closer to a structure that allows member functions as well as member variables: A record containing data and the operations that can be performed on that data.

>how do those new data types differ from current c++ ones
They do more with less code. Take the C++ string class; to create a dynamic C-style string and initialize it, you would be forced to do something like this:

That is the manual way of doing it. But the C++ string class does more, with less code:

Code:

#include <iostream>
#include <string>

using namespace std;

int main()
{
string s ( "This is a test" );

cout<< s <<endl;
}

Not only do you now have a dynamic string, you also have a whole slew of operations to perform on that string contained inside of s with only a single line of code. The same cannot be said of the manual method.

11-27-2003

ssjnamek

i see the great deal of difference in coding and the class i presume then just allows you to pull that "s" back up anytime you please and that s im guessing came from a previously created class so to summarize before i go off and attempt this on my own and start playing with it:

classes allow for creating portable code
classes allow for simplifying things because they create a permanent function to use rather than rewriting things 1000x times over
classes allow for creating own data types

but im guessing one of the MAIN reasons is the portability feature so a string of a certain value will be a string of a certain value no matter what system it may be played on which this is done by creating our own data types which im still gonna have to practice with pretty badly but unless i get my mom to let me install my compiler on here and live to tell the tail after the arguement it could be a while so hopefully ill remeber but thanx for your time and i guess this the basics behind it?