Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Understanding classes

Status
Not open for further replies.

kgomotso

Programmer
Oct 31, 2000
20
ZA
I have just started using C++ and I don't understand the whole logic for having a class and an initialiser for it. What exactly is an initialiser and why can't we move the code on the initialiser to the class definition? I am really sorry if this sounds stupid but OO is new to me.
 
Hi kgomotso,
classes and objectorientated programming is a very hot stuff in C and higher programming language. Frst of all
a class is a description of object, in which you put all
methods and variables of an object.
An example
class car
{
colour
form
fuel
}
you can see a car as a class and in the ram as an object. There are several kinds of car. You can describe the differences bei the colour, form and the fuel you can use.
to initiate a class is that you build in th ram a object a ford or a mercedes

car *ford = new car
car *mercedes = new car.

They are cars but with different attributes.

It is very important to book a course in OO-Programming. Don`t think you can get all informations by reading a book. The books are not build very good and you can use
them when you had had a course.

I hope I could help you a little bit
Greetings Olaf
 
A class can initialize its data in multiple ways. You can have one constructor that initializes all variables to a default amount (like zero). You can have another constructor that takes arguments and uses them to intialize the member data. You can have another constructor that takes a copy of the same object and initializes all member data with the same values as the other object.

If you initialized data in the definition, you wouldn't have the power or flexibility to provide multiples types of construction.
 
A class is a type.
Example:
int is a class of numbers that are integer,
bool is a class of values represented by true or false etc...
These types are known as built-in types or primary types.
The structure type was added and I hope you know about that. It also is known as user type.
A structure is a class of type known as "struct".
A class is also known as a user type.
An object is an instance of a given type e.g. an instance of a given class:
Example :
int a =10; // a is an object of int type and the value stored there is 10.
When the object a is created a constructor of the "int" class is called to build an integer from the value 10 and also there is the assignment operator called to assign it to the a object.
struct employee
{
char name[64];
int salary;
}
The above statements define the structure called "employee"
struct employee emp1; // create an object named "emp1" of type "employee"
struct employee emp2; // create an object named "emp2" of type "employee"
So what is an object ?
As in the real world, an object should be born e.g. created, have a name.
As in the real world the creation of an object is a very important moment and a major improvment introduced by classes is the posibility to control the initialization of an object at the creation moment.
That is way any class must have a constructor or if you do not provide one the compiler will generate one from you but as I said , is good you TO CONTROL HOW THE OBJECT IS CREATED.
A constructor is very important in the classes that allocate memory and in this case that class must have a destructor implemented.
As in the previous answers, there are many ways to initialize an object and you can find these in any C++ book.
Keep in mind what you should have as mimimum in every class you write:
- constructor (mimimum one)
- destructor
- copy constructor
- assignment operator
-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top