Initializing Variables in Derived Classes

Hi, I have my main class with multiple variables and a constructor. Then I derived other classes that use the same variables from the base class and I want to initialize them accordingly using the constructor of the base class.

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
#include <iostream>
#include <string>
using namespace std;

class animal{
public:
	virtual void name(){cout<<"This animal is a : "<<type;};
	virtual void phy(){cout<<"The animal's height and weight are as follows: "<<endl<<"Hieght: "<<height<<"\t"<<"Weight: "<<weight<<endl;}
	virtual void config(){cout<<"Enter a new height (using standard units): "; cin>>height; cout<<"\nEnter a new weight (using standard units): ";cin>>weight; cout<<"Here are the new height and weight values:"<<endl<<height<<weight<<endl;}
	virtual void convert(){ metric_height = height * 2.8;  metric_weight = weight / 2.2; }
	animal(string a="NULL", int b=0, int c=0){type=a; height=b; metric_height=b; weight=c; metric_weight=c;}

	string type;
	int height,weight,metric_height,metric_weight;
};

class lion: public animal{
	public:
	 void name(){cout<<"This animal is a : "<<type;};
	 void phy(){cout<<"The animal's height and weight are as follows: "<<endl<<"Hieght: "<<height<<"\t"<<"Weight: "<<weight<<endl;}
	 void config(){cout<<"Enter a new height (using standard units): "; cin>>height; cout<<"\nEnter a new weight (using standard units): ";cin>>weight; cout<<"Here are the new height and weight values:"<<endl<<height<<weight<<endl;}
	 void convert(){ metric_height = height * 2.8;  metric_weight = weight / 2.2; }
	

};

class dog: public animal{
	public:
	 void name(){cout<<"This animal is a : "<<type;};
	 void phy(){cout<<"The animal's height and weight are as follows: "<<endl<<"Hieght: "<<height<<"\t"<<"Weight: "<<weight<<endl;}
	 void config(){cout<<"Enter a new height (using standard units): "; cin>>height; cout<<"\nEnter a new weight (using standard units): ";cin>>weight; cout<<"Here are the new height and weight values:"<<endl<<height<<weight<<endl;}
	 void convert(){ metric_height = height * 2.8;  metric_weight = weight / 2.2; }
};


int main(){

lion a("lion",45,300); //Here how do I assign these values to the variables from the base class?
	dog b("dog",60,120);//Here how do I assign these values to the variables from the base class?
		animal *obj[2]={&a,&b};
	
		system("COLOR 1F");
		

	return 0;
}
If you add constructors for the derived classes you can call the base class constructor directly. Would the name (type) for a specific animal always be the same? You could hard code that into the constructor instead of having to pass it.
For example, a constructor for the lion class could look like this:
lion(int b=0, int c=0): animal("lion",b,c){}
Your line 38 in the main() would then look like this:
lion a(45,300);// 'type' will be assigned the value "lion" automatically
You call the animal constructor from within the lion and dog constructors

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
#include <iostream>
#include <string>
using namespace std;

class animal{
public:
	virtual void name(){cout<<"This animal is a : "<<type;};
	virtual void phy(){cout<<"The animal's height and weight are as follows: "<<endl<<"Hieght: "<<height<<"\t"<<"Weight: "<<weight<<endl;}
	virtual void config(){cout<<"Enter a new height (using standard units): "; cin>>height; cout<<"\nEnter a new weight (using standard units): ";cin>>weight; cout<<"Here are the new height and weight values:"<<endl<<height<<weight<<endl;}
	virtual void convert(){ metric_height = height * 2.8;  metric_weight = weight / 2.2; }
	animal(string a="NULL", int b=0, int c=0){type=a; height=b; metric_height=b; weight=c; metric_weight=c;}

	string type;
	int height,weight,metric_height,metric_weight;
};

class lion: public animal{
	public:
	 void name(){cout<<"This animal is a : "<<type;};
	 void phy(){cout<<"The animal's height and weight are as follows: "<<endl<<"Hieght: "<<height<<"\t"<<"Weight: "<<weight<<endl;}
	 void config(){cout<<"Enter a new height (using standard units): "; cin>>height; cout<<"\nEnter a new weight (using standard units): ";cin>>weight; cout<<"Here are the new height and weight values:"<<endl<<height<<weight<<endl;}
	 void convert(){ metric_height = height * 2.8;  metric_weight = weight / 2.2; }
	 lion(string inType, int firstInt, int secondInt):animal(inType, firstInt, secondInt){};

};

class dog: public animal{
	public:
	 void name(){cout<<"This animal is a : "<<type;};
	 void phy(){cout<<"The animal's height and weight are as follows: "<<endl<<"Hieght: "<<height<<"\t"<<"Weight: "<<weight<<endl;}
	 void config(){cout<<"Enter a new height (using standard units): "; cin>>height; cout<<"\nEnter a new weight (using standard units): ";cin>>weight; cout<<"Here are the new height and weight values:"<<endl<<height<<weight<<endl;}
	 void convert(){ metric_height = height * 2.8;  metric_weight = weight / 2.2; }
	 dog(string inType, int firstInt, int secondInt): animal(inType, firstInt, secondInt){};
};


int main(){

lion a("lion",45,300);
	dog b("dog",60,120);
	animal *obj[2]={&a,&b};
	
		system("COLOR 1F");
		
	return 0;
}
ohhh thank you!!!
Topic archived. No new replies allowed.