小白从零开始学c++之继承对象的内存空间

构造函数调用顺序,先调用父类,再调用子类

cpp 复制代码
#include<iostream>

using namespace std;

//基类   父类
class Base{
public: //公有权限   类的外部  类的内部 
        Base(){cout<<"Base()"<<endl;}
        Base(int a):baseA(a)
        {
            cout<<"Base(int a)"<<endl;
        }

        ~Base(){cout<<"~Base()"<<endl;}

        void setData(int a){
            baseA = a;
        }
        void show(){
            cout<<"baseA:"<<baseA<<"\t addr:"<<&baseA<<endl;
        }
protected: //保护权限  类的内部  子类
        
private: //私有权限  类的内部
        int baseA;
};

//子类  派生类
class Child: public Base
{
public: //公有权限   类的外部  类的内部 
        //在参数列表中指定 父类的构造函数
        Child():Base(100)
        {
            cout<<"Child()"<<endl;
        }
        Child(int a,int b):Base(a),childA(b)
        {
            cout<<"Child(int a)"<<endl;
        }

        ~Child(){cout<<"~Child()"<<endl;}

        void show(){
            Base::show();
            cout<<"childA:"<<childA<<"\t addr:"<<&childA<<endl;
        }
protected: //保护权限  类的内部

private: //私有权限  类的内部
        int childA;   
         
};


int main()
{
    Child mya(100,200);

    cout<<"mya size:"<<sizeof(mya)<<"\t addr:"<<&mya<<endl;

    mya.show();
}

打印结果:

cpp 复制代码
gec@ubuntu:/mnt/hgfs/GZ22229/14C++/05/1-code$ ./a.out 
Base(int a)
Child(int a)
mya size:8     addr:0x7ffed6dc6630
baseA:100     addr:0x7ffed6dc6630
childA:200     addr:0x7ffed6dc6634
~Child()
~Base()

注意:

1、子类调用成员函数的时候会检测该成员函数在子类中是否存在,如果存在就调用自己的, 如果不存在就调用 父类的(前提是父类要有这个函数)

2、如果子类和父类存在同名函数,那么在子类中 父类的函数成员会被隐藏,默认调用的就是子类函数成员。如果要调用父类的函数成员必须添加类名和作用域。

mya.Base::showData();

练习1:

设计一个基类动物类(属性:体重,颜色,年龄 行为:跑,吃,睡),构造方法初始化属性

设计一个猫类继承动物类(属性:体重,颜色,年龄,品种 行为:跑吃睡,抓老鼠,叫)

定义一个猫对象--咖菲猫,调用猫的喊叫行为,抓老鼠行为 ,输出猫的属性

练习2:编写一个输出学生和教师数据的程序,学生数据有编号、姓名、年龄、学号和成绩;教师数据有编号、姓名、年龄、职称和部门。要求声明一个 person 类,并作为学生数据操作类 student 和教师数据操作类 teacher 的基类

cpp 复制代码
#include <iostream>
#include <cstring>
//将命名空间打开
/*
设计一个基类 动物类(属性:体重、颜色、年龄 、性别    行为:吃喝拉撒)
设计一个猫类 继承动物类(属性:体重、颜色、年龄 、性别、    行为:吃喝拉撒、抓老鼠)
定义一个猫对象 ---断尾猫,调用猫的行为*/
using namespace std;
//动物类
class zoon{
public:   //公有权限
    zoon(int weight,int age, const char * color){
        this->weight = weight;
        this->age = age;
        strcpy(this->color,color);
    }
    void show(){
        cout << "weight:" << weight <<endl;
        cout << "age:"   << age <<endl;
        cout << "color:"  << color <<endl;
    }
protected:
    //私有属性
    int weight;    //体重
    int age;         //年龄
    char color[256];  //颜色
};
//猫类
class cat:public zoon
{
public:   //公有权限
    cat(int weight,int age,const char * color,const char *type):zoon(weight,age,color) {
        strcpy(this->type,type);
    }
    void show(){
        cout << "weight:" << weight <<endl;
        cout << "age:"   << age <<endl;
        cout << "color:"  << color <<endl;
        cout << "type:"  << type <<endl;
    }
    void behavior(){
        cout << "behavior" <<endl; 
    }
private:
    //私有
    char type[256];
    // cat Broken_tail(10,6,"white","BrokenTail");
};
int main()
{
    cat mya(10,6,"white","BrokenTail");
    mya.show();
    mya.behavior();  //行为
    
    
    return 0;
}
相关推荐
奇妙方程式4 分钟前
2026年第九届GXCPC广西大学生程序设计大赛(热身赛)题解
c++·编程比赛·编程竞赛·gxcpc
MartinYeung527 分钟前
[论文学习]DP2Unlearning:高效且具保证的大型语言模型遗忘框架(基于差分隐私的 LLM Unlearning 方法)
学习·算法·语言模型
Tian_Hang40 分钟前
C++原型模式(Protype)
开发语言·c++·算法
bIo7lyA8v40 分钟前
算法复杂度的渐进分析与实际运行时间的差异的技术8
算法
天天讯通1 小时前
OKCC 呼叫中心安全性能全解析:技术防护与管理措施指南
大数据·开发语言·网络·人工智能·安全·语音识别
xufengzhu1 小时前
第三方 Python 库 redis-py + hiredis 的使用
开发语言·redis·python
jingling5551 小时前
go | 环境安装和快速入门
开发语言·后端·golang
yuan199972 小时前
欧拉梁静力与屈曲计算的 MATLAB 实现(有限差分法 + 解析解)
开发语言·算法·matlab
llxxyy卢2 小时前
polar夏季赛部分题目
开发语言·python
AI玫瑰助手2 小时前
Python模块:from...import...导入指定内容
开发语言·python·信息可视化