You are on page 1of 53

ELECTRONICS

AND
TELECOMMUNICATION
ENGINEERING
B.E. V SEMESTER
Data Structures and Programming
with C++

Subject Code - 328552(28)


Syllabus
• UNIT I - Principles of Object Oriented Programming: Basic Concepts of
Object Oriented Programming, Benefits of OOPs, Classes and Objects: C
Structures Revisited, Specifying a Class, Defining Member Functions, Making an
Outside Function Inline, Nesting of Member Functions, Private Member Functions,
Arrays Within a Class, Memory Allocation for Objects, Static Data M embers,
Static Member Functions, Arrays of Objects, Objects as Function Arguments,
Friendly Functions, Returning Objects. Constructors and Destructors: Constructors,
Parameterized Constructors, Multiple Constructors in a Class, Constructors with
Default Arguments, Dynamic Initialization of Objects, Copy Constructor,
Destructors.

• UNIT II - Operator Overloading and Inheritance: Defining Operator


Overloading, Overloading Unary Operators, Overloading Binary Operators,
Overloading Binary Operators Using Friends, Manipulation of Strings Using
Operators, Rules for Overloading Operators, Function Overloading, Defining
Derived Classes, Single Inheritance, Making a Private Member Inheritable,
Multilevel Inheritance, Multiple Inheritance, Hierarchical inheritance, Virtual Base
Classes, Abstract Classes.
Syllabus
• UNIT III - Pointers and Runtime Binding: Pointers and their Binding,
Address Operator &, Pointer Variables, Void Pointers, Pointer Arithmetic,
Runtime Memory Management, Pointers to Pointers, This Pointer,
Introduction to Virtual Functions, Need for Virtual Functions, Pointer to
Derived Class Objects, Definition of Virtual Functions, Array of Pointers to
Base Class Objects, Pure Virtual Functions, Abstract Classes.

• UNIT IV - Introduction to Data Structures: Searching: Linear Search,


Binary Search, Sorting: Insertion Sort, Bubble Sort and Selection Sort,
Introduction to Linked Lists, Stacks and Queues.

• 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.

#include<iostream.h> complex complex :: operator +(complex c)


#include<conio.h> { complex temp;

class complex temp.x=x+c.x;


temp.y=y+c.y;
{
return(temp);
int x,y;
}
public :
complex()
void main()
{} { clrscr();
complex c1(2,3);
complex(int a,int b) complex c2(4,5);
{ x=a; complex c3;
y=b;
} c3=c1+c2;

void display() c1.display();


{ c2.display();
cout<<x<<"+j"<<y<<"\n"; c3.display();
}
complex operator +(complex c); getch();
}; }
Complex operator + (complex c)
{
complex temp ;

temp
6=2+4 temp.x = Xx + C.X
c.x

8=3+5 temp.y = Yy + C.y


C.Y

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.

complex operator +(complex c);

friend complex operator +(complex c1,complex c2);


Binary Operator Overloading using Friend Function
#include<iostream.h> complex operator +(complex c1,complex c2)
#include<conio.h> { complex temp;
class complex
{ temp.x=c1.x+c2.x;
int x,y; temp.y=c1.y+c2.y;
public :
return(temp);
complex()
}
{ }

complex(int a,int b) void main()


{ x=a; { clrscr();
y=b; complex c3;
} complex c1(2,3);
void display() complex c2(4,5);
{
cout<<x<<"+j"<<y<<"\n"; c3=c1+c2;
} c1.display();
friend complex operator +(complex c1,complex c2); c2.display();
}; c3.display();

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.

Base Class Base Class

Derived Class
Hierarchical Inheritance

• In this type of inheritance, multiple derived classes inherit from


a single base class. Means in this case there is only one base
class and many derived classes.

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.

Base Class Grand Father

Intermediate Base Father


Class

Derived Class Child


Hybrid (Virtual) 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.

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

Class ABC : private XYZ


{ //Public derivation
Members of class ABC;
} Class ABC : public XYZ
{
Members of class ABC; //Private derivation by default
}
Class ABC : XYZ
{
Members of class ABC;
}
Private Inheritance
• When a base class is privately inherited by a derived
class then the ‘public members’ of the base class
become ‘private members’ of the derived class.
• Public members of the base class become ‘private
members of the derived class therefore these members
can not be accessed by the objects of the derived class.
• Hence public members of the base class can only be
accessed by the member functions of the derived class.
• No members of the base class is accessible to the
objects of the derived class.
Write a c++ program to explain private inheritance
#include<iostream.h> void main()
#include<conio.h> { clrscr();
class A B b;
{
b.y=20;
public:
b.add();
int x;
b.display();
void input()
{ x=10; getch();
} }
};
class B : private A
{ public :
int y,z;
void add()
{ input();
z=x+y;
}
void display()
{
cout<<"Sum ="<<z;
}
};
Private Inheritance
Class A
Private No Elements

Public int x;
getdata();

statements

Class B
Private No Elements

Public Int y,z;


Add();
Display()
Public Inheritance
• When a base class is publicly inherited by a derived
class then the ‘public members’ of the base class
become ‘public members’ of the derived class.
• Public members of the base class become ‘public
members of the derived class therefore these
members can be accessed by the objects of the
derived class.
• Private members of the base class is not inherited
and not accessible to the objects of the derived
class.
Public Inheritance
Class A
Private No Elements

Public int x;

statements

Class B
Private No Elements

Public int y,z;


Add();
Display()
Write a c++ program to explain public inheritance
#include<iostream.h> void main()
#include<conio.h> { clrscr();
class A B b;
{ b.x=10;
b.y=20;
public:
b.add();
int x; b.display();
}; getch();
class B : public A }
{

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.

• The private data can be inherited by a derived class by modifying the


visibility mode of the private member by making it public.

• By making private member public , this member will be accessible to all


the functions in the program, so taking away the advantage of data
hiding.

• Hence c++ provides third visibility modifier i.e. “Protected Mode” .

• A member declared as protected mode can be accessed by the member


function within that class and its child class only.

• This member declared as protected can not be accessed by the other


functions outside these two classes.
Visibility of Inherited Members
Write a c++ program to explain the concept of Protected Mode
#include<iostream.h> void mul()
#include<conio.h> { input();
z=y*x;
class A }
{ protected: void display()
int x; {
public: cout<<"\nx= "<<x;
int y; cout<<"\ny= "<<y;
void input() cout<<"\nz= "<<z;
{ x=10; }
} };
};
void main()
class B : public A {clrscr();
{ B b;
public : b.y=20;
int z; b.mul();
b.display();
getch();
}
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.

Base Class Grand Father

Intermediate Base Father


Class

Derived Class Child


Write a c++ program to explain the concept of Multilevel Inheritance

#include<iostream.h> class C :public B


#include<conio.h> {
class A public:
{ int total;
public: void display()
int RollNo; {
void input() total = sub1+sub2;
{ cout<<"Enter the Roll Number of Student"; cout<<"\nRollNo = "<<RollNo;
cin>>RollNo; cout<<"\nsub1 = "<<sub1;
} cout<<"\nsub2 = "<<sub2;
}; cout<<"\nThe Total Marks is ="<<total;
class B : public A }
{ };
public:
int sub1,sub2;

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.

Base Class Base Class

Derived Class
General syntax to derive multiple classes

class derived_class : visibilty_mode B1,visibility_mode B2


{
………………..
………………..
Body of the derived class.
………………..
};
#include<iostream.h>
#include<conio.h> class C :public B,public A
{
class A
public:
{
int total;
public :
int RollNo; void display_total()
void input() {
{ total = sub1+sub2;
cout<<"Enter the Roll Number of Student"; cout<<"\nRollNo = "<<RollNo;
cin>>RollNo; cout<<"\nsub1 = "<<sub1;
} cout<<"\nsub2 = "<<sub2;
cout<<"\nThe Total Marks is ="<<total;
};
}
};
class B
{
public: void main ()
int sub1,sub2; { clrscr();
void input_marks() C c;
{ c.input();
cout<<"Enter the sub1 and sub2 marks";
c.input_marks();
cin>>sub1>>sub2;
c.display_total();
}
getch();
}; }
Hierarchical Inheritance

• In this type of inheritance, multiple derived classes inherit from


a single base class. Means in this case there is only one base
class and many derived 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

• 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.

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

You might also like