Monday, April 5, 2021

single inheritance (OOP) (20)

  /*single inheritance

                                   *******************

in single inheritance one parent class and one child class*/

#include<iostream>

using namespace std;

class A  //this is the parent class named A

{

private: //this is the private part of the parent class

int a;

public://this is the public part of the parent class

void gatvalue_a(int);

int gat_a();

};

class B:public A // here we are inherit the class B with class A. this is also known as inheritance.

{

private: //this is the private part of the child class

int b,c;

public: //this is the public part of the child class

void gatvalue_b(int);

void add();

void display();

};

//here we define our member function of class A and class B 

void A:: gatvalue_a(int x)

{

a=x;

}

int A:: gat_a()

{

return a;

}

void B:: gatvalue_b(int y)

{

b=y;

}

void B :: add()

{

c=gat_a();

c=c+b;

}

void B:: display()

{

cout<<"value of a is:"<<gat_a()<<endl;

cout<<"value of b is:"<<b<<endl;

cout<<"the sum is:"<<c<<endl;

}

int main()

{

int x,y;

B obj1;

cout<<"please enter the value of a:";

cin>>x;

cout<<"please enter the value of b:";

cin>>y;

obj1.gatvalue_a(x);

obj1.gat_a();

obj1.gatvalue_b(y);

obj1.add();

obj1.display();

}

No comments:

Post a Comment