C++ Notes-Inheritance-06

C7-1 账户类

 
(100 满分)
题目描述

定义一个基类Account,数据成员包含string类变量userName用于保存账户主人姓名,函数成员包括默认构造函数、带参构造函数用于初始化数据成员和输出姓名的成员函PrintName()。从Account类派生出CreditAccount类,增加整型数据成员credit用于记录该用户信用额度,函数成员包括带参构造函数用于初始化数据成员和输出账户信息的成员函数PrintInfo()。要求:在函数PrintInfo()中需要调用基类的成员函数PrintName()

输入描述

输出描述

输出共两行,第一行为账户姓名,第二行为账户信用额度

样例输入


样例输出
I Love CPP
10000
Code:
#include <iostream>
#include <string>
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;
}

C7-2 多继承

 
(100/100 分数)
题目描述

下面的代码声明了三个基类Base1Base2Base3,然后从这三个基类按照公有方式派生出类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 called
Code:

#include <iostream>
using 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;
}

C7-3 用类实现A+B

 
(100/100 分数)
题目描述

下面的代码声明了两个基类Base1Base2,然后从这两个基类按照公有方式派生出类Derived。基类和派生类都各自包含一个公有成员x,并且Base1和Base2各有接受一个整型参数的构造函数,Derived的构造函数接受Base1和Base2的对象引用a,b来初始化Derived类对象,并令x为Base1::x和Base2::x之和。请将下面的代码补充完成,使得输出符合要求。

输入描述

每组输入为 2 个整数用空格隔开

输出描述

主函数自动完成输出

样例输入

1 2

样例输出
1+2=3
Code:
#include <iostream>
using 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;
}







你可能感兴趣的:(C++,继承)