Data Structures and Programming With C++ - U2-2
Data Structures and Programming With C++ - U2-2
AND
TELECOMMUNICATION
ENGINEERING
B.E. V SEMESTER
Data Structures and Programming
with C++
• UNIT V - Files and Streams: Classes for File Stream Operations, Opening
and Closing a File, Detecting end-of file, More about Open:File Modes,
File Pointers and Their Manipulations, sequential Input and Output
Operations, Updating a File: Random Access, Error Handling During File
Operations, Command-line Arguments, Introduction to Templates and
Exception Handling.
Text Books
1. Object oriented Programming with C++, E
Balaguruswamy, 3rd Edition, Mcgraw-Hill.
2. Data Structures, Seymour Lipschutz,
Schaum’s Outline Series.
UNIT-II
Operator Overloading and
Inheritance
Overloading
Function Operator
Overloading Overloading
Operator Overloading
• Operator overloading is an important concept in C++.
• It is a type of polymorphism in which an operator is
overloaded to give user defined meaning to it.
• This means that C++ has the ability to provide the
operators with a special meaning for data types. And the
mechanism of giving such special meanings to an operator
is known as operator overloading.
• Overloaded operator is used to perform operation on user-
defined data type. For example '+' operator can be
overloaded to perform addition on various data types, like
for Integer, String(concatenation) etc.
The general form of an operator overloading is:
Operator Overloading
5
+ 5+6=11
6
When we overload the ‘+’ operator it can add the string also
Hello
Sir
+ Hello + Sir = HelloSir
Unary Operators Overloading
The unary operators operate on the object for which they were called and normally,
this operator appears on the left side of the object, as in -obj.
#include<iostream.h> Void operator -()
#include<conio.h> { x=-x;
class test y=-y;
{ z=-z;
int x,y,z; }
public : };
void input(int a,int b,int c) void main()
{x=a; { clrscr();
y=b; test t;
z=c; t.input(10,-20,30);
} t.display();
void display() -t;
{ t.display();
cout<<"\nX= "<<x; getch();
cout<<"\nY= "<<y; }
cout<<"\nZ= "<<z;
}
Binary Operator Overloading
• Addition (+) Operator • Normally if we have to
• Multipliction (*) Operator add two numbers then
• Subtraction (-) Operator we write a statement
• Division (/) Operator like this
C=sum(A,B);
• The above notation can
be replaced by a natural
looking expression.
C=A+B;
Binary Operator Overloading
Write a c++ program to add two complex numbers using binary operator overloading.
temp
6=2+4 temp.x = Xx + C.X
c.x
Return (temp);
}
C3 = C1 + C2
6 X
2 X 4 X
8 Y 3 Y 5 Y
Binary Operator Overloading using Friend
Function
• Friend function may be used in place of member
functions to overload Binary Operators.
• A friend function requires two arguments to be explicitly
passed to it , while a member function requires only one.
• for ex.
getch();
}
Rules for operator overloading
• Only built-in operators can be overloaded. New operators can
not be created.
• The basic meaning of the operators cannot be changed.
• Precedence and associativity of the operators cannot be
changed.
• Overloaded operators cannot have default arguments except
the function call operator () which can have default arguments.
• Assignment (=), subscript ([]), function call (“()”), and member
selection (->) operators must be defined as member functions.
• Except the operators specified in above point , all other
operators can be either member functions or a non member
functions.
• Some operators like (assignment)=, (address)& and comma (,)
are by default overloaded.
Function Overloading
• Function overloading is a C++ programming feature
that allows us to have more than one function having
same name but different parameter list.
• Different parameter list means the data type and
sequence of the parameters is different.
• For ex.
sum(int num1, int num2);
sum(int num1, int num2, int num3);
sum(int num1, double num2);
C++ program to find area of square, rectangle, circle and triangle
by using function overloading
#include<iostream.h>
void area(int s)
#include<conio.h>
void area(int); {
void area(int,int); cout<<"\nArea of square is"<<s*s;
void area(float);
}
void area(float,float);
void main()
{ void area(int l,int b)
int s,l,b;
{
float r,bs,ht;
cout<<"\nArea of rectangle is "<<l*b;
cout<<"Enter side of a square:"; }
cin>>s;
cout<<"Enter length and breadth of rectangle:";
cin>>l>>b; void area(float r)
cout<<"Enter radius of circle:"; {
cin>>r;
cout<<"Enter base and height of triangle:";
cout<<"\nArea of circle is "<<3.14*r*r;
cin>>bs>>ht; }
area(s);
area(l,b);
void area(float bs,float ht)
area(r); {
area(bs,ht); cout<<"\nArea of triangle is
"<<((bs*ht)/2);
getch();
} }
Inheritance
• The C++ strongly supports the concept of reusability.
• Based on the requirement the programmers can use the code of the
existing classes.
• When the programmer is creating new classes then the functions and
variables of the existing classes can be reused.
• The mechanism of deriving or creating new classes from an old one is
called inheritance or derivation.
• The old class is referred to as the base class and the new one is called
the derived class or subclass.
Types of Inheritance
Single Inheritance
• In this type of inheritance one derived class inherits from
only one base class. It is the simplest form of Inheritance. In
this case one base class and one derived class.
Base Class
Derived Class
Multiple Inheritances
• In this type of inheritance a single derived class may inherit
from two or more than two base classes.
• In this case many base classes and only one derived class.
Derived Class
Hierarchical Inheritance
Base Class
Derived Class
Multilevel Inheritance
• In this type of inheritance the derived class inherits from a class, which in turn inherits from some other
class. The Super class for one is sub class for the other.
• Class A serves as a base class for the derived class B, which in turn serves as a base class for the derived
class C. The class B is known as intermediate base class since it provides a link for the inheritance
between A and C. the chain ABC is known as inheritance path.
Hierarchical
Inheritance
Multilevel
Inheritance
Multiple
Inheritance
Defining Derived Classes
• A derived class can be defined by specifying its relationship with
the base class in addition to its own details.
• The colon indicates that child class is derived from base class .
• The visibility mode is optional and default
visibility mode is private.
• Examples
//Private derivation
Public int x;
getdata();
statements
Class B
Private No Elements
Public int x;
statements
Class B
Private No Elements
public :
int y,z;
void add()
{
z=x+y;
}
void display()
{
cout<<"Sum ="<<z;
}
};
Sample Program 1
#include<iostream.h> void main()
#include<conio.h> { clrscr();
class A B b;
{ b.x=10;
public: b.y=20;
int x,y; b.z=30;
};
cout<<"\nThe value of x is "<<b.x;
class B : public A
cout<<"\nThe value of y is "<<b.y;
{
cout<<"\nThe value of z is "<<b.z;
public:
getch();
int z;
}
};
Sample Program 2
#include<iostream.h> void display()
#include<conio.h> {
class A cout<<"\nx= "<<x;
{ cout<<"\ny= "<<y;
public: cout<<"\nz= "<<z;
}
int x,y;
};
};
void main()
class B : public A { clrscr();
{ B b;
public : b.x=10;
int z; b.y=20;
void mul() b.mul();
{ b.display();
getch();
z=y*x;
}
}
Sample Program 3
#include<iostream.h> class B : private A
#include<conio.h> {
class A public:
int z;
{
void mul()
public :
{ input();
int x,y; z=x*y;
void input() }
{ void show()
x=10; {
display();
y=20;
cout<<"\nz = "<<z;
} }
void display() };
{ void main ()
cout<<"\nx = "<<x; { clrscr();
cout<<"\ny = "<<y; B b;
b.mul();
}
b.show();
}; getch();
}
Sample Program 4
#include<iostream.h> void mul()
#include<conio.h> { input();
class A z=y*get_x();
{ int x;
}
public:
void display()
int y;
void input() {
{ x=10; cout<<"\nx= "<<get_x();
} cout<<"\ny= "<<y;
int get_x() cout<<"\nz= "<<z;
{ }
return x;
};
}
void main()
};
{ B b;
class B : public A //b.x=10;
{ b.y=20;
public : b.mul();
int z; b.display();
getch();
}
Making a Private Member Inheritable(Protected Mode)
• We know that a private member of the base class can not be inherited
and it is not available for the derived class directly.
void main ()
void input_marks()
{ clrscr();
{ cout<<"Enter the sub1 and sub2 marks";
C c;
cin>>sub1>>sub2;
c.input();
}
c.input_marks();
c.display();
};
getch();
}
Multiple Inheritances
• In this type of inheritance a single derived class may inherit
from two or more than two base classes.
• In this case many base classes and only one derived class.
Derived Class
General syntax to derive multiple classes
Base Class
Derived Class
#include<iostream.h> class D : public A
#include<conio.h> {
class A public :
{public : void mul()
int x,y; {
cout<<"\nThe multiplication of x and y is ="<<x*y;
void input()
}
{
};
cin>>x>>y;
}
}; void main()
{
class B : public A clrscr();
{ public : B b;
void add() C c;
D d;
{ cout<<"\nThe summation of x and y is ="<<x+y;
cout<<"\nEnter the value of X and Y for Addition";
}
b.input();
}; b.add();
cout<<"\nEnter the value of X and Y for
class C: public A Substraction";
{public : c.input();
void sub() c.sub();
cout<<"\nEnter the value of X and Y for
{ cout<<"\nThe subtraction of x and y is ="<<x-y; Multiplication";
} d.input();
}; d.mul();
getch();
}
Hybrid (Virtual) Inheritance
Hierarchical
Inheritance
Multilevel
Inheritance
Multiple
Inheritance
#include<iostream.h> class C :public B,public S
#include<conio.h>
class A
{
{ public:
public : int total;
int RollNo;
void display_total()
void input()
{ cout<<"Enter the Roll Number of Student"; {
cin>>RollNo; total = sub1+sub2+sport_marks;
}
cout<<"\nRollNo = "<<RollNo;
};
cout<<"\nsub1 = "<<sub1;
class B : public A cout<<"\nsub2 = "<<sub2;
{ cout<<"\nSport Marks = "<<sport_marks;
public:
int sub1,sub2; cout<<"\nThe Total Marks is ="<<total;
void input_marks() }
{ };
cout<<"Enter the sub1 and sub2 marks";
cin>>sub1>>sub2;
} void main ()
}; { clrscr();
class S
{
C c;
public : c.input();
int sport_marks; c.input_marks();
void i_s_marks()
c.i_s_marks();
{ cout<<"Enter the sport marks of Student";
cin>>sport_marks; c.display_total();
} getch();
};
}
Virtual Base Classes
Grand Parent
Parent - 1 Parent - 2
Child
• In the previous diagram the ‘Child’ has two direct base
classes ‘Parent -1’ and ‘Parent – 2’ which have a
common base class ‘Grand Parent’.
• The child inherits the properties of ‘Grand Parent’
through two separate paths.
• All the public and protected members of ‘Grand Parent’
are inherited into ‘Child’ twice first ‘Parent -1’ and again
through ‘Parent -2’.
• This means ‘Child’ would have duplicate sets of
members inherited from ‘Grand Parent’.
• This introduces ambiguity and should be avoided.
• The duplication of inherited members due to these
multiple paths can be avoided by making the common
base class as virtual base class during inheritance.
Class grandparent
{
……….
……….
};
Class parent-1 : virtual public grandparent
{
………
………
};
Class parent-2 : virtual public grandparent
{
………….
………….
};
Class Child :public parent-1,public parent-2
{
…………
…………
};
class S : virtual public A
{
#include<iostream.h>
public :
#include<conio.h> int sport_marks;
class A void i_s_marks()
{ { cout<<"Enter the sport marks of Student";
public : cin>>sport_marks;
int RollNo; }
void d_s_marks()
void input()
{
{ cout<<"Enter the Roll Number of Student";
cout<<"\nSport Marks = "<<sport_marks;
cin>>RollNo; }
} };
void display() class C :public B,public S
{ {
cout<<"\nRollNo = "<<RollNo; public:
} int total;
void display_total()
};
{
class B : virtual public A total = sub1+sub2+sport_marks;
{ cout<<"\nThe Total Marks is ="<<total;
public: }
int sub1,sub2; };
void input_marks() void main ()
{ clrscr();
{ cout<<"Enter the sub1 and sub2 marks";
C c;
cin>>sub1>>sub2;
c.input();
} c.input_marks();
void display_marks() c.i_s_marks();
{ cout<<"\nsub1 = "<<sub1; c.display();
cout<<"\nsub2 = "<<sub2; c.display_marks();
} c.d_s_marks();
}; c.display_total();
getch();
}