c++ 单继承

本文深入探讨了C++中的单继承、权限、函数覆盖、显示调用父类成员方法、父类构造和析构顺序、赋值兼容规则、赋值兼容副作用解决方法、纯虚函数和抽象类等关键概念,全面展示了继承与多态的基本原理与应用。

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

01_单继承语法.cpp

#include<iostream>
using namespace std;
//继承就是使得一个类可以拥有另一个类的成员数据和方法的一种快速语法形式
//语法:
//class/struct [子类名]:[父类名]
//             [派生类]:[基类]

struct Father{
    int dollor=1000000;
    Father(){}
    Father(int d):dollor(d){}
    ~Father(){}
    void show_me_the_money()
    {
        cout<<"dollor="<<dollor<<endl;
    }
    int get_dollor(){return dollor;}
};

struct Son: Father{

};

int main()
{
    Son s;
    s.show_me_the_money();
}

02_继承的权限.cpp

#include<iostream>
using namespace std;
//继承就是使得一个类可以拥有另一个类的成员数据和方法的一种快速语法形式
//语法:
//class/struct [子类名]:[父类名]
//             [派生类]:[基类]

//当父类原有的权限和继承的权限共存时,其成员的权限取其中收缩更紧的一个使用(约束力更强的)
class Father{
//  public:
    private:
//  protected:
    int dollor=1000000;
    public:
    Father(){}
    Father(int d):dollor(d){}
    ~Father(){}
    void show_me_the_money()
    {
        cout<<"dollor="<<dollor<<endl;
    }
};

class Son: 
//  public 
    private
    Father{
    public:
    Son(){show_me_the_money();}
    int get_dollor(){return dollor;}
};

int main()
{
    Son s;
//  s.show_me_the_money();
    cout<<s.get_dollor()<<endl;
//  s.dollor = 2000000;
}

03_函数覆盖.cpp

#include<iostream>
//函数覆盖是指子类对父类的成员函数的执行体进行重新定义;


using namespace std;
class Father{
    int dollor=1000000;
    public:
    Father(){}
    Father(int d):dollor(d){}
    ~Father(){}
    void show_me_the_money()
    {
        cout<<"dollor="<<dollor<<endl;
    }
    int get_dollor(){return dollor;}
};

class Son: public   Father{
    int RMB = 2000000;
    public:
    Son(){}
    //override
    void show_me_the_money()
    {
        cout<<"dollor="<<get_dollor()<<endl;
        cout<<"RMB="<<RMB<<endl;
    }

};

int main()
{
    Son s;
    s.show_me_the_money();
//  cout<<s.get_dollor()<<endl;
//  s.dollor = 2000000;
}

04_显示调用父类成员方法.cpp

#include<iostream>
//函数覆盖是指子类对父类的成员函数的执行体进行重新定义;

using namespace std;
class Father{
    int dollor=1000000;
    public:
    //Father(){}
    Father(int d):dollor(d){}
    ~Father(){cout<<"father Destructor"<<endl;}
    void show_me_the_money()
    {
        cout<<"dollor="<<dollor<<endl;
    }
};

class Son: public   Father{
    int RMB = 2000000;
    public:
    Son(int d):Father(d)
    {
    //err:  dollor =d;
    }
//  ~Son(){Father::~Father();}
    //override
    void show_me_the_money()
    {
        Father::show_me_the_money();
        cout<<"RMB="<<RMB<<endl;
    }

};

int main()
{
    Son s(5000000);
    s.show_me_the_money();
//  cout<<s.get_dollor()<<endl;
//  s.dollor = 2000000;
    Father f(1000000);
    f.~Father();
}

05_父类构造和析构顺序.cpp

#include<iostream>
using namespace std;

//父类先构造,子类先析构
struct GrandFather{
    int pound=2000000;
    GrandFather(){
        cout<<"grandfather construction"<<endl;}
    GrandFather(int p):pound(p){}
    ~GrandFather(){
        cout<<"grandfather destrucor"<<endl;}

};
struct Father: GrandFather{
    int dollor=1000000;
    Father(){
        cout<<"father construction"<<endl;}
    Father(int d):dollor(d){}
    ~Father(){
        cout<<"father destrucor"<<endl;}
};

struct Son: Father{
    Son(){
        cout<<"Son construction"<<endl;}
    ~Son(){
        cout<<"Son destructor"<<endl;}
};

int main()
{
}

06_赋值兼容规则.cpp

#include<iostream>
//赋值兼容规则:
//1,指向子类的指针不能指向父类对象;
//2,指向父类的指针可以指向子类对象;
//3,父类的对象不能给子类对象赋值;
//4,子类的对象可以给父类对象赋值;


using namespace std;
class Father{
    int dollor=1000000;
    public:
    Father(){}
    Father(int d):dollor(d){}
    ~Father(){}
    void show_me_the_money()
    {
        cout<<"dollor="<<dollor<<endl;
    }
    int get_dollor(){return dollor;}
};

class Son: public   Father{
    int RMB = 2000000;
    public:
    Son(){}
    //override
    void show_me_the_money()
    {
        cout<<"dollor="<<get_dollor()<<endl;
        cout<<"RMB="<<RMB<<endl;
    }

};

int main()
{
    Son s;
    Father f;

    Father * pf = &f;
    Son * ps = &s;
    pf->show_me_the_money();

    pf = &s; //Son *
//err:  ps = &f;
//err:  s = f;
    f = s;
}

07_赋值兼容副作用解决方法-虚函数.cpp

#include<iostream>
using namespace std;
//赋值兼容会出现一个副作用,子类覆盖的函数会被隐藏,而导致逻辑结果不正确,解决该副作用的方法是采用虚函数
//虚函数是晚绑定,是一种动态多态的实现方法
//虚函数用法是在其基类被以后子类覆盖的函数前,加上关键字virtual

//父类先构造,子类先析构
struct GrandFather{
    int pound=2000000;
    GrandFather(){
//      cout<<"grandfather construction"<<endl;
    }
    GrandFather(int p):pound(p){}
    ~GrandFather(){
//      cout<<"grandfather destrucor"<<endl;
    }
    virtual void speak(){
        cout<<"I am grandfather"<<endl;
    }
};
struct Father: GrandFather{
    int dollor=1000000;
    Father(){
//      cout<<"father construction"<<endl;
    }
    Father(int d):dollor(d){}
    ~Father(){
//      cout<<"father destrucor"<<endl;
    }
    void speak(){
        cout<<"I am father"<<endl;
    }
};

struct Son: Father{
    Son(){
//      cout<<"Son construction"<<endl;
    }
    ~Son(){
//      cout<<"Son destructor"<<endl;
    }
    void speak(){
        cout<<"I am son"<<endl;
    }
};

void show(GrandFather * pgf)
{
    pgf->speak();
}

int main()
{
    GrandFather gf;
    Father f;
    Son s;
    GrandFather *pgf= &gf;
//  pgf = &f;
//  pgf->speak();
//  f.speak();
//  s.speak();
    show(&gf);
    show(&f);
    show(&s);

}

08_纯虚函数和抽象类.cpp

#include<iostream>

using namespace std;

//拥有纯虚函数的类,被称作抽象类;
//抽象类只能用来被继承,而不能定义对象
class Animal{
    public:
    virtual void cry()=0;//纯虚函数
};
class Dog: public Animal{
    public:
    void cry(){cout<<"wang wang"<<endl;}
};
class Cat:public Animal{
    public:
    void cry(){cout<<"miao miao"<<endl;}

};

int main()
{
//err:  Animal a;
    Dog d;
    Cat c;
    d.cry();
    c.cry();

}
实验题目1:班级学生学期成绩管理系统 (1)程序功能简介 灵活运用类的继承、对象成员等机制,设计一个能够实现班级学生学期成绩管理的程序。 (2)程序设计说明 ① 个人信息类CPerson的数据成员有姓名、性别、年龄、身份证号等数据成员,成员函数根据需要自行设计; ② 学生类CStudent从CPerson派生,并增加学号、CCourse对象成员数组(大小至少3个)等数据成员,并根据需要自行设计成员函数,包括能够求解所选修课程的总学分、显示某个学生基本信息和课程信息的成员函数; ③ 课程类CCourse包括课程名、学分、分数、任课老师等数据成员,成员函数根据需要自行设计; ④ 班级类CClass的数据成员有班级名称、班级人数、CStudent对象成员数组(大小由构造函数确定)等。本班级类CClass的对象成员数组需要在构造函数中用new动态分配内存空间,在析构函数中用delete释放。在CClass类中设计包括能够求解最高成绩、最低成绩和平均成绩以及通过学号查找并输出某个学生全部信息(例如Seek())等功能在内的成员函数; ⑤ 构造三十个学生的数据,每个学生都有三门课程成绩,输出并显示这些数据; ⑥ 根据类的需要添加适当的其它成员,编写完整的程序并测试。 (3)程序调试运行 运行程序查看结果,并进行源代码调试和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值