Here is the source code of the C++ program illustrates inheritance. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to Illustrate Inheritance
*/
#include <iostream>
class Base {
protected:
int data;
public:
Base(int val = 0) : data(val) { }
int getData(void) const { return data; }
};
class Derived : public Base {
public:
void changeData(int val)
{
std::cout << "Change of Derived::data from "
<< data << "->" << val << std::endl;
data = val;
}
};
int main()
{
Base b;
Derived d;
d.changeData(20);
// getData is available to Derived Class
std::cout << "Base Class data = " << b.getData() << std::endl;
std::cout << "Derived Class data = " << d.getData() << std::endl;
}
$ a.out Change of Derived::data from 0->20 Base Class data = 0 Derived Class data = 20