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
|
// Subject: Inheritance exercise.
//
//***********************************************************************
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
class Pet
{
private:
char name[20];
int age;
double weight;
public:
Pet();// default constructor
Pet(char[20], int, double); // argument constructor
char getName()const; // read the name
int getAge()const; // read the age
double getWeight()const; // read weight
void setName(char[20]);// set the name
void setAge(int); // set the age
void setWeight(double); // set the weight
string getLifespan();// return a string
};
///////////////////////////////////////////////////////////////////////////////
// Class Derived Dog
class Dog: public Pet
{
private:
char breed[30];
public:
Dog();//:Pet(){} // default constroctur
Dog(char n[20], int a, double w, char b[30]):Pet(n, a, w){}// argument constructor
char getBreed()const; // read the bread type
void setBreed(char[30]); // set the breed of the dog
string getLifespan(); // return a string
};
///////////////////////////////////////////////////////////////////////////////
// Class Derived Rock
class Rock : public Pet
{
public:
Rock():Pet(){}
Rock(char n[20],int a, double w): Pet(n, a, w){}
string getLifespan()// return string
{
string s;
s = "Thousands of years";
return s;
}
};
///////////////////////////////////////////////////////////////////////////////
// The Class Pet declerations
Pet::Pet()
{
name[0] = '\0';
age = 0;
weight = 0.0;
}
Pet::Pet (char n[20], int a, double w)
{
name[20] = n[20];
age = a;
weight = w;
}
char Pet::getName()const
{
return name[20];
}
int Pet::getAge()const
{
return age;
}
double Pet::getWeight()const
{
return weight;
}
void Pet::setName(char n[20])
{
name[20] = n[20];
}
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()
{
Pet();
breed[0] ='\0' ;
}
char Dog::getBreed() const
{
return breed[30];
}
void Dog::setBreed(char b[30])
{
breed[30] = b[30];
}
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()
{
Dog d; // object derived Dog
Rock r;// object derived Rock
string s; // to get the string from getLifespan() and store it.
char n[20] = "Sparky";// used on the base class Pet for name
int a = 1;// used on the base class Pet for age
double w = 17.20;// used on the base class Pet for weight
char b[30] = "Bichon-Puddle";// used on the derived class Dog for breed
cout <<"The Derived Dog output: " << endl;
d.setName(n); // set the name at the class Pet
d.setAge(a); // set the age at class Pet
d.setWeight(w);// set the weight at class Pet
d.setBreed(b); // set the breed at derived class Dog
cout <<"Name : " << d.getName(); cout << endl;
cout <<"Age : " << d.getAge(); cout << endl;
cout <<"Weight: " << d.getWeight();cout << endl;
cout <<"Breed : " << d.getBreed(); cout << endl;
cout << endl;
cout << endl;
cout <<"Derived Dog getLifespan() = ";
s = d.getLifespan();
cout << s << endl;
cout <<"\n\nThe Derived Rock output: " << endl;
r.setName(n);
r.setAge(a);
r.setWeight(w);
cout << endl;
cout <<"Name : " << r.getName(); cout << endl;
cout <<"Age : " << r.getAge(); cout << endl;
cout <<"Weight: " << r.getWeight();cout << endl;
cout << endl;
cout << endl;
cout <<"Derived Rock getLifespan() = ";
s = r.getLifespan();
cout << s << endl << endl;
return 0;
}
|