栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++(二十)继承中的对象模型,继承中子构造和析构

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++(二十)继承中的对象模型,继承中子构造和析构

复习一下:
1)静态成员函数类中声明,类外定义;
2)static 同时const修饰的成员,在定义的时候必须赋初值,负责会报错;
3)静态成员不属于类的对象上
4)静态成员函数只能访问静态成员(除非参数传递一个对象进来,通过对象访问)

(1)继承中的对象模型:
1)继承会继承父类的成员,(除了静态成员),因为它不属于类的对象上
visual studio 可以打开命令提示符:

进入存放你.cpp的文件的目录然后
输入:cl /d1 reportSingleClassLayout 类名 文件名
可以查看该文件中该类的布局

下面是一个实例:

#include
using namespace std;

class father
{
    friend void test();//使用友元打破封装
public:
    int a;
    father();
protected:
    int b;
private:
    int c; 
    static const int d; 
};
const int father::d=10;//静态成员的定义需要在类外,static修饰的常量需要在定义的时候赋初值
void test()
{
    cout<
public:
    int a;
};
int main()
{
    test();
  cout< 

(2)继承中子构造和析构
1)构造子类对象之前会先构造父类对象,
2) 释放父类对象之前会先释放子类对象

#include
using namespace std;
class father
{
public:
        father();
        ~father();
};
class son:father
{
public:
        son();
        ~son();
};
father::father()
{
    cout<<"this is father_constuctor"<
    cout<<"this is father_distructor"<
    cout<<"this is son_constructor"<
    cout<<"this is son_constructor"<
    son obj;
}
int main()
{
        test();
    
    
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1001085.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号