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++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

c++中std::placeholders的使用方法

《c++中std::placeholders的使用方法》std::placeholders是C++标准库中的一个工具,用于在函数对象绑定时创建占位符,本文就来详细的介绍一下,具有一定的参考价值,感兴... 目录1. 基本概念2. 使用场景3. 示例示例 1:部分参数绑定示例 2:参数重排序4. 注意事项5.

使用C++将处理后的信号保存为PNG和TIFF格式

《使用C++将处理后的信号保存为PNG和TIFF格式》在信号处理领域,我们常常需要将处理结果以图像的形式保存下来,方便后续分析和展示,C++提供了多种库来处理图像数据,本文将介绍如何使用stb_ima... 目录1. PNG格式保存使用stb_imagephp_write库1.1 安装和包含库1.2 代码解

C++实现封装的顺序表的操作与实践

《C++实现封装的顺序表的操作与实践》在程序设计中,顺序表是一种常见的线性数据结构,通常用于存储具有固定顺序的元素,与链表不同,顺序表中的元素是连续存储的,因此访问速度较快,但插入和删除操作的效率可能... 目录一、顺序表的基本概念二、顺序表类的设计1. 顺序表类的成员变量2. 构造函数和析构函数三、顺序表

使用C++实现单链表的操作与实践

《使用C++实现单链表的操作与实践》在程序设计中,链表是一种常见的数据结构,特别是在动态数据管理、频繁插入和删除元素的场景中,链表相比于数组,具有更高的灵活性和高效性,尤其是在需要频繁修改数据结构的应... 目录一、单链表的基本概念二、单链表类的设计1. 节点的定义2. 链表的类定义三、单链表的操作实现四、

使用C/C++调用libcurl调试消息的方式

《使用C/C++调用libcurl调试消息的方式》在使用C/C++调用libcurl进行HTTP请求时,有时我们需要查看请求的/应答消息的内容(包括请求头和请求体)以方便调试,libcurl提供了多种... 目录1. libcurl 调试工具简介2. 输出请求消息使用 CURLOPT_VERBOSE使用 C

C++实现获取本机MAC地址与IP地址

《C++实现获取本机MAC地址与IP地址》这篇文章主要为大家详细介绍了C++实现获取本机MAC地址与IP地址的两种方式,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 实际工作中,项目上常常需要获取本机的IP地址和MAC地址,在此使用两种方案获取1.MFC中获取IP和MAC地址获取

C/C++通过IP获取局域网网卡MAC地址

《C/C++通过IP获取局域网网卡MAC地址》这篇文章主要为大家详细介绍了C++如何通过Win32API函数SendARP从IP地址获取局域网内网卡的MAC地址,感兴趣的小伙伴可以跟随小编一起学习一下... C/C++通过IP获取局域网网卡MAC地址通过win32 SendARP获取MAC地址代码#i

C++中使用vector存储并遍历数据的基本步骤

《C++中使用vector存储并遍历数据的基本步骤》C++标准模板库(STL)提供了多种容器类型,包括顺序容器、关联容器、无序关联容器和容器适配器,每种容器都有其特定的用途和特性,:本文主要介绍C... 目录(1)容器及简要描述‌php顺序容器‌‌关联容器‌‌无序关联容器‌(基于哈希表):‌容器适配器‌:(