0% found this document useful (0 votes)
8 views70 pages

18BCS33C U4

Uploaded by

rajkumar184
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views70 pages

18BCS33C U4

Uploaded by

rajkumar184
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 70

Government Arts College(Autonomous)

Coimbatore – 641018
Re-Accredited with ‘A’ grade by NAAC

Object Oriented Programmimg with C++

Dr. S. Chitra
Associate Professor
Post Graduate & Research Department of Computer Science
Government Arts College(Autonomous)
Coimbatore – 641 018.
Year Subject Title Sem. Sub Code

2018 -19
OBJECT ORIENTED PROGRAMMING WITH C++ III 18BCS33C
Onwards

Objective:
•Learn the fundamentals of input and output using the C++ library
•Design a class that serves as a program module or package.
•Understand and demonstrate the concepts of Functions, Constructor and inheritance.

UNIT – I
Principles of Object Oriented Programming: Software Crisis - Software Evolution - Procedure
Oriented Programming - Object Oriented Programming Paradigm - Basic concepts and benefits of
OOP - Object Oriented Languages - Structure of C++ Program - Tokens, Keywords, Identifiers,
Constants, Basic data type, User-defined Data type, Derived Data type – Symbolic Constants –
Declaration of Variables – Dynamic Initialization - Reference Variable – Operators in C++ - Scope
resolution operator – Memory management Operators – Manipulators – Type Cast operators –
Expressions and their types – Conversions – Operator Precedence - Control Structures
UNIT – II

Functions in C++: Function Prototyping - Call by reference - Return by reference - Inline functions -
Default, const arguments - Function Overloading – Classes and Objects - Member functions - Nesting
of member functions - Private member functions - Memory Allocation for Objects - Static Data
Members - Static Member functions - Array of Objects - Objects as function arguments - Returning
objects - friend functions – Const Member functions .

UNIT – III

Constructors: Parameterized Constructors - Multiple Constructors in a class - Constructors with


default arguments - Dynamic initialization of objects - Copy and Dynamic Constructors - Destructors
- Operator Overloading - Overloading unary and binary operators – Overloading Using Friend
functions – manipulation of Strings using Operators.
UNIT – IV

Inheritance: Defining derived classes - Single Inheritance - Making a private member inheritable – Multilevel,
Multiple inheritance - Hierarchical inheritance - Hybrid inheritance - Virtual base classes - Abstract classes -
Constructors in derived classes - Member classes - Nesting of classes.

UNIT – V

Pointers, Virtual Functions and Polymorphism: Pointer to objects – this pointer- Pointer to derived Class -
Virtual functions – Pure Virtual Functions – C++ Streams –Unformatted I/O- Formated Console I/O – Opening
and Closing File – File modes - File pointers and their manipulations – Sequential I/O – updating a file :Random
access –Error Handling during File operations – Command line Arguments.

TEXT BOOKS

1.E. Balagurusamy, “Object Oriented Programming with C++”, Fourth edition, TMH, 2008.
Unit IV - Inheritance
Reusability is yet another feature of OOP. C++ strongly supports the concept of reusability using Inheritance.
Types of Inheritance
Single Inheritance
When a class inherits from a single base class, it is known as single inheritance. Following program shows the single inheritance using
public derivation.
#include<iostream.h>
#include<conio.h>
class worker
{
int age;
char name [10];
public:
void getworker ( );
void showworker( );
};
void worker : : getworker( )
{
cout <<”yout name please”
cin >> name;
cout <<”your age please” ;
cin >> age;
}
void worker :: showworker( )
{
cout <<”\n My name is :”<<name<<”\n My age is :”<<age;
}
class manager : public worker //derived class (publicly)
{
int now;
public:
void getnow( ) ;
void shownow( ) ;
};
void manager : : getnow( )
{
cout << “number of workers under you”;
cin >> now;
}
void manager :: shownow( )
{
cout <<“\n No. of workers under me are: “ << now;
}
main ( )
{
clrscr ( ) ;
manager M1;
M1.getworker( ); M1.getnow( );
M1.showworker( ); M1.shownow( );
}
RUN OUTPUT:
Your name please
Ravinder
Your age please
27
number of workers under you
30
Then the output will be as follows:
My name is : Ravinder
My age is : 27
No. of workers under me are : 30
The following program shows the single inheritance by private derivation.
#include<iostream.h>
#include<conio.h>
class worker //Base class declaration
{
int age;
char name [10] ;
public:
void getworker( ) ;
void showworker( ) ;
};
void worker : : getworker( )
{
cout << “your name please” ;
cin >> name;
cout << “your age please”;
cin >>age;
}
void worker : showworker( )
{
cout << “\n my name is: “ <<name<< “\n” << “my age is : “ <<age;
}
class manager : private worker //Derived class (privately by default)
{
int now;
public:
void getnow( ) ;
void shownow( ) ;
};
void manager : : getnow( )
{
getworker( ); //calling the get function of base
cout << “number of worker under you”;
cin >>now;
}
void manager : : shownow( )
{
showworker( ) ;
cout << “in no. of worker under me are : “ <<now;
}
main ( )
{
clrscr ( ) ;
manager ml;
ml.getnow( ) ;
ml.shownow( );
}
The following program shows the single inheritance using protected derivation
#include<conio.h>
#include<iostream.h>
class worker //Base class declaration
{ protected:
int age; char name [20];
public:
void getworker( );
void showworker( );
};
void worker :: getworker( )
{
cout >> “your name please”;
cin >> name;
cout << “your age please”;
cin >> age;
}
void worker :: showworker( )
{
cout << “in my name is: “ << name << “in my age is “ <<age;
}
class manager:: protected worker // protected inheritance
{
int now;
public:
void getnow( );
void shownow( ) ;
};
void manager : : getnow( )
{
cout << “please enter the name In”;
cin >> name;
cout<< “please enter the age In”; //Directly inputting the data
cin >> age; members of base class
cout << “ please enter the no. of workers under you:”;
cin >> now;
}
void manager : : shownow( )
{
cout « "your name is : "«name«" and age is : "«age;
cout «“No. of workers under your are : "«now;

main ( )
{
clrscr ( ) ;
manager ml;
ml.getnow( ) ;
cout « "\n \n";
ml.shownow( );
}
Making a Private Member Inheritable
Basically we have visibility modes to specify that in which mode you are deriving the another class
from the already existing base class. They are:

a. Private: when a base class is privately inherited by a derived class, 'public members' of the base
class become private members of the derived class and therefore the public members of the base
class can be accessed by its own objects using the dot operator. The result is that we have no
member of base class that is accessible to the objects of the derived class.

b. Public: On the other hand, when the base class is publicly inherited, 'public members' of the base
class become 'public members' of derived class and therefore they are accessible to the objects of the
derived class.

c. Protected: C++ provides a third visibility modifier, protected, which serve a little purpose in the
inheritance. A member declared as protected is accessible by the member functions within its class
and any class immediately derived from it. It cannot be accessed by functions outside these two
classes.
This declaration will form the different levels of inheritance.
Following program exhibits the multilevel inheritance.
#include<iostream.h>
#include<conio.h>
class worker // Base class declaration - worker
{
int age;
char name [20] ;
public;
void get( ) ;
void show( ) ;
}
void worker: get ( )
{
cout << “your name please” ;
cin >> name;
cout << “your age please” ;
}
void worker : : show ( )
{
cout << “In my name is : “ <<name<< “ In my age is : “ <<age;
}
class manager : public worker //Intermediate base class derived - manager
{ //publicly from the base class
int now;
public:
void get ( ) ;
void show( ) ;
};
void manager :: get ( )
{
worker : :get () ; //calling get ( ) fn. of base class
cout << “no. of workers under you:”;
cin >> now;
}
void manager : : show ( )
{
worker : : show ( ) ; //calling show ( ) fn. of base class
cout << “In no. of workers under me are: “<< now;
}
class ceo: public manager //publicly inherited from the intermediate base class - ceo
{
int nom;
public:
void get ( ) ;
void show ( ) ;
};
void ceo : : get ( )
{
manager : : get ( ) ;
cout << “no. of managers under you are:”; cin >> nom;
}
void manager : : show ( )
{
cout << “No. of managers under me are: “<<nom;
}
main ( )
{
clrscr ( ) ;
ceo cl ;
cl.get ( ) ; cout << “\n\n”;
cl.show ( ) ;
}
#include<iostream.h>
#include<conio . h>
class father //Declaration of base classl
{
int age ;
char name [20] ;
public:
void get ( ) ;
void show ( ) ;
};
void father : : get ( )
{
cout << “your father name please”;
cin >> name;
cout << “Enter the age”;
cin >> age;
}
void father : : show ( )
{
cout<< “My father’s name is: ‘ <<name<< “My father’s age is:<<age;
}
class mother //Declaration of base class 2
{
char name [20] ;
int age ;
public:
void get ( )
{
cout << “mother’s name please” ;
cin >> name;
cout << “mother’s age please” ;
cin >> age;
}
void show ( )
{
cout << “My mother’s name is: “ <<name;
cout << “My mother’s age is: “ <<age;
}
class daughter : public father, public mother //derived class inheriting publicly the features of both the base class
{
char name [20] ;
int std;
public:
void get ( ) ;
void show ( ) ;
};
void daughter :: get ( )
{
father :: get ( ) ;
mother :: get ( ) ;
cout << “child's name: “;
cin >> name;
cout << “child's standard”;
cin >> std;
}
void daughter :: show ( )
{
father :: show ( );
mother :: show ( ) ;
cout << “In child’s name is : “ <<name;
cout << “In child's standard: “ << std;
}
main ( )
{
clrscr ( ) ;
daughter d1;
d1.get ( ) ;
d1.show ( ) ;
}
// Program to show the hierarchical inheritance
#include<iostream.h>
# include<conio. h>
class father //Base class declaration
{
int age;
char name [15];
public:
void get ( )
{
cout<< “father name please”; cin >> name;
cout<< “father’s age please”; cin >> age;
}
void show ( )
{
cout << “father’s name is ‘: “<<name;
cout << “father’s age is: “<< age;
}
};
class son : public father //derived class 1
{
char name [20] ;
int age ;
public;
void get ( ) ;
void show ( ) ;
};
void son : : get ( )
{
father :: get ( ) ;
cout << “your (son) name please” ; cin >>name;
cout << “your age please” ; cin>>age;
}
void son :: show ( )
{
father : : show ( ) ;
cout << “my name is : “ <<name;
cout << “my age is : “ <<age;
}
class daughter : public father //derived class 2.
{
char name [15] ;
int age;
public:
void get ( )
{
father : : get ( ) ;
cout << “your (daughter’s) name please”; cin>>name;
cout << “your age please”; cin >>age;
}
void show ( )
{
father : : show ( ) ;
cout << “my name is: “ << name;
cout << “my age is: “ <<age;
}
};
main ( )
{
clrscr ( ) ;
son S1;
daughter D1 ;
S1. get ( ) ;
D1. get ( ) ;
S1 .show( ) ;
D1. show ( ) ;
}
Hybrid Inheritance
There could be situations where we need to apply two or more types of inheritance to design a program. Basically
Hybrid Inheritance is the combination of one or more types of the inheritance. Here is one implementation of
hybrid inheritance.
//Program to show the simple hybrid inheritance
#include<i sos t ream. h>
#include<conio . h>
class student //base class declaration
{
protected:
int r_no;
public:
void get _n (int a)
{
r_no =a;
}
void put_n (void)
{
cout << “Roll No. : “<< r_no;
cout << “\n”;
}
};
class test : public student //Intermediate base class
{
protected : int part1, part2;
public :
void get_m (int x, int y)
{ parti = x; part 2 = y; }
void put_m (void) {
cout << “marks obtained: “ << “In”
<< “Part 1 = “ << part1 << “in”
<< “Part 2 = “ << part2 << “In”;
}
};
class sports // base for result
{
protected : int score;
public:
void get_s (int s)
{ score = s }
void put_s (void)
{ cout << “ sports wt. : “ << score << “\n\n”; }
};
class result : public test, public sports //Derived from test & sports
{
int total;
public:
void display (void);
};
void result : : display (void)
{
total = part1 + part2 + score;
put_n ( ) ;.
put_m ( );
put_S ( );
cout << “Total score: “ <<total<< “\n”
}
main ( )
{
clrscr ( ) ;
result S1;
S1.get_n (347) ;
S1.get_m (30, 35);
S1.get_s (7) ;
S1.dciplay ( ) ;
}
Virtual Base Classes
The duplication of the inherited members can be avoided
by making common base class as the virtual base class: for
e.g.
class g_parent
{
//Body
};
class parent1: virtual public g_parent
{
// Body
};
class parent2: public virtual g_parent
{
// Body
};
class child : public parent1, public parent2
{
// body
};
The 'child' has two direct base classes ‘parent1’ and ‘parent2’ which themselves has a
common base class ‘grandparent’.

The child inherits the traits of ‘grandparent’ via two separate paths. It can also be inherit
directly as shown by the broken line.

The grandparent is sometimes referred to as ‘INDIRECT BASE CLASS’.


Now, the inheritance by the child might cause some problems.

All the public and protected members of ‘grandparent’ are inherited into ‘child’ twice, first
via ‘parent1’ and again via ‘parent2’.
So, there occurs a duplicacy which should be avoided.

When a class is virtual base class, C++ takes necessary care to see that only one copy of that
class is inherited, regardless of how many inheritance paths exists between virtual base class
and derived class.

The keywords ‘virtual’ and ‘public’ can be used in either order.


//Program to show the virtual base class
#include<iostream.h>
#include<conio . h>
class student // Base class declaration
{ protected: int r_no; public:
void get_n (int a)
{ r_no = a; }
void put_n (void)
{ cout << “Roll No. “ << r_no<< “ln”;}
};
class test : virtual public student // Virtually declared common { //base
class 1
protected:
int part1;
int part2;
public:
void get_m (int x, int y)
{ part1= x; part2=y;}
void putm (void)
{
cout << “marks obtained: “ << “\n”;
cout << “part1 = “ << part1 << “\n”;
cout << “part2 = “<< part2 << “\n”;
}
};
class sports : public virtual student // virtually declared common { //base class 2
protected:
int score;
public:
void get_s (int a) {
score = a ;
}
void put_s (void)
{ cout << “sports wt.: “ <<score<< “\n”;}
};
class result: public test, public sports //derived class
{
private : int total ;
public:
void show (void) ;
};
void result : : show (void)
{ total = part1 + part2 + score ;
put_n ( );
put_m ( );
put_s ( ) ; cout << “\n total score= “ <<total<< “\n” ;
}
main ( )
{
clrscr ( ) ;
result S1 ;
S1.get_n (345)
S1.get_m (30, 35) ;
S1.get-S (7) ;
S1. show ( ) ;
}
//Program to show hybrid inheritance using virtual base classes
#include<iostream.h>
#include<conio.h>
Class A
{
protected:
int x; public:
void get (int) ;
void show (void) ;
};
void A : : get (int a)
{x=a;}
void A : : show (void)
{ cout << X ;}
Class A1 : Virtual Public A
{
protected:
int y ;
public:
void get (int) ;
void show (void);
};
void A1 :: get (int a)
{ y = a;}
void A1 :: show (void)
{
cout <<y ;
{
class A2 : Virtual public A
{
protected:
int z ;
public:
void get (int a)
{ z =a;}
void show (void)
{ cout << z;}
};
class A12 : public A1, public A2
{
int r, t ;
public:
void get (int a)
{ r = a;}
void show (void)
{t= x+ y+z+ r;
cout << “result =” << t ;
}
};
main ( )
{
clrscr ( ) ;
A12 r ;
r.A : : get (3) ;
r.A1 : : get (4) ;
r.A2 : : get (5) ;
r.get (6) ;
r . show ( ) ;
}
REFERENCES:

1.E. Balagurusamy, “Object Oriented Programming with C++”, Fourth edition, TMH,
2008.

2. LECTURE NOTES ON Object Oriented Programming Using C++ by Dr. Subasish Mohapatra,
Department of Computer Science and Application College of Engineering and Technology, Bhubaneswar
Biju Patnaik University of Technology, Odisha

3. K.R. Venugopal, Rajkumar, T. Ravishankar, “Mastering C++”, Tata McGraw-Hill


Publishing Company Limited

4. Object Oriented Programming With C++ - PowerPoint Presentation by Alok Kumar

5. OOPs Programming Paradigm – PowerPoint Presentation by an Anonymous Author

You might also like