C++day6

#include <iostream>

using namespace std;
class Zoo
{
private:
    string name; // 名称
    string y; // y
    int *age; // 年龄
public:
    Zoo(); // 无参构造函数
    Zoo(string name,string y,int age); // 有参构造函数
    Zoo(const Zoo &other); // 拷贝构造函数
    Zoo & operator=(const Zoo &other); // 拷贝赋值运算符
    ~Zoo(); // 析构函数
};
Zoo::Zoo(){cout << "Zoo::无参" << endl;} // 无参构造函数实现
Zoo::Zoo(string name,string y,int age):name(name),y(y),age(new int(age)){cout << "Zoo::有参" << endl;} // 有参构造函数实现
Zoo::Zoo(const Zoo &other):name(other.name),y(other.y),age(new int(*(other.age))){cout << "Zoo::拷贝构造" << endl;} // 拷贝构造函数实现
Zoo & Zoo::operator=(const Zoo &other)
{
    name = other.name; // 名称赋值
    y = other.y; // y赋值
    age = new int(*(other.age)); // 年龄赋值
    return *this; // 返回当前对象
    cout << "Zoo::拷贝赋值" << endl; // 输出提示信息
}
Zoo::~Zoo(){delete age;age = nullptr;cout << "Zoo::析构" << endl;} // 析构函数实现
class Dog:protected Zoo
{
private:
    int *a; // 腿的个数
public:
    Dog(); // 无参构造函数
    Dog(string name,string y,int age,int a); // 有参构造函数
    Dog(const Dog &other); // 拷贝构造函数
    Dog &operator=(const Dog &other); // 拷贝赋值运算符
    void speak(); // 叫声
    ~Dog(); // 析构函数
};
Dog::Dog(){cout << "Dog::无参" << endl;} // 无参构造函数实现
Dog::Dog(string name,string y,int age,int a):Zoo(name,y,age),a(new int(a)){cout << "Dog::有参" << endl;} // 有参构造函数实现
Dog::Dog(const Dog &other):Zoo(other),a(new int(*(other.a))){cout << "Dog::拷贝构造" << endl;} // 拷贝构造函数实现
Dog &Dog::operator=(const Dog &other)
{
    Zoo::operator=(other); // 调用基类的拷贝赋值运算符
    this->a = new int(*(other.a)); // a赋值
    return *this; // 返回当前对象
    cout << "Dog::拷贝赋值" << endl; // 输出提示信息
}
void Dog::speak()
{
    cout << "汪汪汪~~~" << endl;
}
Dog::~Dog(){delete a; a = nullptr;cout << "Dog::析构" << endl;} // 析构函数实现
int main()
{
    Dog z1("张三","黄",8,4); // 创建对象z1
    Dog z2 = z1; // 使用拷贝构造函数创建对象z2
    Dog z3; // 创建对象z3
    z3 = z1; // 使用拷贝赋值运算符赋值
    return 0; // 返回成功
}

#include <iostream>; // 包含输入输出流库

using namespace std; // 使用标准命名空间
class Animal // 定义动物类
{
public: // 公有访问权限
    Animal(); // 构造函数声明
    virtual void perform() = 0; // 纯虚函数声明
    ~Animal(); // 析构函数声明
};
class Cat:protected Animal // 定义猫类,继承自动物类并保护继承
{
private: // 私有访问权限
    string name; // 名称
    string trait; // 特征
public: // 公有访问权限
    Cat(); // 构造函数声明
    Cat(string name,string trait); // 带参构造函数声明
    void perform(); // 执行函数声明
    ~Cat(); // 析构函数声明
};
class Dog:protected Animal // 定义狗类,继承自动物类并保护继承
{
private: // 私有访问权限
    string name; // 名称
    string trait; // 特征
public: // 公有访问权限
    Dog(); // 构造函数声明
    Dog(string name,string trait); // 带参构造函数声明
    void perform(); // 执行函数声明
    ~Dog(); // 析构函数声明
};
Dog::Dog(){} // 狗类无参构造函数实现
Dog::Dog(string name,string trait):name(name),trait(trait){} // 狗类带参构造函数实现
void Dog::perform(){cout << name << " " << trait << endl;} // 狗类执行函数实现
Dog::~Dog(){} // 狗类析构函数实现
Cat::Cat(){} // 猫类无参构造函数实现
Cat::Cat(string name,string trait):name(name),trait(trait){} // 猫类带参构造函数实现
void Cat::perform(){cout << name << " " << trait << endl;} // 猫类执行函数实现
Cat::~Cat(){} // 猫类析构函数实现
Animal::Animal(){} // 动物类无参构造函数实现
Animal::~Animal(){} // 动物类析构函数实现
int main() // 主函数入口
{
    Cat a("喵","火力冲击"); // 创建猫对象a
    a.perform(); // 调用猫对象a的执行函数
    Dog b("汪","水漫金山"); // 创建狗对象b
    b.perform(); // 调用狗对象b的执行函数
    return 0; // 返回成功
}

 

【有道云笔记】C++.mindmap
https://note.youdao.com/s/8zn9E1DE

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值