运算符重载类的继承

该代码示例展示了C++中如何定义一个名为Person的类,包括其构造函数、析构函数以及成员变量。Person类有一个子类Stu,具有额外的成员变量。代码创建了一个Stu对象,并调用了显示、更新成员函数以及父类的成员函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述
work.cpp

#include <iostream>

using namespace std;
class person{
protected:
    string name;
private:
    int age;
    int high;
    int weight;
public:
    person(){}
    person(int age,int high,string name,int weight):age(age),high(high),weight(weight),name(name){}
    void show(){
        cout<<"person:name="<<name<<"age="<<name<<"high="<<high<<"weight="<<weight<<endl;
    }
    ~person(){
        cout<<"p"<<endl;
    }
};
class stu:public person{
    int class_val;
    int score;
public:
    stu(){}
    stu(int class_val,int score,string name,int age,int high,int weight):class_val(class_val),score(score),person(age,high,name,weight){}
    void show(){
        cout<<this->name<<endl;
    }
    void update(string name){
        this->name=name;
    }
    ~stu(){
        cout<<"s"<<endl;
    }
};
int main()
{
    stu p(1,98,"myname",20,180,150);
    p.person::show();
    p.update("my");
    p.show();
    p.person::show();
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值