[Inheritance]Animal & Human

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值