面向对象——继承、多态、方法重写、构造方法重载简单例子

说明:

这学期开了面向对象的课程,老师上星期布置了作业,之前用JAVA写了一遍,今天心血来潮又用C++写了一遍。博主只会敲代码,但面向对象是小白一个,欢迎交流学习。

题目:

C++代码:

cpp 复制代码
#include <iostream>
#include <algorithm>
#include <string>
constexpr int MAX_LEN = 3;
class Animal{
private:
    int age;
    std::string color;
    std::string animalType = "未知种类的动物";
public:
    Animal() {

    }
    Animal(int age, std::string color){//重载构造函数
        this->age = age;
        this->color = color;
    }
    int getAge() const {
        return age;
    }
    void setAge(int age) {
        Animal::age = age;
    }
    const std::string &getColor() const {
        return color;
    }
    void setColor(const std::string &color) {
        Animal::color = color;
    }
    const std::string &getAnimalType() const {
        return animalType;
    }
    void setAnimalType(const std::string &animalType) {
        Animal::animalType = animalType;
    }
    virtual void eat(std::string something){
        std::cout << getAnimalType() << "吃" << something << '\n';
    }
};
class Cat: public Animal{
private:

public:
    Cat():Animal() {

    }
    Cat(int age, std::string color):Animal(age, color) {
        setAnimalType("猫");
    }
    void catchMouse() {
        std::cout << "猫抓老鼠" << '\n';
    }
    void eat(std::string something) override {
        std::cout << getAge() << "岁" << getColor() << "的猫眯着眼睛侧着头吃";
        std::cout << something << '\n';
    }
};
class Dog: public Animal{
private:

public:
    Dog():Animal() {

    }
    Dog(int age, std::string color):Animal(age, color) {
        setAnimalType("狗");
    }
    void lookHome() {
        std::cout << "狗看家" << std::endl;
    }
    void eat(std::string something) override {
        std::cout << getAge() << "岁的" << getColor() << "的狗两只前腿死死地抱住";
        std::cout << something << "猛吃\n";
    }
};
class Person{
private:
    std::string name;
    int age;
public:
    Person() {

    }
    Person(std::string name, int age) {
        this->name = name;
        this->age = age;
    }
    const std::string &getName() const {
        return name;
    }
    void setName(const std::string &name) {
        Person::name = name;
    }
    int getAge() const {
        return age;
    }
    void setAge(int age) {
        Person::age = age;
    }
    void keepPet(Animal &animal,std::string something) {
        std::cout << "年龄为" << getAge() << "岁的" << getName() << "养了一只";
        std::cout << animal.getColor() << "的" << animal.getAge() << "岁的";
        std::cout << animal.getAnimalType() << '\n';
        animal.eat(something);
    }
};
int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);
    Animal *animals[MAX_LEN];
    animals[0] = new Dog(2, "黑颜色");
    animals[1] = new Cat(3, "灰颜色");
    animals[2] = new Animal(4, "白颜色");

    Person *persons[MAX_LEN];
    persons[0] = new Person("老王", 30);
    persons[1] = new Person("老李", 25);
    persons[2] = new Person("老张", 40);

    persons[0]->keepPet(*animals[0], "骨头");
    persons[1]->keepPet(*animals[1], "鱼");
    persons[2]->keepPet(*animals[2], "肉");
    return 0;
}

输出:

相关推荐
ForteScarlet8 分钟前
Kotlin 2.2.20 现已发布!下个版本的特性抢先看!
android·开发语言·kotlin·jetbrains
anlogic16 分钟前
Java基础 9.10
java·开发语言·算法
yongche_shi21 分钟前
第二篇:Python“装包”与“拆包”的艺术:可迭代对象、迭代器、生成器
开发语言·python·面试·面试宝典·生成器·拆包·装包
Elastic 中国社区官方博客44 分钟前
介绍 Python Elasticsearch Client 的 ES|QL 查询构建器
大数据·开发语言·数据库·python·elasticsearch·搜索引擎·全文检索
Hóng xīng qiáo1 小时前
swVBA自学笔记014、Lisp适合对SolidWorks进行二次开发吗 ?
开发语言·笔记·lisp
一拳一个呆瓜1 小时前
【MFC】对话框:位置属性(居中、绝对对齐、X位置Y位置)应用示例
c++·mfc
m0_552200821 小时前
《UE5_C++多人TPS完整教程》学习笔记48 ——《P49 瞄准偏移(Aim Offset)》
c++·游戏·ue5
带鱼吃猫1 小时前
C++的诗行:一文读懂C++的继承机制
开发语言·c++·学习·visual studio
John_ToDebug1 小时前
Chrome 核心事件循环揭秘:TaskSequenceManager 与 MessagePump 的设计与实现
c++·chrome
好多171 小时前
《Java中的IO流》
java·开发语言·php