classes

Started by
12 comments, last by Fruny 18 years, 8 months ago
hey, i would probally prefer structors than classes, i really don't need anything to be private or public or whateer, nothing will change, but what exactly does a contructor and a decontrucot rdo, an example would be nice also :)
Advertisement
For one think in C++ a struct is the same as a class with the only difference that all members are public.

A Constructor is way to initialize the values on your class/struct,
a destructor is mainly used, to free any by your class/struct allocated memory.

For example:


struct Test1
{
Test1()
: gc( NULL )
{
gc = new GraphicContext; // now you are sure that gc is initialized always, when you use Test1
}

~Test1()
{
if( gc ) delete gc; // only with this you can easily free all memory allocated by your class
}
GraphicContext* gc;
}


This is an really simple example, with not much purpose so far, but I hope you get the point.
uhh, no, thats the most advanced structor i have ever seen in my life, could you please break that down, i didn't even know you could call a function inside a struct
and what is null?
---
EDIT:
functions declared in a struct are not called, but function declarations.
normally you will split them as follows:

Header:
class Test1
{
// standard constructor
Test1();
}

Code-File:
Test1::Test1()
{
}

The same as with each other function you define in a header in declare in your code-file
---
As mentioned structs are the same as classes (in c++!):

a constructor is a member with the same name as your struct/class
a destructor is a member with also the same name as your struct/class and ~ as prefix...


The main difference is that in a class you have the chance to declare members private (only the class itself can access them, protected the class and all it's child can access them, or public everyone can access them).


each class/struct can have function members as you like:

lets take this a step further:

struct Test1
{
Test1()
: gc( NULL )
{
gc = new GraphicContext; // now you are sure that gc is initialized always, when you use Test1
}

~Test1()
{
if( gc ) delete gc; // only with this you can easily free all memory allocated by your class
}

void refresh()
{
graphiccontext_refresh( gc );
}
GraphicContext* gc;
}

Where graphiccontext_refresh is some routine from an third-party library

using Test1 would look than, as follows:

{
Test1 t1; // here the constructor will be called implicit as our contructor takes no parameters.

t1.refresh(); // call of Test1::refresh;
} // here the destructor for t1 will be called, as you leave the area, where t1 is declared.

EDIT:
NULL is just the same as 0, only that you use it to initialize pointers, which don't point on allocated memory.
Quote:Original post by Sparhawk42
---
EDIT:
functions declared in a struct are not called, but function declarations.
normally you will split them as follows:

Header:
class Test1
{
// standard constructor
Test1();
}

Code-File:
Test1::Test1()
{
}

The same as with each other function you define in a header in declare in your code-file
---
As mentioned structs are the same as classes (in c++!):

a constructor is a member with the same name as your struct/class
a destructor is a member with also the same name as your struct/class and ~ as prefix...


The main difference is that in a class you have the chance to declare members private (only the class itself can access them, protected the class and all it's child can access them, or public everyone can access them).


each class/struct can have function members as you like:

lets take this a step further:

struct Test1
{
Test1()
: gc( NULL )
{
gc = new GraphicContext; // now you are sure that gc is initialized always, when you use Test1
}

~Test1()
{
if( gc ) delete gc; // only with this you can easily free all memory allocated by your class
}

void refresh()
{
graphiccontext_refresh( gc );
}
GraphicContext* gc;
}

Where graphiccontext_refresh is some routine from an third-party library

using Test1 would look than, as follows:

{
Test1 t1; // here the constructor will be called implicit as our contructor takes no parameters.

t1.refresh(); // call of Test1::refresh;
} // here the destructor for t1 will be called, as you leave the area, where t1 is declared.

EDIT:
NULL is just the same as 0, only that you use it to initialize pointers, which don't point on allocated memory.



could you explain the code :( you are "declaring" a function as a member, why?
and what is : gc(null) i don't know any of that stuff, does this have something t do with graphics? i dunno, idon't understand :(
By the way I would suggest, that you start with a good tutorial on object oriented programming with C++, if you really want to see the whole picture.
The point in declaring functions as members is to group code, like you do it with namespaces, the only difference is (and here come the classes into the game) that they can access all private members of your class and you can so garantue that all your private class members are always valid.

GraphicContext is just an class I made up for this example, nothing more.

A constructor works like follows:

Test1::Test1()
: gc( NULL ) // direct writing to the members, as soon as the memory is allocated
{
// doing some more complex stuff

gc = new GraphicContext; // in rare cases this might fail, if you don't have enough memory, for that purpose gc was initialized with NULL.
}


Quote:Original post by Sparhawk42
---
The main difference is that in a class you have the chance to declare members private.


Not true, a struct can declare members private if you want. The only, ONLY, difference between a class and a struct is the default access level for properties or methods. In a struct, the default level is public, and in a class, it is private. This is for backwards compatibility with regular C.

class Chicken{	int someData;};struct Chicken // we are identical as above{private:	int someData;};


In C++, structs can have methods just as classes do. They can also have constructors and destructors, just as classes do. A constructor is called upon the creation of an instance of the class, and the destructor is called when the instance dies.

class Chicken{public:	Chicken()	{		cout << "Hello World";	}	~Chicken()	{		cout << "We're dying.\n";	}};


Thus calling "Chicken k" will immediately call Chicken(), and when k falls out of scope, it will call ~Chicken().

Hope that helps.
[ search: google ][ programming: msdn | boost | opengl ][ languages: nihongo ]
oh, ok, i understand aobut 35% of that, i'll read up some more on classes, but i don't see why anyone would need any of their members private, nothing can change the members

This topic is closed to new replies.

Advertisement