Classes and Objects Part 1
Classes and Objects Part 1
htm]
The main purpose of C++ programming is to add object orientation to the C programming language and
classes are the central feature of C++ that supports object-oriented programming, also called user-defined
types.
A class specifies the form of an object and it combines data representation and methods for manipulating
that data into one neat package. The data and functions within a class are called members of the class.
A class definition starts with the keyword class followed by the class name; and the class body, enclosed by a
pair of curly braces. A class definition must be followed either by a semicolon or a list of declarations. For
example, we defined the Box data type using the keyword class as follows:
class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
The keyword public determines the access attributes of the members of the class that follows it. A public
member can be accessed from outside the class anywhere within the scope of the class object. You can also
specify the members of a class as private or protected which we will discuss in a sub-section.
class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
int 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 <<'\n';
// volume of box 2
volume = Box2.height * Box2.length * Box2.breadth;
cout << "Volume of Box2 : " << volume <<'\n';
return 0;
}
Output:
Volume of Box1 : 210
Volume of Box2 : 1560
It is important to note that private and protected members can not be accessed directly using direct
member access operator (.). We will learn how private and protected members can be accessed.
Let us take previously defined class to access the members of the class using a member function instead of
directly accessing them:
class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
double getVolume(void);// Returns box volume
};
Member functions can be defined within the class definition or separately using scope resolution operator:
Defining a member function within the class definition declares the function inline, even if you do not use
the inline specifier. So either you can define Volume() function as below:
class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
double getVolume(void) {
return length * breadth * height;
}
};
If you like, you can define the same function outside the class using the scope resolution operator (::) as
follows:
double Box::getVolume(void) {
return length * breadth * height;
}
Here, only important point is that you would have to use class name just before :: operator. A member
function will be called using a dot operator (.) on a object where it will manipulate data related to that
object only as follows:
Box myBox; // Create an object
// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// volume of box 1
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume <<'\n';
// volume of box 2
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume <<'\n';
return 0;
}
Output:
Volume of Box1 : 210
Volume of Box2 : 1560
A class can have multiple public, protected, or private labeled sections. Each section remains in effect until
either another section label or the closing right brace of the class body is seen. The default access for
members and classes is private.
class Base {
public:
// public members go here
protected:
};
class Line {
public:
double length;
void setLength( double len );
double getLength( void );
};
// Member functions definitions
double Line::getLength(void) {
return length ;
}
return 0;
}
Output:
Length of line : 6
Length of line : 10
By default all the members of a class would be private, for example in the following class width is a private
member, which means until you label a member, it will be assumed a private member, we define:
class Box {
double width;
public:
double length;
void setWidth( double wid );
double getWidth( void );
};
Practically data in private section and related functions in public section so that they can be called from
outside of the class as shown in the following program.
class Box {
public:
double length;
void setWidth( double wid );
double getWidth( void );
private:
double width;
};
// Member functions definitions
double Box::getWidth(void) {
return width ;
}
return 0;
}
Output:
Length of box : 10
Width of box : 10
A constructor will have exact same name as the class and it does not have any return type at all, not even
void. Constructors can be very useful for setting initial values for certain member variables.
return 0;
}
Output:
Object is being created
Length of line : 6
Parameterized Constructor
A default constructor does not have any parameter, but if you need, a constructor can have parameters.
This helps you to assign initial value to an object at the time of its creation as shown in the following
example:
class Line {
public:
void setLength( double len );
double getLength( void );
Line(double len); // This is the constructor
private:
double length;
};
A destructor will have exact same name as the class prefixed with a tilde (~) and it can neither return a value
nor can it take any parameters. Destructor can be very useful for releasing resources before coming out of
the program like closing files, releasing memories etc.
private:
double length;
};
return 0;
}
Output:
Object is being created
Length of line : 6
Object is being deleted