#include <iostream>
#include <string>
Class Overloading
Public:
String x,y,z;
{z+x+y;;}
Cout<<”final string”<<Z;
}};
Int main()
Overloading a;
a.x=ABC;
a.y=”DEFm ”;
+a;
a.display();
return o;
inheritance
involves two classase one will be source/base and the other will be derived
classes
(A) (B)
(Source/base) (derived)
-> Inheritance is when there is a relationship between two classes at least , one can access the
properties of the other.
Note(er database)
Class<base>
};
Class<derived>:<access-specifier> <Base>
};
// implementing inheritance
Base
Name -talk
Age -walk
Profession -eat
Derived
Class person{
Public:
Int age;
Cout<<name<<prof<<age;
};
{void music ()
{cout<<”I can play music\n”;
}};
{void soccer(){
};
Int main(){
Teacher t;
t.name=”david”;
t.age=26;
t.prof=”teacher”;
t.info();
t.canTeach();
musician m;
m.name=”yves”;
m.age=40;
m.prof=”Musician”;
m.info();
soccerPlayer s;
s.name =”sam”;
s.age=17;
s.prof=”soccerplayer”;
s.info();s.soccer()
return 0;
Types of inheritance
9. There are four types of inheritance`
I. Simple inheritance(1 base ,1 derived)
II. Multilevel inheritance
III. Multiple inheritance(many base class for 1 derived)
IV. Hierarchical inheritance(single base classes, multiple derived
Class <derived>:<access-specifeir><base>
};
2) Multiple inheritance
N-base classes, one derived class
Class<Base-1>{ };
Class<Base-2>{ };
Class <Derived> :<access-speciufier>,base-1,
<access-specifier><base-2>{ };
3) Hierarchical inheritance
one bass class ,N derived classes
class <Base>{ };
class <derived-1>:<access-specifier><base>{ };
class<derived-2><access-specifier><base>{ };
4) Multilevel inheritance
The next class is derived of previous ,adjacent class
Lets say we have 3 classes A , B, C
A is the base class of B.
B is the base class of C, and the derived class of A
C is the derived class of b
Class A{ };
Class B:publicA {};
Class C:public B{};
A
derived| base
B
derived|base
C
derived|base
D
derived|base
E
Person
Multilevel inheritance
class A
{
public: int a, b;
void inputdata()
{
cout<<”Enter 2 numbers”;
cin>> a>>b;
}
};
class B:public A
{
public: int x, y;
void result()
{
cout<<”Sum is ” << a+b;
}
};
class C:public B
{
public:
void product()
{
cout<<”Result is ” << x*y;
}
};
int main()
{
B n;
n.inputdata();
n.result();
C m;
m.x=12;
m.y=20;
m.product();
return 0;
}
//read and print employee info using public multiple
inheritance
N-base classes, one derived class
Class<Base-1>{ };
Class<Base-2>{ };
Class <Derived> :<access-speciufier>,base-1,
<access-specifier><base-2>{ };
Base employee
Name
Age
Idno
Base 2
Company
Branch
Division()
position
worker()
derived 1
function overriding
class base{
};
};
Function overriding
Overriding a function is nothing but duplicating both in a base class and a derived class.
Concept:suppose we have a software using object oriented programming everytime we don’t want to
use previous values sometimes we can have multiple inmheritance no problem for this we don’t need
function overridi ng now in these case now we have base clkas which has four member and we ant to
duplicate this task and we need to use function overridind to quickly release memory
\ function we have
Int main ()
Derived d;
d.function();return 0;
class base{
public:void input(){
cout<<”data is input\m”
}};
\{public:void input()
};
Int main()[
Derived;
x.input();return 0
In derived insert
}};
Book-s.no-title-price
In the derived class it will help update information of a book using method/function overriding
#include <cmath>Hybrid inheritance (mixing inheritance)
Class cube
{ public;
Void volume(){
Cout<<”volume is\n”;
Cout <<side*side*side;endl,}
};
Class cuboid{
Public:int length,breadth,height;
Void intro(){
Cube::intro();}
}};
{public:int width;
Void shapedetails(){
Cout<<width*length*height;}}
Int main()
cubec;c.intro();c.volume();c.side=4
cuboid cu
cu.shapedetails()
cu.length+12
cu.height=4
cu.breasdth=2;