12th Week "C + + language Basics" program reading-Multiple inheritance (5)

Source: Internet
Author: User

Problem Description:

(5) Read the definition of the following class, and say the result of a call from a different situation in the test function
<span style= "Font-family:kaiti_gb2312;font-size:18px;color: #ff6666;" ><strong> #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& lt;< "A:" <<a<< "\TB:" <<b<< "\TC:" <<c<<endl;    }}; int main () {    a A ();    b b (2,3,4);    Add the code in the following quiz    return 0;} </strong></span>

(a)
<span style= "Font-family:kaiti_gb2312;font-size:18px;color: #ff6666;" ><strong>    a=b;    A.printa ();    B.printa ();    B.PRINTB ();</strong></span>
What do you think the output is: a:2 B:3 a:2 B:3 a:2 b:3 c:4 The result of running the program is: (b)
<span style= "Font-family:kaiti_gb2312;font-size:18px;color: #ff6666;" ><strong>    b=a;    A.printa ();    B.printa ();    B.PRINTB ();</strong></span>
A compilation error occurs because B inherits a data member that has a a, but not equal to a, data member.
Record the errors and understand the prompts in the IDE:

(c)
<span style= "Font-family:kaiti_gb2312;font-size:18px;color: #ff6666;" ><strong>    A &r1=a;    A &r2=b;    R1.printa ();    R2.printa ();    R2.PRINTB ();</strong></span>
Delete the line where the error occurred;
For the rest of the program, you think the output is:
a:1 b:1 a:2 B:3 the actual running output is:The wrong reason for that line is that a &r2=b; R2 is a object that cannot use the member function of B.
(d)
<span style= "Font-family:kaiti_gb2312;font-size:18px;color: #ff6666;" ><strong>    A *p=&a;    P->printa ();    p=&b;    P->printa ();    P->PRINTB ();</strong></span>
Delete the line where the error occurred;
For the rest of the program, you think the output is:
a:1 b:1 a:2 B:3
The actual running output is: the wrong reason for that line is that p is a pointer to a object, because B inherits a so it can execute p=&b; P->printa (); But there is no printb () in a, so p->printb (); error cannot be performed. (E)
Add member functions in Class A:
<span style= "Font-family:kaiti_gb2312;font-size:18px;color: #ff6666;" ><strong>    int Geta () {return A;} </strong></span>
add a general function before the main function:
<span style= "Font-family:kaiti_gb2312;font-size:18px;color: #ff6666;" ><strong>void f (A x) {    cout<< "Aaaaah, my A:" <<x.geta () <<endl;} </strong></span>
The specified part of the main function is:
<span style= "Font-family:kaiti_gb2312;font-size:18px;color: #ff6666;" ><strong>    F (a);    F (b);</strong></span>
What do you think the output is: Aaaaah, my a:1 Aaaaah, my a:2 The result of running the program is:

12th Week "C + + language Basics" program reading-Multiple inheritance (5)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.