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
|
#include <iostream>
#include <string>
#include <cctype>
#include <conio.h>
using namespace std;
const int MAX = 20;
class Pet
{
private:
char* name;
int age;
double weight;
public:
Pet(){ name = 0; age = 0; weight = 0.0;}// default constructor
Pet(const char*, int, double); // argument constructor
Pet(const Pet&);// copy constructor
~Pet();
Pet& operator = (const Pet&);// assignment constructor
double getWeight()const{return weight;} // Accessor
void getInfo() const;// Accessor
void setInfo(); // pure virtual function
virtual void getLifespan()= 0;// pure virtual function
};
///////////////////////////////////////////////////////////////////////////////
// Class Pet declerations
Pet::Pet(const char* n, int a, double w)
{
size_t length = strlen(n)+1;
name = new char[length];
strcpy_s(name, length, n);
age = a;
weight = w;
}
Pet::Pet(const Pet& otherPet)
{
size_t length = strlen( otherPet.name )+1;
name = new char[strlen(otherPet.name)+1];
strcpy_s(name, length, otherPet.name);
age = otherPet.age;
weight = otherPet.weight;
}
Pet::~Pet()
{
delete[] name;
}
Pet& Pet::operator =(const Pet& otherPet)
{
delete[]name;
int length = strlen(otherPet.name)+1;
name = new char[length];
strcpy_s(name, length, otherPet.name);
age = otherPet.age;
weight = otherPet.weight;
return *this;
}
void Pet::setInfo()
{
char ch[20] = "";
int a;
double w;
cout <<"Enter the name: " ; cin >> ch;
delete[] name;
int length = strlen(ch)+1;
name = new char[length];
strcpy_s(name, length, ch);
cout <<"Enter the age: "; cin >> a;
age = a;
cout <<"Enter the weight: "; cin >> w;
weight = w;
}
void Pet::getInfo() const
{
if (name != NULL)
{
cout <<"Pet Name: " << name << endl;
cout <<"Pet Age: " << age << endl;
cout <<"Pet Weight: " << weight << endl;
}
else if (name == NULL)
{
cout <<"Pet Name: N/A" << endl;
cout <<"Pet Age: " << age << endl;
cout <<"Pet Weight: " << weight << endl;
}
}
void Pet::getLifespan()
{
string s = "Uknown lifesapn ";
cout << s << endl;
}
// Class Dog derived from Pet
class Dog : public Pet
{
private:
string breed;
public:
Dog(): Pet(), breed(" "){}
Dog(char* n, int a, double w, string b):Pet(n, a, w), breed(b){}
Dog(const Dog&);
Dog& operator =(const Dog&);
void setBreed();
void getInfo()const;
void getLifespan();
};
///////////////////////////////////////////////////////////////////////////////
// Class Dog derived from Pet declerations
Dog::Dog(const Dog& otherDog): Pet(otherDog)
{
breed = otherDog.breed;
}
Dog& Dog::operator =(const Dog& otherDog)
{
breed = otherDog.breed;
Pet::operator = (otherDog);
return *this;
}
void Dog::setBreed()
{
string b;
cout <<"Enter the breed: "; cin >> b;
breed = b;
}
void Dog::getInfo() const
{
Pet::getInfo();
cout <<"Pet breed: " << breed << endl;
}
void Dog::getLifespan()
{
string s1, s2;
double w;
s1 = "The lifespan is under 7 years.";
s2 = "The lifespan is 13 years.";
w = Pet::getWeight();
if (w > 100)
{
cout << s1 << endl;
}
else if (w < 100)
cout << s2 << endl;
else if (w == 100)
{
Pet::getLifespan();
}
}
// Clas Rock derived from Pet
class Rock : public Pet
{
public:
Rock(): Pet(){}
Rock(char *n, int a, double w): Pet(n, a, w){}
void getLifespan();
};
void Rock::getLifespan()
{
string s = "Thousand of years";
cout << s << endl;
}
int main()
{
Pet* p[10];
// how do I use the copy constructor and assignment constructor????
return 0;
}
|