1
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?

Mykola
  • 3,343
  • 6
  • 23
  • 39
user3239138
  • 143
  • 4
  • 17
  • `can use the variables from class A but can I also grab their values?` So, if you can access variable, whats the problem. – Pranit Kothari Mar 20 '14 at 06:45
  • I'm having trouble with my B object. It requires me to input 3 numbers including the one I have in B called extra. I mean, I can just put 1,2,3,100 but I want to know is there a way I can use class A's object without doing so. – user3239138 Mar 20 '14 at 06:49
  • 1
    I think you have misunderstood what inheritance means. – ebarr Mar 20 '14 at 06:50

4 Answers4

0

you are confused with concept of inheritance,

 B yo2(4,5,6,100); 

during this there is separate copy of base class created for the object yo2 which has nothing to do with the object yo1

A yo1(1,2,3,);

the data members x,y,z will have 1,2,3 for yo1 and 4,5,6 for yo2 if you want the output to be 1 2 3 100 then create the object as :

 B yo2(1,2,3,100); 
balaji
  • 81
  • 6
  • Yes, I know that if I want 1 2 3 100 i can just put the values in my B object, but is there a way to get the values of A yo1(1,2,3) and using it in my B class – user3239138 Mar 20 '14 at 06:51
  • you can use cpy constructor – Ali Kazmi Mar 20 '14 at 06:59
  • but that will like the step i mentioned , he wants that while initializing he will give different data but the old data should stay – balaji Mar 20 '14 at 07:01
0

you can modify your derived class as

class B : public A
{   
public:
B(A& obj, int extra)
: A(obj.x, obj.y, obj.z), num(extra)
{}

void display()
{
    A::display();
    cout << "num is " << num << endl;
}
private:
int num;    
};

and in main you can access as

int main()
{
    A obj(1,2,3);
    B objB(obj,100);
    objB.display();
}
rajenpandit
  • 1,265
  • 1
  • 15
  • 21
0

I think you might have misunderstood inheritance.

There is no way for yo2 to know about the values you have given to yo1. The values yo2 have in A's variables are 4,5,6 respectively.

to illustrate inheritance a little you could rename the display methods "display_a" and "display_b" and then you could access them thus:

yo1.display_a(); // OK. prints values 1,2,3 yo1.display_b(); // Compile error.

yo2.display_a(); // OK. prints values 4,5,6 yo2.display_b(); // OK. prints values 4,5 from variables inherited from A.

To get what happens with the two "display" methods in your original code, look up overloading and the key-word "virtual". Also look up the operator ::

I hope this helps.

Samuel Åslund
  • 2,814
  • 2
  • 18
  • 23
0

First, I think you have misunderstood what inheritance means and yo1 has nothing to do with yo2 at all. Second, you want to use B display function to display A object's members but you are lazy to construct B with argument 1,2,3 because A has constructed using those. Because A's y is protected and A's z is private, you must use a way to copy A object's members to B object if you want achieve your goal. So I think the rajenpandit's answer is right and useful for you.

Thinking
  • 13
  • 2