Class and Objects
Constructor and Destructor with Methods and properties
A class is a user defined data type like a
structure or a union. A class consists of data variables and functions.
These variables and functions are called members of the class. The
variables are called data members and functions are called member
functions. The member functions are also called methods. The data
members are called properties of the class. An object is the instance
of the class. An object is like a compound variable of the user defined
type. It links both code and data. Within the object, members of the
class can be public or private to the object. The declaration of a
class is syntactically same as structure. The class is declared using
keyword class. The general form of the declaration of the class is:-
class class_name
{
access_specifier:
data functions
access_specifier:
data functions
} object_list;
The object_list is optional. The object_list is
used to declare objects of the class. The class_name is the name of the
class. The access_specifier can either public, private or protected.
The members of the class by default are private to the class. If the
access_specifier is private then members of the class are not
accessible outside the class. If the access_specifier is public then
members of the class can be accessed from outside the class. The
protected access_specifier is needed at the time of inheritance. The
members can be accessed using an object’s name, a dot operator and name
of the member. Here is a program which shows how classes and objects
are created.
#include<iostream>
using namespace std;
class cube
{
public:
double side;
double volume()
{
return(side*side*side);
}
};
int main()
{
double volume1=0;
cube c1,c2;
cout << "Enter the lenght of the cube" << endl;
cin >> c1.side;
cout << "The volume of the cube is : " << c1.volume() << endl;
c2.side=c1.side +2;
cout << "The volume of the second cube is : " << c2.volume() << endl;
return(0);
}
The result of the program is:-
The program consists of a class cube which has data
member side of type double and member function which calculates the
volume of the cube. The statement
class cube
declares a class cube. The statements
public:
double side;
double volume()
{
return(side*side*side);
}
declare that access_specifier is public for data
member side and member function volume. These members can be accessed
from the other parts of the program. The statement
cube c1,c2;
declares two objects c1 and c2 of type cube. The statement
cin >> c1.side;
access the data member of the cube. The member is
accessed by specifying the name of the object as c1 then dot operator
and then name of the variable side. The length entered by the user is
stored in c1.side. In the statement
cout << "The volume of the cube is : " << c1.volume() << endl;
c1.volume() calls the member function volume which
returns the volume of the cube of side whose length is entered by the
user. The statement
c2.side=c1.side +2;
equates the side of object c2 to side of object c1 increased by 2. The objects c2 and c1 are different. The statement
cout << "The volume of the second cube is : " << c2.volume() << endl;
displays the volume of second object c2.
Constructor and Destructor:
Constructors are used in order to initialize the
objects. A constructor is a special kind of a function which is the
member of the class. The name of the constructor is same as name of the
class. A constructor is automatically called when object is created. A
constructor does not have a return type.
A default constructor is a constructor with no
parameters. If no constructor is defined by the user then compiler
supplies the default constructor. Once the constructor is defined by
the user then compiler does not supply default constructor and then
user is responsible for defining default constructor.
A destructor is the complement of the constructor.
It is used to destroy the objects. The objects are destroyed in order
to deallocate the memory occupied by them. The name of the destructor
is same as the name of the constructor as is preceded by a tilt
operator ‘~’. A destructor for objects is executed in the reverse order
of the constructor functions.
Here is a program which shows how constructors and destructors are used.
#include<iostream>
using namespace std;
class cube
{
public:
double side;
double volume()
{
return(side*side*side);
}
cube(double side1)
{
cout << "A constructor is called" << endl;
side=side1;
}
cube()
{
cout << "A default constructor is called " << endl;
}
~cube()
{
cout << "Destructing " << side << endl;
}
};
int main()
{
cube c1(2.34);
cube c2;
cout << "The side of the cube is: " << c1.side << endl;
cout << "The volume of the first cube is : " << c1.volume() << endl;
cout << "Enter the length of the second cube : " ;
cin >> c2.side;
cout << "The volume of second cube is : " << c2.volume() << endl;
return(0);
}
The result of the program is:-
The statement
cube(double side1)
{
cout << "A constructor is called" << endl;
side=side1;
}
declares the constructor of the class cube. The
name of the constructor is same as the name of the class. There is no
return type in the constructor. It will initialize the value of data
member side. The statement
cube()
{
cout << "A default constructor is called " << endl;
}
declares a default constructor. The statement
~cube()
{
cout << "Destructing " << side << endl;
}
declares a destructor to deallocate the objects. The statement
cube c1(2.34);
creates an object c1 of type cube. A constructor is
automatically called and initializes the data member side with value
2.34. The statement
cube c2;
creates an object of type c2. When object c2 is created a default constructor is called and the message will be printed. The statements
cout << "The side of the cube is: " << c1.side << endl;
cout << "The volume of the first cube is : " << c1.volume() << endl;
displays the side and volume of the cube where side has value 2.34. The statement
cin >> c2.side;
will set the value of
the side of the object c2 as entered by the user. At the end of the
program objects are deallocated in the reverse order in which
constructors are called. First object c2 is deallocated whose side is
2.5 and then object c1 is deallocated whose side is 2.34.
Advantage of the classes:-
It provides protection to the data. The members of
the class are by default private to the class while the members of the
structure are public. OOP features allow programmer to easily handle
complex problems and multi file projects. They help in modeling real
world objects such as bank accounts and their related transactions.
Exercise-1: One steps through integer points of the straight line. The length of a step must be non negative and can be by one bigger than, equal to, or by one smaller than the length of the previous step. What is the minimum number of steps in order to get from x to y ? The length of the f irst and the last step must be 1. View Details
Exercise-2: Some operators checks about the relationship between two values and these operators are called relational operators.
Given two numerical values your job is just to nd out the relationship between them that is
(i) First one is greater than the second
(ii) First one is less than the second or
(iii) First and second one is equal.
View Details