继承和派生

Posted 0523xw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了继承和派生相关的知识,希望对你有一定的参考价值。

芯片ABC:

#include<iostream>
using namespace std;

class Gross
{
    public:
         void orient(int a,int b);
        void plus();
    private:
        int m,n;
};
void Gross::orient(int a,int b)
{
    m=a;
    n=b;
}
void Gross::plus()
{
    cout<<"两数相加得:"<<m+n<<endl;
}
class A:public Gross
{
    public:
        void orienta(int a,int b);
        void min();
    private:
        int m1,n2;
};
void A::orienta(int a,int b)
{
    orient(a,b);
    m1=a;
    n2=b;
}
void A::min()
{
    cout<<"两数相减得:"<<m1-n2<<endl; 
}
class B:public Gross
{
    public:
        void orientb(int a,int b);
        void mult();
    private:
        int m2,n2;
};
void B::orientb(int a,int b)
{
    orient(a,b);
    m2=a;
    n2=b;
}
void B::mult()
{
    cout<<"两数相乘得:"<<m2*n2<<endl;
}
class C:public Gross
{
    public:
        void orientc(int a,int b);
        void div();
    private:
        int m3,n3;
};
void C::orientc(int a,int b)
{
    orient(a,b);
    m3=a;
    n3=b;
}
void C::div()
{
    cout<<"两数相除得:"<<m3/n3<<endl;
}
int main()
{
    A a;B b;C c;
    a.orienta(1,9);
    a.min();
    a.plus();
    b.orientb(2,8);
    b.mult();
    b.plus();
    c.orientc(3,9);
    c.plus();
    c.div();
} 

 

技术分享图片

车类

#include <iostream>
using namespace std;
class vehicle
{
    public:
        vehicle(int a,int b){maxspeed=a;weight=b;
        }
        void run(){cout<<"nit"<<endl;
        }
        void stop(){cout<<"geiw"<<endl;
        }
    private:
        int maxspeed, weight;
};
class bicycle:public vehicle
{
    public:
        bicycle(int a,int b,int c):vehicle(a,b){height=c;
        }
    ~bicycle(){
    }
    private:
        int height;
};
class motorcar:public vehicle
{
    public:
        motorcar(int a,int b,int d):vehicle(a,b){seatnum=d;
        }
    ~motorcar(){
    }
    private:
        int seatnum;
};
class motorcycle:public bicycle,public motorcar
{
    public:
        motorcycle(int a,int b,int c,int d):bicycle(a,b,c),motorcar(a,b,d){
        }
    ~motorcycle(){
    }
};
int main()
{
    vehicle A(1,2);
    A.run();
    A.stop();
}

技术分享图片

 

以上是关于继承和派生的主要内容,如果未能解决你的问题,请参考以下文章

类的继承与派生

继承与派生

Part7 继承与派生 7.1继承的基本概念和语法 7.2 继承方式

重修课程day22(面向对象三之继承和派生)

面向对象之继承与派生

类的继承与派生