第十一周--通过继承拥有基类的资源

来源:互联网 发布:java语法有多少 编辑:程序博客网 时间:2024/05/22 12:19
/*Copyright(c) 烟台大学计算机与控制工程学院学生作者:刘慧艳日期:2014.05.06版本号:V1.0问题描述:通过继承拥有基类的资源*/#include <iostream>using namespace std;class A{    private :       int x;    protected:       int y;    public:       int z;       A(int a,int b,int c)       {           x=a;           y=b;           z=c;       }       int Getx()       {           return x;       }       int Gety()       {           return y;       }       void ShowA()       {           cout<<"x="<<x<<'\t';           cout<<"y="<<y<<'\t';           cout<<"z="<<z<<'\n';       }};class B:public A{    private :    int m,n;    public :    B(int a,int b,int c,int d,int e):A(a,b,c)    {        m=d;        n=e;    }    void Show()    {        cout<<"m="<<m<<'\t'<<"n="<<n<<'\n';        cout<<"x="<<Getx()<<'\t';        cout<<"y="<<y<<'\t'<<"z="<<z<<'\n';    }    int Sum()    {        return (Getx()+y+z+m+n);    }};int main(){    B b1(1,2,3,4,5);    b1.ShowA();    b1.Show();    cout<<"Sum="<<b1.Sum()<<'\n';    cout<<"x="<<b1.Getx()<<'\t';    cout<<"y="<<b1.Gety()<<'\t';    cout<<"z="<<b1.z<<'\n';    return 0;}

0 0