2. Objects
In C++, the class variables are called objects.
An object is an instance of the class.
Class is the passive entity while object is an
active entity.
3. Creating an object of a Class
Declaring a variable of a class type creates an object. You can have
many variables of the same type (class).
The memory space for the objects is allocated when they are
declared.
When an object is created, space for that object is allocated in
primary memory.
Space for member variables is allocated separately for each object.
5. Objects…
The objects can also be created when a class is defined by
placing their names immediately after the closing braces
like
Class Things
{
private:
… … …;
public:
… … …;
}t1,t2,t3;
6. Accessing Class Members
Operators to access class members
Identical to those for structs
Dot member selection operator (.)
Object
Reference to object
With objects we can access the public members of a
class using a dot operator.
7. Example:
#include <iostream.h>
class Box
{
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height;// Height of a box
};
Void main( )
{
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
double volume = 0.0; // Store the volume of a box here
// box 1 specification
Box1.height = 5.0;
Box1.length = 6.0;
Box1.breadth = 7.0;
// box 2 specification
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 13.0;
// volume of box 1
volume = Box1.height * Box1.length * Box1.breadth;
cout << "Volume of Box1 : " << volume <<endl;
// volume of box 2
volume = Box2.height * Box2.length * Box2.breadth;
cout << "Volume of Box2 : " << volume <<endl;
}
When the above code is compiled and executed, it produces the following result:
Volume of Box1 : 210
Volume of Box2 : 1560
8. Array of Objects
Arrays of variables of type class are called arrays of objects.
Example:
class Employee
{
char name[20];
float age;
public:
void getdata(void);
void putdata(void);
};
9. For example:
Employee manager[3]; //array of manager
Employee worker[15]; // array of worker
Employee peon[2]; // array of peon
The array manager contains three objects.
The array worker contains 15 objects.
The array peon contains two objects.
They all contains the objects of the Employee class.
10. Objects as function argument
Object can be used as a function argument. This can be done in two
method ways:
• A copy of the entire object is passed to the function.
• Only the address of the object is transferred to the function.
The first method is called pass-by-value.
The second method is called pass-by-reference.
Pass-by-reference method is more effective.
A function can also return an object.
11. //value.cpp
#include <iostream.h>
class test
{
int a;
public:
void set(test x)
{
x.a=5;
cout<<"a="<<x.a;
}
void show(test y)
{
cout<<"a="<<y.a;
}
};
void main()
{
test t;
t.set(t);
t.show(t);
}
//reference.cpp
#include <iostream.h>
class test
{
int a;
public:
void set(test *x)
{
x->a=5;
cout<<"a="<<x->a;
}
void show(test *y)
{
cout<<"a="<<y->a;
}
};
void main()
{
test t;
t.set(&t);
t.show(&t);