C++nbsp;继承

2024-06-19 17:58
文章标签 c++ 继承 nbsp

本文主要是介绍C++nbsp;继承,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

注:参考东南大学《C++程序设计教程》视频,主讲老师:何洁月。此内容为自学时所记笔记
第七章   类的继承与派生
继承:保持已有类的特性而构成新类的过程  目的:实现代码重用
派生:在已有类的基础上新增自己的特性而产生新类的过程  目的:原程序进行改造吗
      (1)吸收基类 。(2)改造 同名覆盖。(3)新增加。
基类--->派生类
公有继承:class B{};
          class A:public B{};
          派生类的“成员函数”可以访问基类public ,protected。
          派生类的“对象”只能访问基类的public。

例:
#include
using namespace std;
class Point
{
public :
  //  Point (float x,float y){X=x;Y=y;}
      void InitP(float x,float y){X=x;Y=y;}
      float getX(){return X;}
      float getY(){ return Y;}
      void Print();
      void Move(float x,float y);
private :
      float X,Y;
};
void Point::Move(float x,float y){
      X+=x;
      Y+=Y;
}
void Point::Print(){
          cout<<getX()<<" "<<getY()<<endl;
}
class Rectangle:public Point
{
public :
      void InitR(float x,float y,float w,float h){
              InitP(x,y);
              W=w;
              H=h;
      }
      float getW(){return W;}
      float getH(){return H;}
      //void Print();
private:
        float W,H;
};
int main()
{
        Rectangle r;
        r.InitR(1,1,1,1);
        r.Move(1,1);
        r.Print();
        return 0;
}

私有继承:
    class B{};
    class A:private B{};
    public 和protected 成员以private 出现在派生类中,
    派生类的成员函数不能访问基类的private成员。
    派生类的对象不能访问任何成员,想要访问,必须重新构造方法

  class Rectangle:private Point
{
public :
      void InitR(float x,float y,float w,float h){
              InitP(x,y);
              W=w;
              H=h;
      }
      void Move(float x,float y);    //重新构造
      void Print();                                //重新构造
      float getX(){return Point::getX();}
      float getW(){return W;}
      float getH(){return H;}
      //void Point::Print();
private:
        float W,H;
};
void Rectangle::Move(float x,float y){
    Point::Move(x,y);
}
void Rectangle::Print(){
      Point::Print();
      cout<<W<<"--"<<H<<endl;
}

保护继承(protected)
     class B{};
     class A :protected B{};
     派生类的成员可以访问基类的public,protected
     派生类的对象不能访问基类的所有成员。

     特点与作用:派生类外不能访问基类的protected成员 ,
                 派生类类内可以访问基类的......。

例:
class A
{
public :
      void setA(int aa){a=aa;}
      int getA(){return a;}
protected :
      int a;
};
class B:protected A
{
public :
      int getB(){return b;}
      void changeA(){a=a*2;}    //派生类内可以对基类protected成员操作
      int getA(){A::getA();}
      void setA(int x){A::setA(x);}
private:
            int b;
};
#include
using namespace std;
int main()
{
      B b;
      b.setA(10);
      b.changeA();
      cout<<b.getA()<<endl;
}
总结:三种继承,派生类的成员访问是一样的,派生类的对象访问
      不一样,
      派生类中的成员属性,public->protected->private 变换
              public      protected   private
:public       public      protected   private
:protected    protected   protected   private
:private      private     private     private

protected 对于派生类的对象不能访问,但是派生类内可以访问
一般继承的基类,属性都会protected而不是private

多继承:
      class A{};
      class B{};
      class C:public A,private B{};
继承时的构造函数
      基类的构造函数不被继承。
      class A{a};
      A::A(int aa){a=aa;}
      class B{b};
      B::B(int aa,int bb):A(a){b=bb;}
类似于内嵌对象时的构造函数。

  #include
using namespace std;
class A
{
    public :
          A();
          A(int aa);
          ~A();
          int getA(){return a;}
          void Print();
      private:
          int a;
};
A::A(int aa){a=aa;}
A::~A(){cout<<"a xigou"<<endl;}
void A::Print() {cout<<a<<"--"<<endl;}
class B:public A
{
      public :
            B(int aa,int bb);
            ~B();
            void setB(int bb){b=bb;}
            int getB(){return b;}
            void Print();
      private:
            int b;
};
B::B(int aa,int bb):A(aa){    //B的构造函数
    b=bb;
}
B::~B(){cout<<"b xigou"<<endl;}
void B::Print() {A::Print();cout<<b<<"--"<<endl;}
int main()
{
      B b(1,2);
      cout<<b.getA()<<" "<<b.getB()<<endl;
      b.Print();
      return 0;
}
多继承的构造函数
class A{a};
      A::A(int aa){a=aa;}
      class B{b};
      B::B(int aa,int bb,int cc):A(a),C(c){b=bb;}

构造函数的调用次序
   (1)基类:按照他们被继承时的顺序
    (2)成员对象:按照他们在类中声明的顺序
     (3)构造函数体内的内容。

#include
using namespace std;
class C
{
      public :
            C(int cc){c=cc;}
            void setC(int cc){c=cc;}
            int getC(){return c;}
      private:
            int c;
};
class A
{
    public :
          A();
          A(int aa);
          ~A();
          int getA(){return a;}
          void Print();
      private:
          int a;
};
A::A(int aa){a=aa;}
A::~A(){cout<<"a xigou"<<endl;}
void A::Print() {cout<<a<<"--"<<endl;}
class B:public A
{
      public :
            B(int aa,int bb,int cc);
            ~B();
            void setB(int bb){b=bb;}
            int getB(){return b;}
            void Print();
      private:
            int b;
            C c;
};
B::B(int aa,int bb,int cc):A(aa),c(cc){    //B的构造函数
                                                    //先继承,后对象成员,后自己
    b=bb;
}
B::~B(){cout<<"b xigou"<<endl;}
void B::Print() {
      A::Print();cout<<b<<"--"<<endl;
      cout<<c.getC()<<"--"<<endl;
}
int main()
{
      B b(1,2,3);
      cout<<b.getA()<<" "<<b.getB()<<endl;
      b.Print();
      return 0;
}

析构函数 也需派生类自行声明。
         析构顺序与构造函数顺序恰好相反。可以看成栈的使用。

同名覆盖:派生类与基类有相同成员
           class A{fun()};
           class B:public A{fun()};
           b.fun();
           b.A::fun();  //调用基类的成员,

                 
#include
using namespace std;
class A
{
public :
              A(int aa){a=aa;}
              void fun(){cout<<a<<endl;}
private:
              int a;
};
class B :public A
{
public :
            B(int bb,int aa):A(aa){b=bb;}
            void fun(){cout<<b<<endl;}
private:
              int b;
};
int main()
{
      B b(1,2);
      b.fun();
      b.A::fun();
      return 0;
}
继承的二义性问题:
     class A{fun();}
     class B{fun();}
     class C:public A,public B  //A,B有相同的成员名
 解决方法(1):类名限定
          (2):同名覆盖

#include
using namespace std;
class A
{
public :
              A(int aa){a=aa;}
              void fun(){cout<<a<<endl;}
              int getA(){return a;}
private:
              int a;
};
class C
{
public :
        C(int cc){c=cc;}
        void fun(){cout<<c<<endl;}
private:
        int c;
};
class B :public A,public C
{
public :
            B(int bb,int aa,int cc):A(aa),C(cc){b=bb;}
            void fun(){cout<<b<<"--"<<getA()<<"---";
                                  C::fun();
                        }
private:
              int b;
};
int main()
{
      B b(1,2,3);
      b.fun();
      b.A::fun();
      b.C::fun();
      return 0;
}

虚基类:只保留基类的一份。
    class B1:virtual public B
作用:解决多继承时可能发生的对同一基类继承多次而产生的二义性问题
      为最远派生类提供唯一的基类成员。不重复产生拷贝。
      B--B1       B-->B2      B1,B2-->C

赋值兼容原则:
    公有派生类对象在使用上可以被当做基类的对象
     赋值,引用,指针。

实际开发实例:
class employee  //基类 员工
{
  protected :
        char *name;
        int id;
        int grade;
        float monthpay;
        static  int employno; //编号最大值
          //静态,所有都可以访问
  public :
          employee();
          ~employee();
          void pay();
          void promote(int );
          void display();
};

class technician:public employee  //技术人员
{
    private :
          float hourlyrate;
          int workHours;
    public :
          technician();
          void pay();
          void display();
};

class salesman:virtual public employee //虚基类,销售人员
{
    protected:
        float comnRate;
        float sales;
      public:
          salesman();
          void pay();
          void display();
};
class manager:virtual public employee//经理
{
    protected:
      float monthlyPay;
    public :
        manager();
        void pay();
        void display();
};
class salesmanager:public salesman,public manager
//销售经理
{
    public :
        salesmanager();
        void pay();
        void display();
};
#include
#include
using namespace std;
int employee::employno=1000;
employee::employee()
{
      char c[50];
      cout<<"输入姓名:"<<endl;
      cin>>c;
      name=new char(strlen(c));
      strcpy(name,c);
      employno++;
      id=employno;
      grade=1;
      monthpay=0.0;
}
employee::~employee()
{
      delete name ;//,一开始定义的指针,申请了空间了,释放姓名空间
}
void employee::pay()
{

}
void employee::promote(int increament)
{
      grade+=increament;
}
void employee::display()
{
      cout<<name<<"您好!"<<" 编号:"<<id<<" 等级:"<<grade<<" 月薪:"<<monthpay<<endl;
}
technician::technician()
{
      hourlyrate=10;
}
void technician::pay()
{
      cout<<"请输入 "<<name<<" 本月工作时:";
      cin>>workHours;
      monthpay=hourlyrate*workHours;
}
void technician::display()
{
      employee::display();
      //cout<<"兼职技术人员"<<name<<"编号"<<id<<
    // "级别:"<<grade<<"工资"<<monthpay<<endl;
}
salesman::salesman()
{
    comnRate=0.04;
}
void salesman::pay()
{
      cout<<"输入"<<name<<"本月销售额";
      cin>>sales;
      monthpay=sales*comnRate;
}
void salesman::display()
{
      employee::display();
    // cout<<"销售员"<<name<<"编号"<<id<<
    // "级别为"<<grade<<"工资为"<<monthpay<<endl;
}
manager::manager()
{
    //系统自动调用基类的无参构造函数
    monthlyPay=8000;
}
void manager::pay()
{
      monthpay=monthlyPay;
      cout<<"经理底薪:"<<monthpay<<endl;
}
void manager::display()
{
      employee::display();
  //cout<<"经理"<<name<<"编号"<<id<<
    // "级别为"<<grade<<"工资为"<<monthpay<<endl;
}
salesmanager::salesmanager()
{
      monthlyPay=5000;
      comnRate=0.005;
}
void salesmanager::pay()
{
      cout<<"请输入"<<name<<"销售额:";
      cin>>sales;
      monthpay=monthlyPay+comnRate*sales;
}
void salesmanager::display()
{
      employee::display();
//  cout<<"销售经理"<<name<<"编号"<<id<<
  //  "级别为"<<grade<<"工资为"<<monthpay<<endl;
}
int main()
{
      manager m1;
      technician t1;
      salesmanager sm1;
      salesman s1;

      m1.promote(3);
      m1.pay();
      m1.display();

      t1.promote(2);
      t1.pay();
      t1.display();

      sm1.promote(1);
      sm1.pay();
      sm1.display();

      s1.pay();
      s1.display();
}

这篇关于C++nbsp;继承的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1075742

相关文章

c++ 类成员变量默认初始值的实现

《c++类成员变量默认初始值的实现》本文主要介绍了c++类成员变量默认初始值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录C++类成员变量初始化c++类的变量的初始化在C++中,如果使用类成员变量时未给定其初始值,那么它将被

C++中NULL与nullptr的区别小结

《C++中NULL与nullptr的区别小结》本文介绍了C++编程中NULL与nullptr的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编... 目录C++98空值——NULLC++11空值——nullptr区别对比示例 C++98空值——NUL

C++ Log4cpp跨平台日志库的使用小结

《C++Log4cpp跨平台日志库的使用小结》Log4cpp是c++类库,本文详细介绍了C++日志库log4cpp的使用方法,及设置日志输出格式和优先级,具有一定的参考价值,感兴趣的可以了解一下... 目录一、介绍1. log4cpp的日志方式2.设置日志输出的格式3. 设置日志的输出优先级二、Window

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

c++中的set容器介绍及操作大全

《c++中的set容器介绍及操作大全》:本文主要介绍c++中的set容器介绍及操作大全,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录​​一、核心特性​​️ ​​二、基本操作​​​​1. 初始化与赋值​​​​2. 增删查操作​​​​3. 遍历方

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决

C++11委托构造函数和继承构造函数的实现

《C++11委托构造函数和继承构造函数的实现》C++引入了委托构造函数和继承构造函数这两个重要的特性,本文主要介绍了C++11委托构造函数和继承构造函数的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录引言一、委托构造函数1.1 委托构造函数的定义与作用1.2 委托构造函数的语法1.3 委托构造函

C++11作用域枚举(Scoped Enums)的实现示例

《C++11作用域枚举(ScopedEnums)的实现示例》枚举类型是一种非常实用的工具,C++11标准引入了作用域枚举,也称为强类型枚举,本文主要介绍了C++11作用域枚举(ScopedEnums... 目录一、引言二、传统枚举类型的局限性2.1 命名空间污染2.2 整型提升问题2.3 类型转换问题三、C