Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Questioner

concerning classes and objects in C++

This topic is 6599 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is a newbie question because I am a newbie. Now I am currently ready a beginner''s C++ Book called C++ for Dummies, not a very good book by the way. I am now onto classes and understand how to set one up and how to create objects and such. But one thing the book fails to explain is what I use classes for and how do I use them. If anyone could answer my question it would be apprecieated.

Share this post


Link to post
Share on other sites
Advertisement
Well,
Classes are very very useful and is practically what C++ is all about. I can''t really tell you everything that they are for and how they are used because there is just so much really. But classes bascially ecapsulate your data and functions together. You probably read about public, private and protected variables. Those help encapsulate your data and private/protected hide stuff from a client app that you don''t want them messing with. Another big thing about C++ is inheritance. That is when you inherit one class into another class.

class FooOne
{
int Foo1;

int FooIt();
};

class FooTwo: public FooOne
{
float Foo2;

void FooIt( float X );
};


now the class FooTwo inherits the data and member functions from FooOne. There''s different ways of inheriting one class into another, you should find that in your book. Then when inheriting you can use virtual functions which is another big thing about classes. Cool stuff. But, that''s just a little blurb on what classes are used for, I''m sure you''re book mentions those things so keep reading and good luck!

Share this post


Link to post
Share on other sites
I suggest "Teach Yourself C++ in 21 Days 3rd Edition" by Jesse Liberty. He explains everything.

    
//and int is a interger

int myInt;

//now if

class Ball
{
private:
int X;
int Y;
int DX;
int DX;

public:
void MoveBall(int x, int y)
{
X = x;
Y = y;
}

void DrawBall(int x, int y)
{
//draw functions here

}

};

Ball myBall;

myBall.MoveBall(10, 20);
myBall.DrawBall();


Get the idea?

Edited by - vbisme on August 28, 2000 1:00:40 AM

Share this post


Link to post
Share on other sites
Yes, I understand all this but, if I''ve created a class what do I do with it.
That''s what I want to know. I really have no idea how to use a class.

Share this post


Link to post
Share on other sites
basicly a class is a struct on setriods... structures group data... class group both data AND function with a bunch of new nifty ways of doing things... C++FD gets into this at some point...(I''ve got the book too)...

basicly class help you organize stuff... so say with C you would do something like this
    
struct surface{
char*buffer;
int width;
int height;
int depth;
ect.
};

int blt(surface*dest,surface*src){
//blt surface

}

int Flip(void){
//flip the screen

}
.
.
.
void DrawPlay(void){
blt(screen,player,player_x,player_y);
Flip();
}
[/source]

yea that helps alot right? well in C++ we can take this one step further... instead of grouping the data togeather we can group functions too... like this

[source]
class surface{
public://bad idea(designe) to make everything public but... its easier
int Blt(surface*src);
int Flip(void);
.
.
.
char*buffer;
int width;
.
.
.
};

int surface::Blt(surface*src){
//blt surface from source to this classes surface...

}

int surface::Flip(void){
//flip the screen

}
.
.
.
//use the class here in place of the struct

surface*screen=new surface;

screen->Blt(player,player_x,player_y);
screen->Flip();


it helps to organize stuff... this is a fairly simple example and realy if its only 2 functions I probably would use a struct instead of a class... then again maybe not =)... but say we have ooo I don''t know 100 functions =) for doing what ever... in C they could start to become very hard to understand what is going on but in C++ there all the functions are grouped with the data there using...

theres alot more to classes then this but its a starting point...
I almost never use anything but a class... I''ve fallen in love with OOP desinge... but thats me... you may never use a class ever but I love them =)


Great Milenko

Words Of Wisdom:
"Never Stick A Pretzel In Your Butt It Might Break Off In There."


http://www.crosswinds.net/~milenko
http://www.crosswinds.net/~pirotech

Share this post


Link to post
Share on other sites
Classes can be made to be practically anything under the sun that follow ''object-orientated logic''. A class should have its logical properties. For instance, a class named TELEVISION should have a variable called SCREEN and POWERBUTTON etc. With that in mind you can do practically anything with them to perform logical conclusions and answers and results.

You could consider a SONY 68cm tv called ''SONY_68CM'' an ''instance'' or ''object'', because it is a type of television. Say in your class, you had a function called TURN_ON(). You could call SONY_68CM() which would set the property POWERBUTTON to TRUE.
This code highlights my point:

#define CHANNEL_9 9
#define CHANNEL_2 2 //etc.

class TELEVISION {
private:
bool PowerButton;
int Screen;
int Channel;
public:
void Turn_On();
void Turn_Off();
void ChangeChannel(int channel);
};

void TELEVISION::Turn_On()
{
PowerButton = true;
//do stuff to turn screen on and stuff
}
void TELEVISION:Turn_Off()
{
PowerButton = false;
//do shitdown stuff
}

void TELEVISION::ChangeChannel(int CHANNEL)
{
Channel = CHANNEL; /*btw C++ is case insensitive
this means BOB, bob, Bob, bOb, and boB are all different!*/
}

main()
{
TELEVISION SONY_68CM; //Make a Sony 68cm TV
SONY_68CM.Turn_On();
SONY_68CM.ChangeChannel(CHANNEL_9);
SONY-68CM.Turn_Off();
return(0);
}


That is not very elaborate but I hope you see the gist. I suggest further reading, it will be very much worth your while. There are hundreds of free tuts and bookz online. By the way, another great use of the class is containing things and hiding implementations (like say the WinAPI, which is all sheltered from us so we can only call the functions MS want us to(The other functions like BLOWUP_COMP() have private visibility!!))

Good Luck!
Lucas

Share this post


Link to post
Share on other sites
Ok, I understand all that you''ve told me but I am still having trouble!
I''ve created a class and I know what I want to do with it, btu I don''t know how to make it work!
Do i use it as if it were a function or what!
I basiclly want to know after I''ve made a class how do I use it?
Not what do I use it for, or how do I make one, because I know that. I just don''t know HOW to use it, my book doesn''t explain any of this

Share this post


Link to post
Share on other sites
You''ve got the class, right?

Say it''s called CDoodle. Declare like you would any other variable .


int lala;
CDoodle adoodle;


Then, you call the functions in the class or access the data in the class like this:


adoodle.Function();
adoodle.data = 6;


Get it?

Good luck -

Loki

I like food.

Share this post


Link to post
Share on other sites
Basically, a class consists of data members and function members.

Use data members like regular, non-class data, and use function members like regular, non-class functions. The only difference is syntax.

Here's a demonstration:


class CSAMPLE
{
public:
int ClassInt;
int ClassFunc () { return ClassInt; };
};

CSAMPLE Sample;

int RegularInt;
int RegularFunc () { return RegularInt; };

...
...

int Value;

// Get regular integer value (non-class data).
Value = RegularInt;

// Get class integer value (class data member).
Value = Sample.ClassInt;

// Call regular function to retrieve regular integer value (non-class function).
Value = RegularFunc ();

// Call class function to retrieve class integer value (class function member).
Value = Sample.ClassFunc ();


This is basically it. Hope this clarifies it for ya!

CP

"Can't you see it's only life! We can laugh about it!" - Seal

Edited by - Chai Peddler on August 28, 2000 11:11:05 PM

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!