C++笔记(十八)类继承

//learnc180.h
#ifndef LEARNC_180
#define LEARNC_180
#include<iostream>
#include<string>
using std::string;

class Animal 
{
private://私有变量在派生类中不能直接访问
    string name;
protected://保护成员在派生类中可以直接访问
    int legs;

public:
    Animal();
    Animal(string namep,int legsp);
    string getName();
    bool canWalk();
    bool canFly();//非虚函数不能实现多态
    virtual void eat();//virtual 使用虚函数来实现多态。
    friend std::ostream& operator<<(std::ostream &os, Animal &ani);
};


class Fish : public Animal //共有派生
{
private:
    int tail;
public:
    Fish();
    Fish(string namesp,int legsp,int tailp);
    bool canSwim();
    bool canWalk();
    virtual void eat();
    friend std::ostream& operator<<(std::ostream &os, Fish &ani);
};


class ABC//抽象类,
{
public:
    virtual void abcFunction() = 0;//包含纯虚函数的类为抽象类,纯虚函数只有声明,没有定义
};


class ABCC:public ABC
{
public:
    virtual void abcFunction();
};


#endif // !LEARNC_180
//learnc181.cpp
#include "learnc180.h"

Animal::Animal() {
    name = ""; //默认没有名字
    legs = 2;//默认两条腿
}

Animal::Animal(string namep, int legsp) {
    name = namep;
    legs = legsp;
}

bool Animal::canWalk() {
    return true;
}

bool Animal::canFly() {
    return false;
}

void Animal::eat() {
    std::cout << "eat noting" << std::endl;
}

string Animal::getName() {
    return name;
}

std::ostream& operator<<(std::ostream &os, Animal &ani) {
    os << "name:" << ani.name << " legs:" << ani.legs << " fly:"<< ani.canFly() << " walk:" << ani.canWalk();
    return os;
}

Fish::Fish() : Animal() {
    tail = 1;
}

Fish::Fish(string namesp, int legsp, int tailp) : Animal(namesp, legsp) {
    tail = tailp;
}

bool Fish::canSwim() {
    return true;
}

bool Fish::canWalk() {
    return false;
}
void Fish::eat() {
    std::cout << "eat water" << std::endl;
}

std::ostream& operator<<(std::ostream &os, Fish &fi) {
    os << "name:" << fi.getName() << " legs:" << fi.legs << " tail:" << fi.tail <<  " fly:" << fi.canFly() << " walk:" << fi.canWalk() << " swim:" << fi.canSwim();
    return os;
}

void ABCC::abcFunction() {
    std::cout << "abcFunction" << std::endl;
}
//learnc182.cpp

#include<iostream>
#include "learnc180.h"

int main() {

    using std::cout;
    using std::endl;

    Animal animal("aaa", 2);
    cout << "animal={" << animal << "}" << endl;
    animal.eat();

    Fish fish("bbb", 0,1);
    cout << "fish={" << fish << "}" << endl;
    fish.eat();


    Animal &animalFish = fish;
    cout << animalFish << endl;
    animalFish.eat();

    //ABC abc; 不能创建抽象类的对象
    ABCC abcc;
    abcc.abcFunction();

    ABC &abc = abcc;
    abc.abcFunction();
    return 0;
}

输出结果:

animal={name:aaa legs:2 fly:0 walk:1}
eat noting
fish={name:bbb legs:0 tail:1 fly:0 walk:0 swim:1}
eat water
name:bbb legs:0 fly:0 walk:1
eat water
abcFunction
abcFunction
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值