Monday, April 5, 2021

constructor in derived classes (OOP) (25)

                                  /*constructor in derived classes

                                 ********************************/


#include<iostream>

using namespace std;

class A //this is the parent class named A

{

private:

int a;

public:

A(int x) //here we define a constructor and receive  a int value as a function arrgument

{

a=x;

cout<<"A class constructor initilized:"<<endl;

}

void displaya()

{

cout<<"the value of a is:"<<a<<endl;

}

};

class B

{

private:

int b;

public:

B(int y) //here we define a constructor and receive  a int value as a function arrgument

{

b=y;

cout<<"B class constructor initilized:"<<endl;

}

void displayb()

{

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

}

};

class C:public A,public B//this is the child class of A and B. this class is inherit with class A&B

{

private:

int c;

public:

//this is the constructor of a child class this constructor received a three integer 

//arrguments. one arrgument is for child class and other two for parent classes.

C(int x, int y ,int z):A(x),B(y) //using the mathod

{

c=z;

cout<<"C class constructor initilized:"<<endl;

}

void displayc()

{

cout<<"the value of c is:"<<c<<endl;

}

};

int main()

{

C obj(10,20,30);//here we making the object of child class and passing the value as a arrguments

obj.displaya();

obj.displayb();

obj.displayc();

}

No comments:

Post a Comment