programming-examples/c++/Others/A simple program showing inheritance.cpp
2019-11-15 12:59:38 +01:00

32 lines
549 B
C++

A simple program showing inheritance
#include <iostream>
using namespace std;
class base {
int i, j;
public:
void set(int a, int b) { i = a; j = b; }
void show() { cout << i << " " << j << "\n"; }
};
// inheritance
class derived : public base {
int k;
public:
derived(int x) { k = x; }
void showk() { cout << k << "\n"; }
};
int main()
{
derived ob(3);
ob.set(1, 2); // access member of base
ob.show(); // access member of base
ob.showk(); // uses member of derived class
return 0;
}