I'm creating a program and I need the class 'Child' in the following code to inherit the value of 'age' from the class 'Mother'.

#include <iostream>
using namespace std;

class Mother {
	public:
		int TellAge(void);
	protected:
		int age;
} MObj;

class Child:protected Mother {
	public:
		void ChildTellAge(void);
} CObj;

int Mother::TellAge(){return MObj.age=34;}

void Child::ChildTellAge(){cout << "My mommy is " << CObj.age;}

int main() 
{
    MObj.TellAge();
    CObj.ChildTellAge();
	cin.get();
}

But for some reason, the child doesn't inherit the value of 'age', and instead it just prints "0" (and in the case of a string, nothing). Any help would greatly be appreciated!

Recommended Answers

All 8 Replies

Make the data member public.

Make the data member public.

Is this the only method? I only need to pass the value of the variables to one specific sub-class, but I don't want the other values going to the other sub-classes. If this isn't possible in C++, that'll be a shame.

I looked your code over and....The reason child 'doesn't' inherit from mother is your working with two independent objects. MObj and CObj are two different objects with unique data members. You can't expect values in MObj to be reflected in CObj.

Yes, but when I attempt to use the object of a super class in a child class, it doesn't work :/.. Is that where the use of 'public' comes in?

I think your mixing up terminology. What your doing in your program is not inheriting the value from a parent class. What your doing is getting/setting the values of two independent objects..they don't share any common data. So setting one to 34 will not change the other to 34.

Ahh yes. I get it now. So the real question is: Is it possible to pass the data from an object (that is pointing to protected data) down to a child class? I suppose I can create an intermediary variable and then just delete it, but that doesn't seem very efficient.

P.S.
EDIT: I figured out the question below. You must specificy a type of vector to return it(eg: vector<int> func() works).
I don't think it's worth starting a new topic for, so here's the question: If you want to return a vector, what would you put as the identifier before the function (vector func() doesn't work, so what does?).

Ahh yes. I get it now. So the real question is: Is it possible to pass the data from an object (that is pointing to protected data) down to a child class? I suppose I can create an intermediary variable and then just delete it, but that doesn't seem very efficient.

I don't see what the problem is. Just create member functions that get and set the values of the objects and pass the values with them.

The vector question.

std::vector<double> some_function(double d);

I don't see what the problem is. Just create member functions that get and set the values of the objects and pass the values with them.

Oh, I see. I was thinking of a different approach, but yes, that's what I'm looking for. I just started C++ a week ago and I'm just trying to get all the concepts of OOP down.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.