hi i have little problem with class composition, problem is that i don't know how to use it right, maybe someone could show a sample.
and one more, virtual method is class composition or not?
c++ class composition
- DoubleBenis
- suck-o-fied!
- Posts: 51
- Joined: 14 Nov 2009, 17:00
- 14
- Contact:
-
- Kage
- Posts: 732
- Joined: 31 Mar 2007, 16:00
- 17
- Location: Hidden in a Buffer Protection.
- Contact:
Creating a class in C++ is the sole entity that makes it differ from its big-brother C. Using Classes in C++ is a major defacto of re-usability in C++ as well as allowing C++ to feature such things as encapsulation.
A class derived from the idea of a data structure, it could also be described as an object. Unlike a data structure (also known as structs) a C++ class can hold tons of things such as variables and functions, and also have relationships with other classes also known as "friends" and so forth. Basically a class allows you to be more organized with your code and form data structures.
The syntax of a class includes:
Most class declaration is inlined, which means you define the class data objects later on. Classes are divided into sections, their are a variety of sections.. in this case public, private and protected.
Private sections are only allowed to be manipulated by an object of that class.
Public data objects are allowed to be modified by anything, including objects outside the class.
And Protected is the same as private except friends of the class are also allowed to manipulate them.
Inside a program a class function would like this:
As you can see we make an objectofDataStructs which initialize all the values. If you made a function with the same name as the class, than it is called the constructor, if you dont make a constructor the compiler automatically assigns you with one. As you can see we can access objects of our DataStruct by using the period symbol. However since x,y,z are public anything could modify those values, but only class members could modify the setValue function.
Check more information on:
http://www.cplusplus.com/doc/tutorial/classes/
A class derived from the idea of a data structure, it could also be described as an object. Unlike a data structure (also known as structs) a C++ class can hold tons of things such as variables and functions, and also have relationships with other classes also known as "friends" and so forth. Basically a class allows you to be more organized with your code and form data structures.
The syntax of a class includes:
Code: Select all
class DataStruct
{
public:
int x,y,z;
private:
int setValues(int,int,int);
void printValues(int);
protected:
int GoldenNuggets;
};
Private sections are only allowed to be manipulated by an object of that class.
Public data objects are allowed to be modified by anything, including objects outside the class.
And Protected is the same as private except friends of the class are also allowed to manipulate them.
Inside a program a class function would like this:
Code: Select all
DataStruct objectofDataStruct;
objectofDataStruct.setValues(x=0,y=1,z=3);
Check more information on:
http://www.cplusplus.com/doc/tutorial/classes/
[img]http://i81.photobucket.com/albums/j205/ebrizzlez/4lsint1.jpg[/img]
ebrizzles didn't say a thing about composition.
Composition is using references to objects as instance variables. It's used to describe a has-a relationship. For example: a human has a head.
Composition is simply the act of building by combining. You build one class, then you can use that inside another class. That way you build your program.
Composition is using references to objects as instance variables. It's used to describe a has-a relationship. For example: a human has a head.
Code: Select all
class Head
{
public:
int num_eyes;
};
class Human
{
public:
Head *head;
};
Composition is simply the act of building by combining. You build one class, then you can use that inside another class. That way you build your program.
- DoubleBenis
- suck-o-fied!
- Posts: 51
- Joined: 14 Nov 2009, 17:00
- 14
- Contact:
sorry i was busy for some time, i can't reply, i was needed to write a program to university, use more then 3 class, use class composition, and some other sh...t. i written with virtual method, and get only 90%, because i didn't use class composition... for next time i will know how to use it... thanks for yours answers