zoukankan      html  css  js  c++  java
  • C++ 继承与派生

    public_extends.cpp

    #include <iostream>
    using namespace std;
    #include <string.h>

    /********************************************
    一、派生类的定义格式
    class 派生类名:访问权限 基类名1,
                    访问限定符 基类名2,……,
                    访问限定符  基类名n
    {
    private:
        成员表1; //派生类增加或替代的私有成员
    public:
        成员表2; //派生类增加或替代的公有成员
    protected:
        成员表3; //派生类增加或替代的保护成员
    };
    ********************************************/

    /*--------------------------------------------
    共有继承(public):

      继承特点:
        1、基类公有成员相当于派生类中的共有成员
        2、基类保护成员相当于派生类中的保护成员
        3、基类私有成员在派生类中无法直接访问

    调用基类的成员:
        1、调用基类的成员变量:
            基类名::变量名;
        2、调用基类的成员函数:
            基类名::函数名();
    ---------------------------------------------*/

    class Person
    {
    protected:
        char name[20];
        int age;
        char sex;
    public:
        void Register(char* name, int age, char sex){
            strcpy(this->name, name);
            this->age = age;
            this->sex=(sex='m'?'m':'f');
        }
        void ShowMe(){
            cout<<this->name<<'\t'<<this->age<<'\t'<<this->sex<<endl;
        }
    };

    //基类公有成员相当于派生类中的共有成员
    //基类保护成员相当于派生类中的保护成员
    class Student:public Person
    {
    public:
        int number;
        char className[20];
    public:
        void RegisterStu(char *className, int number, char* name, int age, char sex){
            strcpy(this->className, className);
            this->number = number;
            Person::Register(name, age, sex); //派生类成员函数直接使用基类的共有成员
        }
        void ShowStu(){
            cout<<this->number<<'\t'<<this->className<<endl;
            Person::ShowMe(); //直接使用基类的共有成员函数
            //cout<<Person::age<<endl; //调用基类的成员变量
        }
    };

    int test_public(){
        /*
        Person person;
        person.Register("覃唐弢", 21, 'm');
        person.ShowMe();
        */
        Student stu;
        stu.RegisterStu("软件技术",129781, "覃唐弢", 21, 'm');
        stu.ShowStu();
        stu.ShowMe(); //对象直接使用基类的共有成员

        return 0;
    }

    protected_extends.cpp

    #include <iostream>
    using namespace std;

    /**********************************************************************
    保护继承方式创建的派生类对基类各种成员访问权限如下:
        1、基类的公有成员和保护成员相当于派生类的保护成员
        2、派生类可以通过自身的成员函数或其子类的成员函数访问它们
        3、对于基类的私有成员,无论派生类内部成员或派生类使用者都无法直接访问
    **********************************************************************/
    class Person
    {
    protected:
        char name[20];
        int age;
        char sex;
    public:
        void Register(char* name, int age, char sex){
            strcpy(this->name, name);
            this->age = age;
            this->sex=(sex='m'?'m':'f');
        }
        void ShowMe(){
            cout<<this->name<<'\t'<<this->age<<'\t'<<this->sex<<endl;
        }
    };

    class Student:protected Person //基类的公有成员和保护成员都相当于派生类的保护成员
    {
    public:
        int number;
        char className[20];
    public:
        void RegisterStu(char *className, int number, char* name, int age, char sex){
            strcpy(this->className, className);
            this->number = number;
            Person::Register(name, age, sex); //派生类成员函数直接使用基类的共有成员
        }
        void ShowStu(){
            cout<<this->number<<'\t'<<this->className<<endl;
            Person::ShowMe(); //直接使用基类的共有成员函数
            //cout<<Person::age<<endl; //调用基类的成员变量
        }
    };

    int test_protected(){
        Student stu;
        stu.RegisterStu("软件技术",129781, "覃唐弢", 21, 'm');
        stu.ShowStu();
        //stu.ShowMe(); //error  基类的public函数 通过 protected 继承 在子类中 就成为了 protected 函数
        return 0;
    }

    private_extends.cpp

    #include <iostream>
    using namespace std;
    #include <string.h>

    /*************************************************************
    二、私有继承(private)

     继承特点:
        1、基类的共有成员和保护成员相当于派生类的私有成员
        2、对于基类的私有成员,无论派生类内部成员
        或派生类使用者都无法直接访问
    **************************************************************/

    class Person
    {
    protected:
        char name[20];
        int age;
        char sex;
    public:
        void Register(char* name, int age, char sex){
            strcpy(this->name, name);
            this->age = age;
            this->sex=(sex='m'?'m':'f');
        }
        void ShowMe(){
            cout<<this->name<<'\t'<<this->age<<'\t'<<this->sex<<endl;
        }
    };

    class Student:private Person //基类的共有成员和保护成员都相当于派生类的私有成员
    {
    public:
        int number;
        char className[20];
    public:
        void RegisterStu(char *className, int number, char* name, int age, char sex){
            strcpy(this->className, className);
            this->number = number;
            Person::Register(name, age, sex); //派生类成员函数直接使用基类的共有成员
        }
        void ShowStu(){
            cout<<this->number<<'\t'<<this->className<<endl;
            Person::ShowMe(); //直接使用基类的共有成员函数
            //cout<<Person::age<<endl; //调用基类的成员变量
        }
    };

    int test_private(){
        Student stu;
        stu.RegisterStu("软件技术",129781, "覃唐弢", 21, 'm');
        stu.ShowStu();
        //stu.ShowMe(); //error  private继承方式 基类public的函数 在派生类 就成为了 private函数 所以在外部不能调用
        return 0;
    }

    ///////////////////////////////

    继承 Example

    ////////////////////////////////

    Point.h

    #ifndef POINT_H
    #define POINT_H
    class Point{
    private:
        int x, y; //点x和y的坐标
    public:
        Point(int=0, int=0); //构造函数
    public:
        void setPoint(int, int); //设置坐标
        int getX(){
            return this->x;
        }
        int getY(){
            return this->y;
        }
        void printPoint();
    };
    #endif

     

    Point.cpp

    #include <iostream>
    using namespace std;
    #include "point.h"

    Point::Point(int a, int b){
        this->setPoint(a, b);
    }

    void Point::setPoint(int a, int b){
        this->x = a;
        this->y = b;
    }

    void Point::printPoint(){
        cout<<"["<<this->x<<", "<<this->y<<"]";
    }

    Example.cpp

    #include <iostream>
    using namespace std;
    #include "point.h"
    #include "circle.h"

    int test_point_circle(){
        Point p(30, 50);
        Circle c(120, 80, 10.0);
        cout<<"Point p:";
        p.printPoint();

        cout<<"\nCircle c:";
        c.printCircle();

        cout<<"The center of circle c:";
        c.Point::printPoint(); //调用 基类的 函数 ==> 变量名.基类名::函数名();

        cout<<"\nThe area of circle c:"
            <<c.area()<<endl;
        return 0;
    }

     

    /////////////////////////////

    派生类的构造函数与析构函数

    ////////////////////////

    #include <iostream>
    using namespace std;
    #include <string.h>

    /***********************************************************************
    派生类的构造函数和析构函数:

    一、派生类构造函数的一般形式:
        派生类名::派生类名(参数总表):基类名1(参数表1),…,基类名n(参数表n),
        内嵌对象名1(对象参数表1),…,内嵌对象名m(对象参数表m)
        {
            派生类新增加成员的初始化;
        }

    二、派生类构造函数各部分的执行次序
        1、调用基类构造函数,按他们在派生类定义的先后顺序调用
        2、调用内嵌对象构造函数,调用次序按各个对象在派生类内声明的顺序
        3、派生类的构造函数中的操作

    三、派生类析构函数
        派生类析构函数执行过程与构造函数执行过程相反。
        (1)执行派生类析构函数
        (2)执行内嵌对象的析构函数
        (3)执行基类析构函数
    ***********************************************************************/

    class Person{
    public:
        char name[20];
        int age;
    public:
        Person(char* name, int age){
            strcpy(this->name, name);
            this->age=age;
            cout<<"constructor of person...."<<endl;
        }
        ~Person(){
            cout<<"deconstructor of person...."<<endl;
        }
    public:
        void ShowMe(){
            cout<<this->name<<'\t'<<this->age<<endl;
        }
    };

    class Student:public Person{
    public:
        char className[20];
        Person Monitor; //内嵌对象
    public:
         Student(char* name, int age, char* className, char* name1, int age1)
            :Person(name, age),Monitor(name1, age1)
        {
            strcpy(this->className, className);    
            cout<<"constructor of Student...."<<endl;
        }
        ~Student(){
            cout<<"deconstrucor of Student...."<<endl;
        }
    public:
        void ShowStudent(){
            cout<<"基类对象"<<'\t';
                Person::ShowMe();
            cout<<"内嵌对象"<<'\t';
                Monitor.ShowMe();
            cout<<"派生类对象"<<'\t'<<this->className<<endl;
        }
    };

    int test_constructor_deconstrucor(){
        Student stu("覃唐弢", 21, "软件技术", "刘德华", 66);
        stu.ShowStudent();
        /*
            constructor of person.... //基类 构造函数
            constructor of person.... //内嵌对象 构造函数
            constructor of Student.... //派生类 构造函数
            基类对象        覃唐弢  21
            内嵌对象        刘德华  66
            派生类对象      软件技术
            deconstrucor of Student.... //派生类 析构函数
            deconstructor of person.... //内嵌对象 析构函数
            deconstructor of person.... //基类 析构函数
        */
        return 0;
    }

     

    ////////////////////////////////////

    多重继承

    ////////////////////////////////////

    Base1.h

    #ifndef BASE1_H
    #define BASE1_H
    class Base1{
    private:
        int value;
    public:
        Base1(int x){
            this->value = x;
        }
    public:
        int getData() const{
            return this->value;
        }
    };
    #endif

    Base2.h

    #ifndef BASE2_H
    #define BASE2_H
    class Base2{
    private:
        char letter;
    public:
        Base2(char c){
            this->letter=c;
        }
    public:
        /*
        表示成员函数隐含传入的this指针为const指针,决定了在该成员函数中,
        任意修改它所在的类的成员的操作都是不允许的
        因为隐含了对this指针的const引用
        */
        char getData() const{
            return this->letter;
        }
    };
    #endif

    Derivde.h
    #ifndef CIRCLE_H
    #define CIRCLE_H

    #include <iostream>
    using namespace std;
    #include "point.h"

    class Circle:public Point //继承
    {
    private:
        double radius;
    public:
        Circle(int x=0, int y=0, double r=0.0);
    public:
        void setRadius(double); //设置半径
        double getRadius(); //取半径
        double area(); //计算面积
        void printCircle(); //输出圆心坐标和半径
    };
    #endif

    Derived.cpp

    #include <iostream>
    using namespace std;
    #include "derived.h"

    Derived::Derived(int i, char c, double f):Base1(i),Base2(c)
    {
        this->real = f;
    }

    double Derived::getReal() const{
        return this->real;
    }

    void Derived::outPut(){
        cout<<"Integer:"<<Base1::getData()
            <<"\nCharacter:"<<Base2::getData()
            <<"\nReal number:"<<this->real
            <<endl;
    }

     

    多重继承.cpp

    #include <iostream>
    using namespace std;
    #include "derived.h"
    int main(){
        Base1 b1(10), *base1Ptr=0;
        Base2 b2('Z'), *base2Ptr=0;
        Derived d(7, 'A', 3.5);
      
        cout<<"对象输出各自的数据成员"<<endl;
        cout<<"Object b1 contains integer:"<<b1.getData()
            <<"\nObject b2 contains character:"<<b2.getData()
            <<"\nObject d contains:\n";
        d.outPut();
        cout<<"派生类对象对基类成员函数的访问"<<endl;
        cout<<"Data members of Derived can be"<<"accessed individually:"
            <<"\nInteger:"<<d.Base1::getData() //访问父类的函数
            <<"\nCharacter:"<<d.Base2::getData()
            <<"\nReal number:"<<d.getReal()
            <<endl;
        cout<<"Derived can be treated as an"
            <<"object of either base class:\n";
        cout<<"派生类对象作为Base1对象"<<endl;
        base1Ptr = &d;
        cout<<"base1Ptr->getData() yields:"
            <<base1Ptr->getData()
            <<'\n';
        cout<<"派生类对象作为Base2对象"<<endl;
        base2Ptr = &d;
        cout<<"base2Ptr->getData() yields:"
            <<base2Ptr->getData()
            <<'\n';
        
        return 0;
    }

     

     

     

     

     

     

  • 相关阅读:
    .请写出常用的linux指令
    Maven常用命令有哪些
    Maven的工程类型有哪些
    eclipse中Build Path 导入的包和复制到 lib 包的区别
    马踏棋盘算法递归+回溯法实现 C语言
    微信开发之移动手机WEB页面(HTML5)Javascript实现一键拨号及短信发送功能
    Android_保存用户名和密码码sharedPreference
    Android---XML序列化
    直接拿来用!最火的Android开源项目
    异步http开源框架使用(AsyncHttpClient)
  • 原文地址:https://www.cnblogs.com/qintangtao/p/2764376.html
Copyright © 2011-2022 走看看