第十周练习 继承 二义性和作用域操作限定标识符 阅读程序

/*   
 *Copyright(c) 2016,烟台大学计算机学院   
 *作    者:刘金石   
 *完成日期:2016年5月3日   
 *问题描述:继承机制阅读程序  
*/ 
#include <iostream>
using namespace std;
class Base
{
public:
    Base(char i) { cout<<"Base constructor. --"<<i<<endl; }
};
class Derived1:virtual public Base
{
public:
    Derived1(char i,char j):Base(i){
        cout<<"Derived1 constructor. --"<<j<<endl;
    }
};
class Derived2:virtual public Base
{
public:
    Derived2(char i,char j):Base(i){
        cout<<"Derived2 constructor. --"<<j<<endl;
    }
};
class MyDerived: public Derived1,public Derived2
{
public:
    MyDerived(char i,char j,char k,char l,char m,char n,char x):
    Derived2(i,j), Derived1(k,l), Base(m), d(n){
        cout<<"MyDerived constructor. --"<<x<<endl;
    }
private:
    Base d;
};
int main()
{
    MyDerived obj('A','B','C','D','E','F','G');
    return 0;
}
运行结果:
<img src="http://img.blog.csdn.net/20160503222217577?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />
程序分析:
在此程序中,Derived1和Derived2都继承了Base,而MyDerived又同时继承Derived1和Derived2
所以对于类Base便出现了二义性,对于二义性问题,将Base类设置为虚基类,即在继承方式前加上virtual
在建立对象时,只有最派生的构造函数调用虚基类的构造函数,该派生类的其他基类对虚基类的构造函数的调用会被忽略
所以程序创建MyDerived的对象obj时首先调用Base类的构造函数,输出 Base constructor. --E
然后按照构造函数初始化列表中继承的顺序调用构造函数,
即先调用Derived1(k,l),K不会被输出上面已经说过,故只输出Derived1 constructor. --D
然后调用函数Derived2(i,j),输出Derived2 constructor. --B
然后构造成员函数Base,再次调用Base类的构造函数,输出Base constructor. --F
最后输出MyDerived constructor. --G
程序二:
<pre name="code" class="cpp">#include <iostream>
using namespace std;
class A
{
protected:
    int a,b;
public:
    A(int aa, int bb):a(aa), b(bb) {}
    void printA(){
        cout<<"a: "<<a<<"\tb: "<<b<<endl;
    }
};
class B: public A
{
    int c;
public:
    B(int aa, int bb, int cc):A(aa,bb),c(cc) {}
    void printB()
    {
       cout<<"a: "<<a<<"\tb: "<<b<<"\tc: "<<c<<endl;
    }
};
int main()
{
    A a(1,1);
    B b(2,3,4);
    //此处加入下面各小题中的代码
    return 0;
}

问题(a)

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    a=b<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
    a<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.printA</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
    b<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.printA</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
    b<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.printB</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

你认为输出结果是:____________

a:2 b:3

a:2 b:3

a:2 b:3 c:4

运行程序的结果是:_同上___________ 
问题(b)

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    b=a<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
    a<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.printA</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
    b<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.printA</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
    b<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.printB</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

程序会发生编译错误,原因是:_一个共有派生类的对象在使用上可以被当作基类的对象,反之则禁止___ 
记录下IDE中提示的错误并理解:_no match for 'operator=' in b=a'__ 
问题(c)

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    A &<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">r1</span>=a<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
    A &<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">r2</span>=b<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">r1</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.printA</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">r2</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.printA</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">r2</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.printB</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

将会发生错误的一行删除; 
对余下的程序,你认为输出是:

_a:1     b:1
a:2     b:3___
 
实际运行的输出是:___同上_______ 
那一行的错误原因是:在类A中找不到函数printB
问题(d)

<code class="hljs haskell has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    <span class="hljs-type" style="box-sizing: border-box; color: rgb(102, 0, 102);">A</span> *p=&a;
    p->printA();
    p=&b;
    p->printA();
    p->printB();</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

将会发生错误的一行删除; 
对余下的程序,你认为输出是:____ 

a:1     b:1
a:2     b:3___
 
实际运行的输出是:_同上_________ 
那一行的错误原因是:在类A中找不到函数printB
问题(e) 
在class A中增加成员函数:int getA(){return a;} 
在main函数前增加一般函数:

<code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> f(A x)
{
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"aaaaah, my a: "</span><<x.getA()<<endl;
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

main函数中指定部分为:

<code class="hljs scss has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    <span class="hljs-function" style="box-sizing: border-box;">f(a)</span>;
    <span class="hljs-function" style="box-sizing: border-box;">f(b)</span>;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

你认为输出结果是:

aaaaah, my a: 1
aaaaah, my a: 2
运行程序的结果是:_同上___________



 

你可能感兴趣的:(继承,组合,Class,二义性,作用域操作限定标识符)