Unit III Inheritance & Stream Class 2021
Unit III Inheritance & Stream Class 2021
as Inheritance Or Derivation
modifying them.
3. The class whose members are inherited is called the base class
4. The class that inherits members from base class is called the
5. The derived class inherits all properties or some properties from the
base class.
6. The derived class will have inherited properties and also its own
properties.
derived class.
2
C++ supports six types of inheritance as follows:
Single Inheritance
Multilevel Inheritance
Multiple Inheritance
Hierarchical Inheritance
Hybrid Inheritance
Multipath Inheritance
3
A derived class
with one base Person
class and that
base class is a
derived class of
another is
student
called
multilevel
inheritance.
exam
student
person
Multiple
derived classes
with same base
class is called student employee
hierarchical
inheritance.
4
combining A person
more than
one type of
inheritance is
called
B C student exam
hybrid
inheritance.
D result
person
A derived class
with two base
classes and student exam
these two base
classes have
one common
base class is
called
result
multipath
inheritance.
5
Advantages:-
4. Data hiding : base class can decide to keep some data private so that
it cannot be altered by the derived class
5. Large Code is easy to manage and divided into parent and child
classes.
6
protected members and class access , base class speicification.
1. Protected members of a base class are like private members, which
are accessed by derived class.
2. The base class spcification determines how private , public, protected
members are accessed when they are inherited by the derived classes.
Modes of Inheritance
1. The private member is not inheritable.
2. So, C++ introduces a third visibility modifier, i.e., protected.
3. The member which is declared as protected will be accessible to all
derived classes.
7
Visibility of Inherited Members
Base class Derived class visibility
visibility
The colon operator : indicates that the derived class is derived from
base class
class baseclass
{
//Body of base class
};
8
class derivedclass : Visibilitymode baseclass
{
//Body of derived class
};
class a
{
protected:
member of class a;
};
class b : public a
{
member of class b;
};
9
//Program to show single inheritance
#include<iostream>
using namespace std;
class person
{ Person
protected:
char sname[10];
public:
void get1() student
{
cout<<"input sname"<<endl;
cin>>sname;
}
};
class student : public person
{
private:
char course[20];
public :
void get2()
{
cout<<"input course"<<endl;
cin>>course;
}
void put()
{
cout<<"sname: "<<sname<<endl;
cout<<"course: "<<course<<endl;
}
};
10
int main()
{
student s;
s.get1();
s.get2();
s.put();
return(0);
}
11
Class hierarchies(multilevel inheritance)
1. A base class can be derived from another base class.
grade
get3() , put()
12
class student : public person
{
protected:
char course[20];
public :
void get2()
{
cout<<"input course"<<endl;
cin>>course;
}
};
13
int main()
{
exam e;
e.get1();
e.get2();
e.get3();
e.put();
return(0);
}
Output:
14
Constructors and destructors in inheritance
6. When the object of a derived class destroyed, first the derived class
destructor is invoked followed by the base class destructor.
15
#include<iostream>
using namespace std;
class A
{
public:
A() // default cons without arguments
{
cout<<"Constructor of A class..."<<endl;
}
~A()
{
cout<<"Destructor of A class.... "<<endl;
}
};
16
class B : public A
{
public :
B()
{
cout <<"Constructor of B ..."<<endl;
}
~B()
{
cout << "\nDestructor of B ..."<<endl;
}
};
class C : public B
{
public :
C()
{
cout << "Constructor of C ..."<<endl;
}
~C()
{
cout << "\nDestructor of C ...";
}
};
int main()
{
C x;
return (0);
}
Output:
Constructor of A class...
Constructor of B class...
Constructor of C class...
Destructor of C ..
Destructor of B ..
Destructor of A ...
17
Polymorphism
1. The term "Polymorphism" greek term .
4. Polymorphism concept is
6. Operator overloading
10 + 20 addition operator
Function overloading
Sum(float a, float b)
18
Compile time polymorphism:
19
If we define two or more functions having same name but different
type of arguments, it is known as overloading.
20
Function Overriding
#include <iostream>
using namespace std; Class A
class A Put()
{
public: Class B
void put()
Put()
{
cout<<"from class A"; from class A
} from class B
};
class B : public A
{
public:
void put()
{
cout<<"from class B";
}
};
int main()
{
B x; // x is object
x.put();
return (0);
} Output: from class B
21
Virtual member functions
1. A C++ virtual function is a member function in the base class that you
redefine in a derived class.
It is declared using the virtual keyword.
2. It is used to tell the compiler to perform dynamic linkage or late
binding on the function.
3. When the function is made virtual, C++ determines which function is
to be invoked at the runtime based on the type of the object pointed
by the base class pointer.
#include <iostream>
using namespace std;
class A
{ Class A
public:
virtual Put()
virtual void put()
{
cout << "from class A"<<endl; Class B
}
}; Put()
class B : public A
{
public:
void put()
{
cout << "from class B"<<endl;
}
};
int main()
{
A * p; //pointer of base class
B b; //object of derived class
p = &b;
p->put();
p->put(); //Late Binding occurs
} Output:
Class A
Class B
22
Abstract base classes and Pure Virtual Function
function.
class.
class base
{
virtual void put()=0;
};
compulsory.
23
program to show pure virtual function and abstract class
#include <iostream>
using namespace std;
class A // abstract class
{
public:
virtual void put() =0; // pure virtual function
};
class B : public A
{ abstract base class
public: Class A
void put()
Virtual Put()=0
{
cout << "from class B";
Class B
}
}; Put()
int main()
{
A * p; //pointer of base class
B b; //object of derived class
p = &b;
p->put(); //Late Binding occurs
Output:
From class B
24
Abstract classes
A. Abstract class
B. Interface
25
Multiple Inheritance
Person Employee
Teacher
Syntax: Example:
class A class person
{ {
}; };
class B class employee
{
{
};
}; class teacher : public person, public employee
class C : public A, public B {
{ };
};
26
Program to show multiple inheritance
#include<iostream.h>
using namespace std;
class person
{
protected:
person employee
char name[10];
public: name salary
void get1() get1() get2()
{
cout<<”input name:”<<endl;
cin>>name;
}
}; teacher
27
class teacher : pubic person , public employee
{
private:
char subject[10];
public:
void get3()
{
cout<<”input subject:”<<endl;
cin>>subject;
}
void put()
{
cout<< name<<salary<<subject;
}
};
int main()
{
teacher t;
t.get1();
t.get2();
t.get3();
t.put();
return(0);
}
28
Stream classes
29
ios class
This class is the base class for all stream classes.
The streams can be input or output streams.
istream Class
ostream class
syntax: example:
30
put() and get() functions:
Syntax:
char alpha;
cin.get(alpha);
cout.put(alpha)
31
porgram :
#include <iostream>
using namespace std;
int main()
{
char alpha;
cout<<”input character”<<endl;
cin.get(alpha);
cout.put(alpha);
return(0);
}
Output:
Input character A
A
=================================================
Syntax:
cin.getline(variable, size);
example:
char sname[10];
cin.getline(sname,10);
32
write()
This function is used to output line of text .
Syntax:
cout.write(variable, size);
#include <iostream>
using namespace std;
int main()
{
char sname[10];
cout<<"input sname";
cin.getline(sname,10);
cout.write(sname,5);
return(0);
}
Output:
input sname anu
anu
33
Formatted I/O in C++
C++ helps to format the I/O operations like
1. width() setw()
2. precision() setprecision()
3. fill() setfill()
4. setf() setiosflags()
5. unsetf() resetiosflags()
endl
hex,dec,oct
34
ios class 5 member functions
1.width():
the width function is used to set the required field width.
cout.width(w);
eg:
cout.width(10);
cout<<”hello”;
cout.width(20);
cout<<”hai”;
2.precision():
It is used to set the number of digits printed to the right of the
decimal point.
cout.precision(number);
ex:
cout.precision(2);
cout<<45.5678;
output: 45.56
3.fill():
the fill method is used to set a character to fill in the blank space
of a field.
cout.fill(‘char’);
eg:
cout.width(10);
cout.fill(‘*’);
cout<<hello;
output: **********hello
35
4.setf():
setf() format flag that control the form of output display either
left justification or right justification.
5.unsetf():
the unsetf method is used to remove the flag setting.
36
Manipulators
1. Manipulators are functions specifically designed to be used in
with the insertion (<<) and extraction (>>) operators on
stream objects.
2. In the program, include < iomanip.h> header file for using
these manipulators .
3. The following are manipulators:
A. endl
B. setw
C. setfill
D. setprecision
E. hex,dec, oct
1.endl
#include<iostream>
int main()
{
cout <<"Hello"<<endl;
cout <<"World!";
}
2.setprecision
37
Example
#include<iostream>
#include<iomanip>
int main()
{
long double pi =3.141592653589793239;
Output
3.setw
This manipulator is used to set the required field width.
Example
#include<iostream>
#include<iomanip>
int main()
{
cout<<setw(10)<<”hello”<<setw(20)<<”world”<<endl;
}
Output
hello world
38
4.setfill
Example:
#include <iostream>
#include <iomanip>
int main()
{
cout<< setw(15) << setfill('*') <<”hello”;
return(0);
}
************hello
int main()
{
int n=52; //decimal number
cout<< dec <<n <<endl;
cout<< hex <<n <<endl;
cout<< oct <<n;
return(0)
}
Output:
Decimal Number=52
Hexadecimal Number=34
Octal Number=64
39