定義一個基類Account,數據成員包含string類變量userName用於保存賬戶主人姓名,函數成員包括默認構造函數、帶參構造函數用於初始化數據成員和輸出姓名的成員函PrintName()。從Account類派生出CreditAccount類,增加整型數據成員credit用於記錄該用戶信用額度,函數成員包括帶參構造函數用於初始化數據成員和輸出賬戶信息的成員函數PrintInfo()。要求:在函數PrintInfo()中需要調用基類的成員函數PrintName()。
輸入描述
無
輸出描述輸出共兩行,第一行為賬戶姓名,第二行為賬戶信用額度
樣例輸入無樣例輸出
I Love CPP 10000Code:
#include#include using namespace std; class Account { private: string userName; public: Account(){}; Account(string name); void PrintUserName(); }; class CreditAccount : public Account { public: CreditAccount(string name, int credit); void PrintInfo(); private: int credit; }; Account::Account(string name) { userName = name; } void Account::PrintUserName() { cout << userName << endl; } CreditAccount::CreditAccount(string name, int credit) :Account(name), credit(credit){} void CreditAccount::PrintInfo() { PrintUserName(); cout << credit << endl; } //請實現Account構造函數Account(char *name) //請實現Account的PrintUserName()函數 //請實現CreditAccount類的構造函數CreditAccount(char* name, long number) //請實現CreditAccount類的PrintInfo()函數 int main() { CreditAccount a("I Love CPP", 10000); a.PrintInfo(); return 0; }
下面的代碼聲明了三個基類Base1、Base2和Base3,然後從這三個基類按照公有方式派生出類Derived。在每個類中分別定義帶一個整型參數的構造函數和析構函數輸出提示信息,構造函數的提示信息中需要包含整型參數的數值。請將下面的代碼補充完整,使得輸出結果與樣例輸出相同,注意:測試數據有多組。
輸入描述
每組輸入為4個整數用空格隔開
輸出描述根據構造和析構函數的調用順序輸出
樣例輸入1 2 3 4樣例輸出
Base2 constructor called 3 Base1 constructor called 2 Base3 constructor called 4 Derived constructor called 1 Derived destructor called Base3 destructor called Base1 destructor called Base2 destructor calledCode:
#includeusing namespace std; class Base1 { public: Base1(int x); ~Base1(); }; class Base2 { public: Base2(int x); ~Base2(); }; class Base3 { public: Base3(int x); ~Base3(); }; class Derived :public Base2,public Base1,public Base3//繼承上面3個類 { public: Derived(int x1, int x2, int x3, int x4); ~Derived(); }; Base1::Base1(int x) { cout << "Base1 constructor called " << x << endl; } Base1::~Base1() { cout << "Base1 destructor called" << endl; } //依照Base1類中的代碼實現其它類的構造函數和析構函數 Base2::Base2(int x) { cout << "Base2 constructor called " << x << endl; } Base2::~Base2() { cout << "Base2 destructor called" << endl; } Base3::Base3(int x) { cout << "Base3 constructor called " << x << endl; } Base3::~Base3() { cout << "Base3 destructor called" << endl; } Derived::Derived(int x1, int x2, int x3, int x4) :Base1(x2), Base2(x3), Base3(x4) { cout <<"Derived constructor called " << x1 << endl; } Derived::~Derived() { cout << "Derived destructor called" << endl; } int main() { int x[4]; for (int i = 0; i < 4; ++i) cin >> x[i]; Derived d(x[0], x[1], x[2], x[3]); return 0; }
下面的代碼聲明了兩個基類Base1和Base2,然後從這兩個基類按照公有方式派生出類Derived。基類和派生類都各自包含一個公有成員x,並且Base1和Base2各有接受一個整型參數的構造函數,Derived的構造函數接受Base1和Base2的對象引用a,b來初始化Derived類對象,並令x為Base1::x和Base2::x之和。請將下面的代碼補充完成,使得輸出符合要求。
輸入描述每組輸入為2個整數用空格隔開
輸出描述主函數自動完成輸出
樣例輸入1 2樣例輸出
1+2=3Code:
#includeusing namespace std; struct Base1 { int x; Base1(int x); }; struct Base2 { int x; Base2(int x); }; struct Derived :public Base1, public Base2 { int x; Derived(Base1& a, Base2& b); }; Base1::Base1(int x) :x(x){} Base2::Base2(int x) : x(x){} Derived::Derived(Base1& a, Base2& b) : Base1(a.x), Base2(b.x), x(a.x + b.x){} //請實現Base1,Base2, Derived的構造函數 int main() { int x, y; cin >> x >> y; Base1 a(x); Base2 b(y); Derived d(a, b); cout << d.Base1::x << "+" << d.Base2::x << "=" << d.x << endl; return 0; }