Home > Enterprise >  Is there any way that data can be inherited from one class to another?
Is there any way that data can be inherited from one class to another?

Time:10-18

I am trying to learn object oriented programming, and I got stuck at a problem. I have two class A and B. I am passing command line argument into class A, which then performs some computations and forms a 2d vector. (lets call the vector data)

I want class B to inherit class A.

So I was wondering is there any way, in which when default constructor of class B is called and it prints the contents of 2d vector data.

Sample code of what I have tried

class A
{
        
    public:
        vector<vector<string>>data;
        fstream file;
        string word, filename;
        A()
        {

        }
        A(string fileOpen)
        {
            file.open(fileOpen);
            while (file >> word)
            {
                
                vector<string>rowTemp={word};
                data.push_back(rowTemp);
            }
        }
        vector<vector<string>> getVector()
        {
            return data;
        }
};

class B:A
{
    public:
        B()
        {
            for(auto i:data)
            {
                for(auto j:i)
                {
                    cout<<j<<' ';
                }
                cout<<endl;
            }
        }
};

int main(int argc, char* argv[]){
 
    fstream file;
    string word, filename;
 
    file.open(argv[1]);
    string fileOpen=argv[1];

    A s(fileOpen);
    B c;
    return 0;
}

I basically want class B to have access 2d vector data so that I can perform further computations on it, while logic of computation remains inside class B.

Is there a way to do this?

Also, as you can see inside class A the default constructor is empty. But it is needed because without it, I received an error that default constructor of class B cannot be called. Is there a better way to write this? As having an empty default constructor looks bad.

CodePudding user response:

You seem to misunderstand how inheritance works, it is just not clear what you expected. The thing is: Members of a base class are always inherited. Their access can be limited, but they are still there.

Consider this simplified example:

#include <iostream>

class A {
        
    public:
        int data = 42;
        A() = default;
        A(int value) : data(value) {}
        int getData() { return data; }
};

class B : A {
    public:
        B() {
            std::cout << A::data;         // ok
            std::cout << A::getData();    // ok
        }
};

int main(){ 
    B c;
    //std::cout << c.data; // error: data is private!
}

Output is:

4242

Because B does inherit the data member from A. Inside B you can access data either directly or via getData because both are public in A. However, because B inherits privately from A (thats the default inheritance for classes defined via class) you cannot directly access either data nor getData in main.

Furtermore, when you write:

A s(fileOpen);
B c;

Then s and c are two completely unrelated objects. I suppose you rather want:

B c{fileOpen};

and call As constructor from the constructor of B:

B(const std::string& filename) : A(filename) {
     // now you can access A::data 
     // which has been initialized in 
     // constructor of A
}
  • Related