Academy Online tsinghuax:00740043_2x C + + Language programming advanced Seventh Chapter Lab

Source: Internet
Author: User

First question: Account class

Title Description

Defines a base class account with a data member containing the String class variable username used to hold the name of the owner, the function member includes the default constructor, the parameter constructor is used to initialize the data member, and the Member letter Printname () that outputs the name. Derive the Creditaccount class from the account class and increase the integer data member credits used to record the user credit limit, the function member includes the member function Printinfo () that is used to initialize the data member and output account information with the parameter constructor. Requirement: the member function printname () of the base class needs to be called in the function Printinfo (). Populate the following code:

#include <iostream>#include<string>using namespacestd; classAccount {stringUserName;  Public: Account () {}; Account (stringname); voidPrintusername ();        }; classCreditaccount: PublicAccount { Public: Creditaccount (stringNameintCredit ); voidPrintinfo (); Private:         intCredit ;        }; //please implement account constructor account (char *name)//please implement the Printusername () function of account//please implement the constructor of the Creditaccount class Creditaccount (char* name, long number)//please implement the Printinfo () function of the Creditaccount class        intMain () {Creditaccount A ("I Love CPP",10000);         A.printinfo (); return 0; }

Enter a description

No

Output description

Output a total of two lines, first Action Account name, second act account credit limit

Sample input

No

Sample output

I Love CPP10000
#include <iostream>#include<string>using namespacestd;classaccount{stringUserName; Public: Account (stringname): UserName (name) {UserName=name; }    //Account (string name);    voidPrintusername () {cout<< UserName <<Endl; }};classCreditaccount: Publicaccount{ Public: Creditaccount (stringNameLongNumber ): Account (name), credit (number) {}voidPrintinfo () {printusername (); cout<< Credit <<Endl; }Private:    LongCredit ;};intMain () {Creditaccount A ("I Love CPP",10000);    A.printinfo (); return 0;}

Second question: Multiple inheritance

Title Description

The following code declares three base classes, Base1, Base2, and Base3, and then derives the class derived from the three base classes in public form. In each class, you define a constructor with an integer parameter and a destructor output hint, and the constructor's hint information needs to contain the numeric value of the integral type parameter. Please complete the following code so that the output is the same as the sample output, note: There are several sets of test data.

#include <iostream>using namespacestd; classBase1 { Public: Base1 (intx); ~Base1 ();       }; classBase2 { Public: Base2 (intx); ~Base2 ();    }; classBASE3 { Public: Base3 (intx); ~Base3 ();       }; classDerived://inherit the above 3 classes    {     Public: Derived (intX1,intX2,intX3,intx4); ~Derived ();       }; Base1::base1 (intx) {cout<<"Base1 Constructor called"<<x<<Endl; } BASE1::~Base1 () {cout<<"Base1 destructor called"<<Endl; }    //implement constructors and destructors for other classes according to the code in the Base1 class          intMain () {intx[4];  for(inti =0; I <4; ++i) Cin>>X[i]; Derived D (x[0], x[1], x[2], x[3]); return 0; }

Enter a description

Each set of inputs is 4 integers separated by a space

Output description

Output based on the invocation order of the construction and destructor functions

Sample input

1 2 3 4

Sample output

Base2 constructor called 3base1 constructor called 2base3 constructor called 4Derived constructor called 1Derived destruct or CalledBase3 destructor CalledBase1 destructor CalledBase2 destructor called
#include <iostream>using namespacestd;classbase1{ Public: Base1 (intx); ~Base1 ();};classbase2{ Public: Base2 (intx); ~Base2 ();};classbase3{ Public: Base3 (intx); ~Base3 ();};classDerived: PublicBase2, PublicBASE1, Publicbase3{ Public: Derived (intX1,intX2,intX3,intx4): Base1 (x2), Base2 (x3), BASE3 (x4) {cout<<"Derived Constructor called"<<x1<<Endl; }    ~Derived () {cout<<"Derived destructor called"<<Endl; }}; Base1::base1 (intx) {cout<<"Base1 Constructor called"<<x<<Endl;} BASE1::~Base1 () {cout<<"Base1 destructor called"<<Endl;} Base2::base2 (intx) {cout<<"Base2 Constructor called"<<x<<Endl;} BASE2::~Base2 () {cout<<"Base2 destructor called"<<Endl;} BASE3::BASE3 (intx) {cout<<"Base3 Constructor called"<<x<<Endl;} BASE3::~Base3 () {cout<<"Base3 destructor called"<<Endl;}intMain () {intx[4];  for(inti =0; I <4; ++i) Cin>>X[i]; Derived D (x[0], x[1], x[2], x[3]); return 0;}

Academy Online tsinghuax:00740043_2x C + + Language programming advanced Seventh Chapter Lab

Related Article

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.