You are on page 1of 14

AMIE(I) STUDY CIRCLE(REGD.

)
A Focused Approach
Before taking printout of this chapter, please ensure that this material has not been covered
in the printed course material provided to you (by us).

Additional Course Material


Programming in C++
OBJECT ORIENTED PROGRAMMING
Object Oriented Programming is an approach that provides a way of modularization of
programs by creating partitioned memory area for both data and functions that can be used as
templates for creating copies of such modules.
The fundamental concept behind object oriented programming is to combine into a single
unit both data and functions that operate on the data. Such a unit is called an Object. An
Object is a self contained unit or modules of data and code. All data and procedures related
to the object are defined within it. Therefore, procedures are tied to the data. A message
identifying a method to execute is passed to the object.
The advantages of OOP fall into two broad categories increased programming productivity
and decreased costs. Some classes and objects may be reused in other applications. Re-used
code would have already been tested, so it should need little testing when used in a new
program.

OOP Terminology and Characteristics


There are following basic concepts underlying OOP.
Objects
Classes
Data Abstraction and Encapsulation
Inheritance
Polymorphism
Objects and Classes. Class describes the data and its behaviour or functionality. Objects are
said to be members of classes. For example, Fruit is a class and apple is an object. The basic
features of Fruit are common to all fruits. Therefore apple which is an object of class
Fruit inherits these properties. The apple can then be assigned individual characteristics like
colour, size, etc.
A class is a key concept of C++. A class, when declared in a C++ program, does not occupy
space in memory. On the other hand, an object is a physical identifiable identity with some
characteristics. An object is a type of data which is self-contained unit containing data, codes
and the member functions which operate on data. The object may be a place, person or any
POST BOX NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 1
TOTAL PAGES:
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
other thing. Each object have a different identity. For example, two students can be treated as
similar objects, since their function is same, i.e., learning, but one may be science student and
the other an art student.
Class is a user defined data type. Classes provide data hiding, guaranteed initialization of
data, implicit type conversion for user defined types, user controlled memory management
and a mechanism for overloaded operators.
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 objects 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);

P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 2
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
}

};

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);

P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 3
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
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.

Relation between Class and Object


We know that a class can have number of objects each comprising data and function of that
class. The data of an object can be accessed only by the functions associated with that object.
However, functions of one object can access the function of other object.
Consider the following statement :
int num1, num2, num3;
num1, num2, num3 are three variables of type int. Similarly, we can define many objects of
the same class.
Inheritance. Inheritance is the capability to define a new class in terms of an existing (base)
class. The new class is known as derived class. The principle here is that each subclass shares
common characteristics with the class from which it has been derived. In addition to the
characteristics shared with other members of the class, each subclass has its own particular
characteristics.
The concept of inheritance provide an important extension to the idea of reusability. Once a
class has been written, created and debugged, it can be distributed to other programmers for
use in their own programs. A programmer can take an existing class and without modifying
it, add additional features and capabilities to it. This is done by deriving a new class from the
existing one. The new class will inherit the capabilities of the old one, but it is free to add
P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 4
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
new features to its own. An already existing class can therefore be reused to define a new
class, reducing programming effort. Data members and member functions of the base class
can be inherited by the derived class. Private member of the base class can not be inherited.
The general form of inheritance is:-

class derived_name : access_specifier base_name

};

The derived_name is the name of the derived class. The base_name is the name of the base
class. The access_specifier can be private, public or protected. If the access_specifier is
public then all public members of the base class become public members of the derived class
and protected members of the base class become the protected members of the derived class.
If the access_specifier is private then all public and protected members of the base class will
become private members of the derived class. If the access_specifier is protected then the
public and protected members of the base class become the protected members of the derived
class. Whether access_specifier is public, private or protected, private members of the base
class will not be accessed by the members of the derived class.
The access_specifier protected provides more flexibility in terms of inheritance. The private
members of the base class cannot be accessed by the members of the derived class. The
protected members of the base class remain private to their class but can be accessed and
inherited by the derived class. The protected members of the base class will remain private to
the other elements of the program.
A derived class can inherit one or more base classes. A constructor of the base is executed
first and then the constructor of derived class is executed. A destructor of derived class is
called before the destructor of base class. The arguments to the base class constructor can be
passed as follows:-
derived_constructor (argument list): base1 (arg_list)

base2(arg_list1)

baseN(arg_list)

The derived_constructor is the name of the derived class. The argument list is list of the data
members of the derived class. The base1 is name of the base class. The arg_list is the list of
the members of the base class. Here is a program which illustrates the features of inheritance.
#include<iostream>

using namespace std;

P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 5
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
class shape

private :

double length;

protected:

double breadth;

public :

double len()

return(length);

shape(double length1,double breadth1)

length=length1;

breadth=breadth1;

//shape() { }

};

class shape1

public:

double height;

shape1(double height1)

P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 6
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
height=height1;

//shape1() { }

};

class cuboid : public shape, private shape1

public:

cuboid(double length1,double breadth1,double


height1):shape(length1,breadth1),shape1(height1)

cout << " A constructor is called " << endl;

double volume()

return(height*breadth*len());

double bre()

return(breadth);

double ht()

return(height);

};

int main()

P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 7
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
cuboid c1(2.4,3.5,6.7);

cout << "The length of the cuboid is : " << c1.len() << endl;

cout << "The breadth of the cuboid is : " << c1.bre() << endl;

cout << "The height of the cuboid is : " << c1.ht() << endl;

cout << "The volume of the cuboid is : " << c1.volume() << endl;

return(0);

The result of the program is:-

The program has two base classes shape and shape1 and one derived class called cuboid
which inherits shape as public and shape1 as private. The public and protected members of
shape become pubic and protected members of derived class cuboid. The private members of
shape remain private to the class shape. The members of shape1 class become the private
members of the derived class cuboid.
The statement

class cuboid : public shape, private shape1

states that class cuboid inherits class shape as public and class shape1 as private. The
statement

cuboid(double length1,double breadth1,double


height1):shape(length1,breadth1),shape1(height1)

cout << " A constructor is called " << endl;

declares the constructor of the class cuboid. When constructor of class cuboid is called first
constructor of shape is executed and then constructor of shape1 is executed and after that the
constructor of cuboid is executed. The statements

double volume()
P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 8
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
{

return(height*breadth*len());

calcula te the volume of the cuboid. The class cuboid cannot access the private data member
length of the shape class. It access the length by calling the function len() which returns the
private data member length. The data member breadth becomes the protected member of the
class cuboid. The height which is public member of shape1 class becomes the private
member of the class cuboid as it inherits the shape 1 class as private. The statements

double bre()

return(breadth);

returns the breadth of the cuboid as data member breadth cannot be accessed outside the
class as it is protected member of cuboid. The statement

double ht()

return(height);

returns the height of the cuboid as data member height cannot be accessed outside the class
as height is the private data member of the class cuboid. The statement

cuboid c1(2.4,3.5,6.7);

creates an object c1 of type cuboid. The constructor is called to initialize the values of the
cuboid. The constructor of shape is executed and then constructor of shape1 is executed and
then finally constructor of cuboid is executed. The statement

cout << "The length of the cuboid is : " << c1.len() << endl;

displays the length of the cuboid as c1.len() calls the len() function of class shape which is
also the public member function of cuboid. The state ment

cout << "The breadth of the cuboid is : " << c1.bre() << endl;

displays the breadth of the cuboid. As the data member breadth cannot be accessed directly
as it is protected member of the class cuboid so the function bre() returns the breadth of the
cuboid. The statement

P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 9
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
cout << "The height of the cuboid is : " << c1.ht() << endl;

displays the height of the cuboid. The data member height cannot be accessed directly as it is
private member of class cuboid so it is accessed through the function ht() which returns
height.

Polymorphism. The polymorphism is an important concept of object oriented programming


(OOP). It is a Greek term, means the ability to take more than one form. It refers to the fact
that a single operation can have different behaviour in different objects. In other words,
different object react differently to the same message. For example, consider the operation of
addition for two numbers, addition should generate the sum. In a programming language that
support OOP, we should be able to express the operation of addition by a single operator,
say, +. We can use the expression x + y to denote the sum of x and y for many different
types of x and y, i.e., for integers, floating point numbers and complex numbers. We can also
define the + operation for two string to mean the concatenation of the string.
Polymorphism plays an important role in allowing objects having different internal structure
to share the same external interface. Therefore, a general class of operations may be accessed
in the same manner even though specific actions associated with each operation may differ.
Here is a program which illustrates the working of compile time function overloading and
constructor overloading.

#include<iostream>

using namespace std;

class employee

public:

int week;

int year;

double calculate(double salary)

return(salary*week);

int calculate(int salary)

return(salary*week*year);

P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 10
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
}

employee(int week1)

week=week1;

employee(int week1,int year1)

week=week1;

year=year1;

};

int main()

int sal;

double sal2;

employee emp1(10);

employee emp2(10,3);

cout << "Enter the no years for first employee" << endl;

cin >> emp1.year;

cout << endl << "Enter the salary per week for first employee" << endl;

cin >> sal;

cout << "The total salary of first employee is : " << emp1.calculate(sal) << endl;

cout << endl << "Enter the salary per week for second employee is : " << endl;

cin >> sal2;

cout << "The total salary of second employee is for one year: " <<
emp2.calculate(sal2) << endl;

return(0);

P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 11
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
}

The result of the program is:-

Objective Questions
Set 2
1. Which of the following is not a valid C++ identifier?
(a) _temp
(b) break
(c) BREAK
(d) _brk
2. Which one of the following is correct?
(a) The formal and actual parameter names in function should have different names
(b) The local and global variables can have same name
(c) The constants of an uninitialised character array cells will contain zeros
(d) The contents of external variables are stored in interval registers of CPU
3. The output of the following program segment will be
int main ()
{
increment ();
}
void increment ()
{static x = 5;
cout<<x= <<x;
x = x + 5;
}
P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 12
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
(a) 5 10
(b) 6 10
(c) 10 5
(d) 0 5
4. A program segment defines
int I = 8, j = 5;
what will be the value of expression (I > 0) && (j<5)?
(a) 8
(b) 5
(c) 0
(d) 1
5. Which of the numerical values is not a valid constant?
(a) 0xAB12
(b) 199
(c) 07891
(d) 0321
6. Which of the following converts a high level program to machine executable code?
(a) translator
(b) assembler
(c) interpreter
(d) compiler
7. A 32 bit address bus can address upto
(a) 4 megabytes
(b) 4 terabytes
(c) 4 gegabytes
(d) 32 gigabytes
8. Which of the following is an accounting software package?
(a) focus
(b) windows 2000
(c) foxpro
(d) tally
9. In relational database, a relation is represented by

P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 13
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
(a) field
(b) record
(c) table
(d) primary key
10. Which of the following is a volatile memory?
(a) hard disk
(b) floppy disk
(c) ROM
(d) RAM
Answers: 1. b 2. d 3. a 4. c 5. a 6. d
7. c 8. d 9. b 10. d
TRUE/FALSE
11. Pentium is a 32 bit machine because it has got a 32 bit address bus.
12. Unix is a multiuser operating system.
13. C is a procedural programming language.
14. A C++ compiler actually translates a C++ source code into the equivalent machine
code of the target CPU.
15. Operating system acts as a resource manager for any computer system.
16. When one needs to connect all the computers in one building, then it is preferable to
use a LAN.
17. TCP/IP is actually two protocols rolled into one.
18. If is a valid C++ identifier.
19. A-23 is a valid C++ constant.
20. Flash memory is a writable non volatile memory.
Answer: 11. F 12. T 13. T 14. T 15. 15 16.
T
17. T 18. T 19. F 20. T

P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 14

You might also like