欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C++ Notes-Inheritance-06

程序员文章站 2023-08-26 08:36:13
c7-1 账户类 (100 满分) 题目描述 定义一个基类account,数据成员包含string类变量username用于保存账户主人姓名,函数成员包括默认构造函数、带参构造函数用于初始化数据成...

c7-1 账户类

(100 满分)

题目描述

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

输入描述

输出描述

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

样例输入
样例输出
i love cpp
10000

code:

#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;
}

c7-2 多继承

(100/100 分数)

题目描述

下面的代码声明了三个基类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 called
code:
#include 
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 分数)

题目描述

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

输入描述

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

输出描述

主函数自动完成输出

样例输入
1 2
样例输出
1+2=3

code:

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