1

I'am trying to print class A user define funtion via class B. Class A and class B are inherited but still I cannot print "name" and "ID" of a person in main through class B funtion. When I call show() funtion in main Directly it prints Name and ID enterted but when I call class B funtion Name and ID are totally blank.

#include<iostream>
#include<string>

using namespace std;

class A
{
protected:
    string name;
    string ID;
public:
    void set()
    { 
        cout << "Enter your name: ";
        cin >> name;
        cout << "Enter Your ID: ";
        cin >> ID;
    }

    void show()
    {
        cout << "Name: " <<name << endl;
        cout << "ID : " << ID << endl;      
    }
};

class B: public A
{
public:  
    void Bshow()
    {
        cout << "THe name of person and ID of a person is: " << endl;

        A::show();
    }
};


void main()
{
    A a;
    B b;

    a.set();
    cout << endl;

    b.Bshow();
    cout << endl;

    system("pause"); 
}
Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49

3 Answers3

1

When I call show() funtion in main Directly it prints Name and ID enterted but when I call class B funtion Name and ID are totally blank.

I'm guessing you forgot to call b.set() first.

a and b are two different objects. The state of a is set by the call a.set(). That does nothing to set the state of b. Hence, the member variables of b remain default-constructed.

Use:

b.set();
b.Bshow();
cout << endl;
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • there is no set funtion in class B bro.... funtion Bshow() in class B has funtion of class A i.e show().... In main, funtion b.show() having class A function show() does not print ID and Name entered by user. – Muhammad Hamza Jan 08 '17 at 08:02
  • @MuhammadHamza, since `B` is derived from `A`, `A::set` can be called on objects of type `B`. – R Sahu Jan 08 '17 at 08:05
  • @MuhammadHamza, that comment is indicative that you should read through a textbook to understand class inheritance in C++. That will be more useful than asking questions here to understand specific problems with your code. – R Sahu Jan 08 '17 at 08:11
1

The instance of B (b) does not inherit the values of a. It only inherits the set method. You need to call the set method on b, not on a.

https://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm

rob2universe
  • 7,059
  • 39
  • 54
0

Since you have inherited class A with access specifier public, the set() public member function of class A can be accessed publicly with an object of class B. You have forgot to call b.set() first.

Considering you example:

C++ Class definitions do not assign memory. Class is like typedef and struct. C++ Object creation assigns memory. I guess the problem with your understanding is that you were assuming that class definitions will assign them memory and since you have inherited class A into class B, the class B and A will share common set of memory. But it is not what really happens behind the scene.

At memory level, you will have two objects (atleast). One will be of type class A and other will be of class B. The Class A object a will have data members name and ID. And class B will have it's own data members as well as data members inherited from class A. However, object a and object b will both have their own (different) set of data members name and ID. Initializing class A's object a data members (name and ID) by calling a.set() wouldn't initialize the data members of class B's object b.

Instead you should make call to b.set() to initialize the object b's data members name and ID.

Have a look at the code below:

#include<iostream>
#include<string>

using namespace std;

class A
{
protected:
    string name;
    string ID;
public:
    void set()
    { 
        cout << "Enter your name: ";
        cin >> name;
        cout << "Enter Your ID: ";
        cin >> ID;
    }

    void show()
    {
        cout << "Name: " <<name << endl;
        cout << "ID : " << ID << endl;

    }
};


class B: public A
{
public:

    void Bshow()
    {
        cout << "THe name of person and ID of a person is: " << endl;

        A::show();
    }

};


int main()
{
    A a;
    B b;

    b.set();
    cout << endl;

    b.Bshow();
    cout << endl;


    system("pause");
    return 0;

}

You can have a look at this link which explains how inheritance works at memory level.

Community
  • 1
  • 1
abhiarora
  • 9,743
  • 5
  • 32
  • 57