小白从零开始学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;
}
相关推荐
麻雀无能为力13 分钟前
python自学笔记2 数据类型
开发语言·笔记·python
招风的黑耳26 分钟前
Java集合框架详解与使用场景示例
java·开发语言
xrkhy27 分钟前
java中XML的使用
xml·java·开发语言
抽风的雨61030 分钟前
【python基础知识】Day 27 函数专题2:装饰器
开发语言·python
-一杯为品-36 分钟前
【深度学习】#11 优化算法
人工智能·深度学习·算法
_F_y37 分钟前
list简单模拟实现
c++·list
前进的程序员41 分钟前
C++ 在 Windows 和 Linux 平台上的开发差异及常见问题
linux·c++·windows
-qOVOp-1 小时前
zst-2001 上午题-历年真题 计算机网络(16个内容)
网络·计算机网络·算法
Swift社区1 小时前
涂色不踩雷:如何优雅解决 LeetCode 栅栏涂色问题
算法·leetcode·职场和发展
冠位观测者1 小时前
【Leetcode 每日一题】2900. 最长相邻不相等子序列 I
数据结构·算法·leetcode