您的位置:首页 > 其它

第11周-继承和派生-课后实践和阅读理解

2015-05-17 10:20 302 查看
/* 
* Copyright (c) 2014, 烟台大学计算机学院 
* All rights reserved. 
* 文件名称:test.cpp 
* 作    者:刘畅 
* 完成日期:2015 年 5  月  17 日 
* 版 本 号:v1.0 
* 
* 问题描述:请阅读程序。
* 输入描述: ;
* 程序输出: 按要求输出。

(1)

#include <iostream>
using namespace std;
class Data
{
public:
    Data (int i):x(i){cout<<'A';}
    ~Data(){cout<<'B';}
private:
    int x;
};
class Base
{
public:
    Base(int i):b1(i){cout<<'C';}
    ~Base(){cout<<'D';}
private:
    int b1;
};

class Derived:public Base
{
public:
    Derived (int i,int j):Base(i),d1(j){
        cout<<"E";
    }
    ~Derived(){cout<<"F";}
    Data d1;
};

int main()
{
    Derived obj(1,2);
    return 0;
}


运行结果:



(2)

#include <iostream>
using namespace std;
class G
{
public:
    static int m;
    G( ){
        m++;
        cout<<"G begins\n";
    }
    ~G(){
        cout<<"G ends\n";
        m--;
    }
};

int G::m=0;
class D:public G
{
public:
    D(){
        m++;
        cout<<"D begins\n";
    }
    ~D(){
        cout<<"D ends\n";
        m--;
    }
};

int main()
{
    D objg;
    cout<<G::m<<endl;
    return 0;
}


运行结果:




(3)



#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;
}


运行结果:




将public删除之后对程序进行编译,如图:



原因是:将共用继承改变为私有继承之后(修改处不作声明时默认为私有继承),基类A中的公用成员在派生类B中变成了私有成员,不能在派生类外被直接访问。



将public改为protected后,如图:



原因是,公用继承改为保护继承之后所有成员同样不能被类外访问,其公用成员和保护成员可以被派生类的成员函数访问,私有成员则不可访问。



(4)

#include <iostream>
using namespace std;
class Part
{
public:
    Part();
    Part(int i);
    ~Part();
private:
    int val;
};
Part::Part()
{
    val=0;
    cout<<"调用Part的默认构造函数:"<<val<<endl;
}
Part::Part(int i)
{
    val=i;
    cout<<"调用Part的构造函数: "<<val<<endl;
}
Part::~Part()
{
    cout<<"调用Part的析构函数: "<<val<<endl;
}
class Whole: public Part
{
public:
    Whole();
    Whole(int,int,int,int);
    ~Whole();
private:
    Part one;
    Part two;
    int data;
};
Whole::Whole()
{
    data=0;
    cout<<"调用whole的默认构造函数: "<<data<<endl;
}
Whole::Whole(int p, int i,int j,int k):Part(p),one(j),two(i),data(k)  //问题2
{
    cout<<"调用whole的构造函数: "<<data<<endl;
}
Whole::~Whole()
{
    cout<<"调用whole的析构函数: "<<data<<endl;
}
void f()
{
    Whole w1;
    Whole w2(1,2,3,4);
}
int main()
{
    f();
    return 0;
}


运行结果:



将Whole 类的构造函数改为下面形式:请解释出现的警告信息:

Whole::Whole(int p, int i,int j,int k): Part(p),two(i),one(j),data(k)

如下:





将one two对应的数值在继承时交换了位置,在派生类中容易出现问题。。。具体怎么解释我也没弄清,感觉大致就是这个意思吧,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: