Inheritance Confusion?

Hello everyone,

I have a program with class and derived class.

On my derived class Dog in the function string getLifespan()

I am trynig to read the weight from base and check, and diplay return a string.

But it looks that weight is allways 0, regrdless that i have a different value from class Pet.

I think weight is set to when a class Dog object is crated, and if that is the case I do not know how to fix and missing where this is converting weight to 0

Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

class Pet
{
private:
	string name;
	//char name[20];
	int age;
	double weight;
public:
	Pet();
	Pet(string, int, double); 
	string getName()const;
	int getAge()const;
	double getWeight()const;
	void setName(string);//char[20]
	void setAge(int);
	void setWeight(double);
	void showInfo()const;
	string getLifespan();
};
///////////////////////////////////////////////////////////////////////////////
// Class Derived Dog
class Dog: public Pet
{
private:
	string breed;
public:
	//Dog():Pet()
	//{ breed = "";}
	//Dog(string n, int a, double w, string b):Pet(n, a, w),breed(b){}

	Dog();
	Dog(string);
	string getBreed()const;
	void setBreed(string);
	string getLifespan();
};
///////////////////////////////////////////////////////////////////////////////
// Class Derived Rock

class Rock : public Pet
{
public:
	string getLifespan()
	{
		string s;
		s = "Thousands of years";

		Pet::getLifespan();
		
		return s;
	}
};
///////////////////////////////////////////////////////////////////////////////
// The Class Pet declerations

Pet::Pet()
{
	name = "";
	age = 0;
	weight = 0.0;
}
Pet::Pet (string n, int a, double w)
{
	name = n;
	age = a;
	weight = w;
}
string Pet::getName()const
{
	return name;
}
int Pet::getAge()const
{
	return age; 
}
double Pet::getWeight()const
{
	return weight;
}
void Pet::showInfo() const
{	
	cout << endl;
	cout <<"Name   : " << name   << endl;
	cout <<"Age    : " << age    << endl;
	cout <<"Weight : " << weight << endl;
	cout << endl;
}
void Pet::setName(string n)
{

	name = n;
}
void Pet::setAge(int a)
{
	age = a;
}
void Pet::setWeight(double w)
{
	weight = w;
}
string Pet::getLifespan()
{
	string s;
	s = "unknown lifespan";

	return s;
}

///////////////////////////////////////////////////////////////////////////////
// The Class Dog Declarations
 
Dog::Dog()
{
	breed = "";
}
Dog::Dog(string b)
{
	breed = b;
}
string Dog::getBreed() const
{
	return breed;
}
void Dog::setBreed(string b)
{
	breed = b;
}
string Dog::getLifespan()
{
	string s1, s2;
	double w;

	s1 = "The lifespan is under 7 years.";
	s2 = "The lifespan is 13 years.";
	
	w = Pet::getWeight();
	Pet::getLifespan();
	
	if (w > 100)
	{
		return s1;
	}
	else if (w < 100)
	{
		return s2;
	}
}

int main()
{
	Pet p; // base object default
	Pet p1("Object2", 2, 100); //base object with argument

	Dog d; // Derived Dog object

	Rock r; // Derived Rock object

	string s; // to get the string from getLifespan() and store it.
	string n = "Sparky";// used on the base class Pet for name
	int a = 1;
	double w = 101.00;
	string b = "Bichon-Puddle";// used on the derived class Dog for breed

	p.setName(n);   // reset the name of the class Pet 
	p.setAge(a);    // reset the age of the class Pet
	p.setWeight(w);	// reset the weight of the class Pet

	d.setBreed(b);  // reset the breed type of the derived class Dog

	cout <<"This is the get functions: " << endl;
	cout << p.getName();   cout << endl;
	cout << p.getAge();    cout << endl;
	cout << p.getWeight(); cout << endl;

	cout << d.getBreed();  cout << endl;
	
	cout << endl;
	cout <<"This p1(object with argument) is the Info function: \n";
	p1.showInfo();

	cout <<"Base getLifespan() = ";
	s = p.getLifespan();
	cout << s << endl;

	cout << endl;
	cout <<"Derived Dog getLifespan() =  ";
	
	s = d.getLifespan();
	cout << s << endl;

	cout << endl;
	cout <<"Derived Rock getLifespan() = ";
	s = r.getLifespan();
	cout << s << endl;

	return 0;
}


Please take a look and see what I am doing wrong.

Thanks in advance
The weight is always zero because you never set it to anything else.

 
Dog d; // Derived Dog object 


When you create 'd', Dog's default constructor is called, which in turn calls Pet's default constructor, which zero's d's weight.

You never change d's weight to anything else. So it remains at zero.
Thank a lot Disch.

I can't belive I missed that.
Topic archived. No new replies allowed.