11th Week reading Program writing execution Results 1 (5)

Source: Internet
Author: User

/**copyright (c) 2016, Yantai University School of computer *all rights reserved.* file name:*    : Liu Yun * completion date: May 8, 2016 * version number: v6.0** problem Description:  Read program write Row result 1 (5) * Input Description:   no * program Output:*//********************************* (a) ********************************************** /#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 ();    b b (2,3,4);    Add the code (a) a=b in each quiz    .    A.printa ();    B.printa ();    B.PRINTB ();    return 0;}

I think the result is:

A:2 B:3
A:2 B:3
A:2 B:3 C:4

The result of running the program is:


/********************************* (a) ****************************************************

/********************************* (b) ****************************************************/

#include <iostream>using namespace Std;class a{protected:int a,b;public:a (int aa,int bb): A (AA), B (BB) {} voi D 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 ();    b b (2,3,4);    Add the code (b) a=b in each quiz.    A.printa ();    B.printa ();    B.PRINTB (); return 0;} Running results of the program://c:\users\user\documents\123.cpp| | In function ' int main () ': |//c:\users\user\documents\123.cpp|26|error:no match for ' operator= ' (operand types is ' B ' and ' A ') |//c:\users\user\documents\123.cpp|26|note:candidate is:|//c:\users\user\documents\123.cpp|11|note:b& B: : operator= (const b&) |//c:\users\user\documents\123.cpp|11|note:no known conversion for argument 1 from ' A ' to ' cons T b& ' |//| | = = = Build failed:1 error (s), 0 warning (s) (0 minute (s), 3 second (s)) ===| 


The cause of the error is:

Derived classes should be assigned to the base class

Understand:

In any case, a derived class inherits all members except the construct and destructor, and the derived class can add new members to increase their uniqueness, and when assigned, only the derived class can be assigned a value to the base class.

/********************************* (c) ****************************************************/

#include <iostream>using namespace Std;class a{protected:int a,b;public:a (int aa,int bb): A (AA), B (BB) {} voi D 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 ();    b b (2,3,4);    Add the code in each quiz (c) A &r1=a;    A &r2=b;    R1.printa ();    R2.printa ();    R2.PRINTB (); return 0;} Running Results 1://c:\users\user\documents\123.cpp| | In function ' int main () ': |//c:\users\user\documents\123.cpp|30|error: ' Class A ' has no member named ' PRINTB ' |//| | = = = Build failed:1 error (s), 0 warning (s) (0 minute (s), 3 second (s)) ===| #include <iostream>using namespace Std;clas s a{protected:int a,b;public:a (int aa,int BB): A (AA), B (BB) {} void PrintA () {cout<< "A:" <<a<< "\ T B: "<<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<& lt; "    \TB: "<<b<<" \TC: "<<c<<endl;    }};int Main () {a A ();    b b (2,3,4);    Add the code in each quiz (c) A &r1=a;    A &r2=b;    R1.printa ();    R2.printa ();    R2.PRINTB (); return 0;} Run results 2://a:1 B:1//a:2 b:3////process returned 0 (0x0) execution time:1.391 s//press any key to continue. Error Original Because: a base class cannot use a function of a derived class

/********************************* (d) ****************************************************/


#include <iostream>using namespace Std;class a{protected:int a,b;public:a (int aa,int bb): A (AA), B (BB) {} voi D 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 ();    b b (2,3,4);    Add the code in each quiz (d) A *p=&a;    P->printa ();    p=&b;    P->printa ();    P-&GT;PRINTB (); return 0;} Run Result://c:\users\user\documents\123.cpp| | In function ' int main () ': |//c:\users\user\documents\123.cpp|30|error: ' Class A ' has no member named ' PRINTB ' |//| | = = = Build failed:1 error (s), 0 warning (s) (0 minute (s), 3 second (s)) ===| #include <iostream>using namespace Std;clas s a{protected:int a,b;public:a (int aa,int BB): A (AA), B (BB) {} void PrintA () {cout<< "A:" <<a<< "\ T B: "<<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 ();    b b (2,3,4);    Add the code in each quiz (d) A *p=&a;    P->printa ();    p=&b;   P->printa ();    P-&GT;PRINTB (); return 0;} Run Result://a:1 b:1//a:2 b:3////process returned 0 (0x0) execution time:1.224 s//press any key to continue. Cause of error : P belongs to a function of a base class pointer that cannot derive a class


/********************************* (e) ****************************************************/

#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;}    int Geta () {return A;}}; 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;    }}; void f (A x) {    cout<< "Aaaaah, my A:" <<x.geta () <<endl;} int main () {    a A ();    b b (2,3,4);    Add the code for each quiz (E)    F (a) here;    f (b);    return 0;}


I think the result of the operation is:

Aaaaah, my a:1
Aaaaah, my A:2

Running results of the program:




11th Week reading Program writing execution Results 1 (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.