C++7-1- inheritance and derivation: account class

Define a base class Account. The data member contains a string class variable userName, which is used to save the name of the Account owner. The function members include the default constructor, the constructor with parameters, and the member function printusername () used to initialize the data member and output the name. The CreditAccount class is derived from the Account class, and the integer data member credit is added to record the user's credit limit. The function members include the constructor with parameters, and the member function PrintInfo() is used to initialize the data member and output the Account information. Requirement: the member function PrintUserName() of the base class needs to be called in the function PrintInfo().

Here is a code you can fill in and test:

#include <iostream>
#include <string>
using namespace std;

class Account
{
public:
    Account(){}
    Account(string name);
    void PrintUserName();
private:
    string userName;
};

class CreditAccount : public Account
{
public:
     CreditAccount(string name, int credit);
     void PrintInfo();
private:
     int credit;
};

//=============================================
// Please implement the Account constructor Account(string name)
Account::Account(string name) {   }

// Please implement the PrintUserName() function of Account
void Account::PrintUserName() {   }

// Please implement the constructor of CreditAccount class CreditAccount(string name, int credit)
CreditAccount::CreditAccount(string name, int credit) {   }

// Please implement the PrintInfo() function of the CreditAccount class
void CreditAccount::PrintInfo() {   }
//=============================================

int main()
{
     CreditAccount a("I Love CPP", 10000);
     a.PrintInfo();
     return 0;
}

In order to ensure that you write the program as required, we will use OJ's code template function.

When submitting code, you can only submit the four member functions that you are required to fill in the code, and never submit any other code; In the code above, you can see the same comments as the two separators. You just need to submit the middle part of the two separators. When submitting, you will also see a code template, including four empty functions; If you plan to program online, you only need to fill in the corresponding code in braces and add calls to the base class constructor.

After your code is submitted, it will first replace the middle part of the two separation lines above the code, and then compile and run it.

Be sure to implement each member function as required. Don't try to directly output "I love CPP" and 10000, otherwise you may get 0 points.

Enter description

You don't know what input the program will receive. All you need to do is implement the four functions that you are required to implement in the topic.

Output description

When calling Account::PrintUserName(), you need to output a line containing the account name, and then wrap the line.

When calling CreditAccount::PrintInfo(), you need to output two lines: the first line is the account name and the second line is the account credit limit; Wrap the end of the second line.

Example 1:

Input: None

Output: I love CPP 10000

/*
#include <iostream>
#include <string>
using namespace std;

class Account // Base class definition
{ 
    string userName;
public:
    Account(){};
    Account( string name );
    void  PrintUserName();
};

class CreditAccount : public Account // Derived class definition
{
public:
    CreditAccount(string name, int credit);
    void PrintInfo();
private:
    int credit;
};
*/

//=============================================
//Please implement the Account constructor Account(string name)
Account::Account(string name)
{
    userName = name;
}
//Please implement the PrintUserName() function of Account
void Account::PrintUserName()
{
    cout << userName << endl; // Output account name
}
//Please implement the constructor of CreditAccount class CreditAccount(string name, int credit)
CreditAccount::CreditAccount(string name,int credit):Account(name),credit(credit){}

//Please implement the PrintInfo() function of the CreditAccount class
void CreditAccount::PrintInfo()
{
    PrintUserName(); // Call the public member function of the base class
    cout << credit << endl; // Output account credit
}
//=============================================

/*
int main()
{
    CreditAccount a("I Love CPP", 10000);
    a.PrintInfo();
    return 0;
}
*/

Tags: C++

Posted by cyprus on Sat, 16 Apr 2022 18:11:47 +0930