Overview Classs
Overview Classs
Subject: CP II
Experiment 10 Interview Class in C++ 4thSemester
A class is used to specify 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.
C++ Class definitions:-
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 define
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.
Define C++ objects :
Example
Box Box1; // declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
Both of the objects Box1 and Box2 whill have their own copy of data members.
#include <iostream>
using namespace std;
class Box
{public:
double length; // Length of a box
double breadth; // Breadth of a box
1
FACULTY OF ELECTONICS TECHNOLOGY
Subject: CP II
Experiment 10 Interview Class in C++ 4thSemester
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 <<endl;
// volume of box 2
volume = Box2.height * Box2.length * Box2.breadth;
cout << "Volume of Box2 : " << volume <<endl;
return 0;
}
// main function
int main()
{RectArea rect;
int a,b;
for(int i=1;i<5;i++){
cout<<"\t Enter width and length of rectangle ";
cin>>a>>b;
rect.set_lengths(a,b);
cout<<" Rectangle Area :"<<rect.area()<<endl;}
system("PAUSE");
return 0;
}
Class member functions A member function of a class is a function that has its definition or its
prototype within the class definition like any other variable.
Class access modifiers A class member can be defined as public, private or protected. By
default members would be assumed as private.
Constructor & destructor A class constructor is a special function in a class that is called when a
new object of the class is created. A destructor is also a special
function which is called when created object is deleted.
3
FACULTY OF ELECTONICS TECHNOLOGY
Subject: CP II
Experiment 10 Interview Class in C++ 4thSemester
C++ copy constructor The copy constructor is a constructor which creates an object by
initializing it with an object of the same class, which has been
created previously.
C++ friend functions A friend function is permitted full access to private and protected
members of a class.
C++ inline functions With an inline function, the compiler tries to expand the code in the
body of the function in place of a call to the function.
The this pointer in C++ Every object has a special pointer this which points to the object
itself.
Pointer to C++ classes A pointer to a class is done exactly the same way a pointer to a
structure is. In fact a class is really just a structure with functions in it.
Static members of a class Both data members and function members of a class can be declared
as static.
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
};
4
FACULTY OF ELECTONICS TECHNOLOGY
Subject: CP II
Experiment 10 Interview Class in C++ 4thSemester
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 defineVolume() 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
myBox.getVolume(); // Call member function for the object
Let us put above concepts to set and get the value of different class members in
a class:
#include <iostream>
using namespace std;
class Box
{
public:
double length; // Length of a box
double breadth; // Breadth of a box
5
FACULTY OF ELECTONICS TECHNOLOGY
Subject: CP II
Experiment 10 Interview Class in C++ 4thSemester
double height; // Height of a box
6
FACULTY OF ELECTONICS TECHNOLOGY
Subject: CP II
Experiment 10 Interview Class in C++ 4thSemester
Class Access Modifiers:-
Data hiding is one of the important features of Object Oriented Programming which
allows preventing the functions of a program to access directly the internal
representation of a class type. The access restriction to the class members is specified
by the labeled public, private, and protected sections within the class body. The
keywords public, private, and protected are called access specifiers.
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:
};
The public Members
A public member is accessible from anywhere outside the class but within a
program. You can set and get the value of public variables without any member
function as shown in the following example:
#include <iostream>
using namespace std;
class Line
{
public:
double length;
void setLength( double len );
double getLength( void );
};
#include <iostream>
class Box
{public:
double length;
8
FACULTY OF ELECTONICS TECHNOLOGY
Subject: CP II
Experiment 10 Interview Class in C++ 4thSemester
void setWidth( double wid );
double getWidth( void );
private:
double width;
};
You will learn derived classes and inheritance in next chapter. For now you can check
following example where I have derived one child class SmallBox from a parent class
Box.
Following example is similar to above example and here width member will be
accessible by any member function of its derived class SmallBox.
#include <iostream>
using namespace std;
9
FACULTY OF ELECTONICS TECHNOLOGY
Subject: CP II
Experiment 10 Interview Class in C++ 4thSemester
class Box
{
protected:
double width;
};
class SmallBox:Box // SmallBox is the derived class.
{
public:
void setSmallWidth( double wid );
double getSmallWidth( void );
};
10