Method Overriding in C++
Master C++ with Real-time Projects and Kickstart Your Career Start Now!!
Program 1
// Inheritance in CPP
#include<iostream>
using namespace std;
class Base
{
public:
Base(int n)
{
cout<<"Constructor of Base class";
}
Base(){}
};
class Derived:public Base
{
public:
Derived(){}
Derived(int n):Base(n)
{
cout<<"\nConstructor of Dervied class";
}
};
class Derived1:public Derived
{
public:
Derived1(){}
Derived1(int n):Derived(n)
{
cout<<"\nConstructor of Dervied 1 class";
}
};
int main()
{
system("cls");
//Base B;
Derived1 D(500);
return 0;
}Program 2
// Inheritance in CPP
#include<iostream>
using namespace std;
class MyMath
{
protected:
int a,b,c;
public:
MyMath()
{
a=500;
b=100;
}
void add()
{
c=a+b;
cout<<"addition is : "<<c;
}
};
class MyMath1:public MyMath
{
public:
MyMath1(){}
void swap()
{
cout<<"\nBefore swaping : "<<a << " "<<b;
c=a;
a=b;
b=c;
cout<<"\nAfter swaping : "<<a << " "<<b;
}
};
int main()
{
system("cls");
MyMath1 M;
//M.add();
M.swap();
return 0;
}Program 3
// Method overriding
#include<iostream>
using namespace std;
class Base
{
public:
void show()
{
cout<<"\n Show method with zero parameter of base class";
}
};
class Derived:public Base
{
public:
void show()
{
cout<<"\n Show method of derived class zero paramert ";
}
};
int main()
{
system("cls");
Derived D;
D.show();
return 0;
} Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

