class A
{
public:
A(int a, int b, int c)
:x(a), y(b), z(c)
{}
void display()
{
cout << "x is " << x << endl;
cout << "y is " << y << endl;
cout << "z is " << z << endl;
}
int x;
protected:
int y;
private:
int z;
};
class B : public A
{
public:
B(int x, int y, int z, int extra)
: A(x, y, z), num(extra)
{}
void display()
{
cout << "x is " << x << endl;
cout << "y is " << y << endl;
//cout << "z is " << z << endl;
}
private:
int num;
};
int main()
{
A yo1(1,2,3,);
B yo2(4,5,6,100); //<---
yo2.display(); //I want to print 1 2 3 100
return 0;
}
I have a simply inheritance here. Using inheritance, I want to get A
's values and put it into B
. How do I access the values of its data members from class A
by created a B
object? I created an object of class A
and gave them the value 1,2,3. In the end, I want to print 1 2 3 100 by using class B
's display function.
I understand how I can use the variables from class A
but can I also grab their values?