18BCS33C U3
18BCS33C U3
Coimbatore – 641018
Re-Accredited with ‘A’ grade by NAAC
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
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.
Constructor
• This is done with the help of a special function called operator function, which describes
the task.
• operator functions must be either member function, or friend function.
• A basic difference between them is that a friend function will have only one
argument for unary operators and two for binary operators.
• This is because the object used to invoke the member function is passed
implicitly and therefore is available for the member functions.
• Arguments may be either by value or by reference.
1. Create a class that defines the data type that is used in the overloading operation.
2. Declare the operator function operator op() in the public part of the class
void main()
{
complex s(3,4);complex t(4,5);
complex m;
m=operator-(s,t);
s.show();t.show();
m.show();
}
Overloading an operator does not change its basic meaning. For example assume the + operator can be overloaded to
subtract two objects. But the code becomes unreachable.
class integer
{
int x, y;
public:
int operator + ( ) ;
}
int integer: : operator + ( )
{
return (x-y) ;
}
Unary operators, overloaded by means of a member function, take no explicit argument and return no explicit values. But,
those overloaded by means of a friend function take one argument (the object of the relevant class).
Binary operators overloaded through a member function take one explicit argument and those which are overloaded
through a friend function take two explicit arguments.
Operator to Overload Arguments passed to the Member Function Arguments passed to the Friend Function
Unary Operator No 1
Binary Operator 1 2
Types of Inheritance
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.
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.
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