Description
First, write a base class animal in animal.h, which has 2 attributes: string _species and int _eyes. This class should also have a method: void printeyes(), which can printf “species has _eyes eyes.” on screen.
Then, you are required to write a human class which is an inheritance class of animal. Its _species should always be “Human” and of course we have 2 eyes. The human class has an extra attribute: string _name. And it is able to say hello to us with an extra method, void greeting() : “Hello, I’m _name.”
Hint
选自zion
Author: 彭勃(TA)
main.cpp
#include <iostream>
#include <string>
#include "animal.h"
#include "human.h"
int main() {
std::string s, n;
int e;
std::cin >> s >> e >> n;
animal * p = new animal(s, e);
p->print_eyes();
delete p;
human h(n);
p = &h;
p->print_eyes();
const human & q = h;
q.greeting();
return 0;
}
human.h
#include <iostream>
//#include "animal.h"
using namespace std;
class human : public animal
{
public:
human();
human(string const & n);
void greeting() const;
private:
string name;
};
human::human()
:animal("Human", 2){}
human::human(string const & n)
:animal("Human", 2) ,name(n){}
void human::greeting() const
{
cout << "Hello, I'm " << name << "." << endl;
}
animal.h
#include <iostream>
using namespace std;
class animal
{
public:
animal(string species, int eyes);
void print_eyes();
void setSpecies(string);
void setEyes(int eyes);
private:
string _species;
int _eyes;
};
animal::animal(string species, int eyes)
:_species(species),_eyes(eyes){}
void animal::print_eyes()
{
cout << _species << " has ";
cout << _eyes << " eyes." << endl;
}