C++ 面向对象 第五,六周作业

文章提供了两段代码,第一段是关于C++中向量类的设计,包括向量的加、减、乘和赋值运算,以及输入输出功能。第二段代码展示了一个员工类的继承结构,包括一般员工、销售员工和销售经理,每个类都有输入、计算工资和输出工资条的功能,其中销售员工和销售经理类基于一般员工类派生并增加了额外的属性和计算逻辑。
摘要由CSDN通过智能技术生成

第五周作业:

有向量X,Y。
编写程序定义向量类 Vectlor,重载运算符+、-、*和=,实现向量之间的加、减、乘和赋值运算;运算符>>、<<实现向量的输入、输出功能。注意检测运算的合法性。

#include <iostream>
using namespace std;
class Vector
{
private:
public:
	double* v;
	int len;
public:
	Vector();
	Vector(int size);
	Vector(double*, int);
	Vector(const Vector&);
	~Vector();
	double& operator[](int i);
	Vector& operator =(const Vector&);
	friend Vector operator +(Vector&, Vector&);
	friend Vector operator - (Vector&, Vector&);
	friend double operator *(Vector&, Vector&);
	friend ostream& operator <<(ostream& output, Vector&);
	friend istream& operator >>(istream& input, Vector&);
};
Vector::Vector()
{
	int size;
	cout << "size of vector:" << endl;
	cin >> size;
	len = size;
	v = new double[len];
	for (int i = 0; i < len; i++)
	{
		v[i] = 0;
	}
}
Vector::Vector(int size)
{
	len = size;
	v = new double[len];
	for (int i = 0; i < len; i++)
	{
		v[i] = 0;
	}
}
Vector::Vector(double* p, int size)
{
	len = size;
	v = new double[len];
	for (int i = 0; i < len; i++)
	{
		v[i] = p[i];
	}
}
Vector::Vector(const Vector& p)
{
	this->len = p.len;
	this->v = new double[this->len];
	for (int i = 0; i < this->len; i++)
	{
		this->v[i] = p.v[i];
	}
}
Vector::~Vector()
{
	if (v != nullptr)
	{
		delete[]v;
		v = nullptr;
	}
	len = 0;
}
double& Vector::operator[](int i)
{
	if (i >= 0 && i < this->len)
	{
		return v[i];
	}
	else
	{
		cout << "number" << i << " is banned" << endl;
		exit(-1);
	}
}
Vector& Vector::operator =(const Vector& p)
{
	if (this == &p)
	{
		return *this;
	}
	else
	{
		this->len = p.len;
		if (this->v != nullptr)
		{
			delete[]v;
			v = nullptr;
		}
		v = new double[this->len];
		for (int i = 0; i < this->len; i++)
		{
			this->v[i] = p.v[i];
		}
		return *this;
	}
}
Vector operator +(Vector& a, Vector& b)
{
	if (a.len == b.len)
	{
		Vector temp(a.len);
		for (int i = 0; i < a.len; i++)
		{
			temp.v[i] = a.v[i] + b.v[i];
		}
		return temp;
	}
	else
	{
		cout << "无法相加" << endl;
		exit(-1);
	}
}
Vector operator -(Vector& a, Vector& b)
{
	if (a.len == b.len)
	{
		Vector temp(a.len);
		for (int i = 0; i < a.len; i++)
		{
			temp.v[i] = a.v[i] - b.v[i];
		}
		return temp;
	}
	else
	{
		cout << "无法相减" << endl;
		exit(-1);
	}
}
double operator *(Vector& a, Vector& b)
{
	if (a.len == b.len)
	{
		double sum = 0;
		for (int i = 0; i < a.len; i++)
		{
			sum += a.v[i] * b.v[i];
		}
		return sum;
	}
	else
	{
		cout << "无法相乘" << endl;
		exit(-1);
	}
}
ostream& operator <<(ostream& output, Vector& p)
{
	for (int i = 0; i < p.len; i++)
	{
		output << p.v[i] << " ";
	}
	return output;
}
istream& operator >>(istream& input, Vector& p)
{
	for (int i = 0; i < p.len; i++)
	{
		input >> p.v[i];
	}
	return input;
}
int main()
{
	double x[6] = { 1,2,3,4,5,6 };
	double mul = 0;
	Vector X(x, sizeof(x) / sizeof(double));
	cout << "X:" << X << endl;
	cout << "X[0]=" << X[0] << endl;
	Vector Y;
	cout << "cin the Y" << "(len=" << Y.len << ")" << endl;
	cin >> Y;
	cout << "Y=" << Y << endl;
	Vector Z(0);
	Z = X + Y;
	cout << "X + Y =" << X << " + " << Y << "=" << Z << endl;
	Z = X - Y;
	cout << "X - Y =" << X << " - " << Y << "=" << Z << endl;
	mul = X * Y;
	cout << "X * Y =" << X << " * " << Y << "=" << mul << endl;
}

第六周作业:

假设某销售公司有一般员工、销售员工和销售经理。月工资的计算办法是:般员工月薪=基本工资;
销售员工月薪-基本工资+销售额x提成率;
销售经理月薪=基本工资+职务工资+销售额x提成率
编写程序,定义一个表示一般员工的基类 Employee,它包含三个表示员工基本信息的数据成员:编号number、姓名name和基本工资 basicSalary。由Employee 类派生表示销售员工的 Salesman类,Salesman类包含两个新数据成员:销售额sales和静态数据成员提成率commrate。
再由 Salesman 类派生表示销售经理的 Salesmanager 类。Salesmanager类包含新数据成员:职务工资jobSalary。
为这些类定义初始化数据的构造函数,以及输入数据input、计算工资 pay 和输出工资条 print的成员函数设一般员工的基本工资是2000元,销售经理的职务工资是3000 元,提成率=5/1000。在 main函数中输入若干个不同类型的员工信息测试你的类结构。

#include <iostream>
#include <string>
using namespace std;
class Employee
{
protected:
	int number;
	string name;
	double basicSalary;
public:
	Employee() :number(0), name("0"), basicSalary(2000)
	{

	}
	~Employee()
	{

	}
    void input()
	{
		int num;
		string n;
		cout << endl << "员工类型:一般员工" << endl;
		cout << "请依次输入员工编号,员工姓名" << endl;
		cin >> num >> n;
		number = num;
		name = n;
	}
	double pay()
	{
		double pay = 0;
		pay = basicSalary;
		return pay;
	}
 void print()
 {
	 cout << "employee's salary:" << pay() << endl;
 }
};
class Salesman :protected Employee
{
protected:
	double sales;
public:
	static double commrate;
	Salesman():sales(0)
	{

	}
	~Salesman()
	{

	}
	void input()
	{
		int num;
		string n;
		double sale;
		cout << endl << "员工类型:销售员工" << endl;
		cout << "请依次输入员工编号,员工姓名,销售额" << endl;
		cin >> num >> n >> sale;
		number = num;
		name = n;
		sales = sale;
	}
	double pay()
	{
		double pay = 0;
		pay = basicSalary + sales * commrate;
		return pay;
	}
	void print()
	{
		cout << "saleman's salary:" << pay() << endl;
	}
};
class Salesmanager :protected Salesman
{
protected:
	double jobSalary;
public:
	Salesmanager():jobSalary(3000)
	{

	}
	~Salesmanager()
	{

	}
	void input()
	{
		int num;
		string n;
		double sale;
		cout << endl << "员工类型:销售经理" << endl;
		cout << "请依次输入员工编号,员工姓名,销售额" << endl;
		cin >> num >> n >> sale;
		number = num;
		name = n;
		sales = sale;
	}
	double pay()
	{
		double pay = 0;
		pay = basicSalary + jobSalary + sales * commrate;
		return pay;
	}
	void print()
	{
		cout << "Salesmanager's salary:" << pay() << endl;
	}
};
double Salesman::commrate = 5.0 / 1000;
void UI()
{
	cout << endl;
	cout << "************************" << endl;
	cout << "1.创建一个一般员工" << endl;
	cout << "2.创建一个销售员工" << endl;
	cout << "3.创建一个销售经理" << endl;
	cout << "0.退出" << endl;
	cout << "************************" << endl;
	cout << endl;
}
int main()
{
	while (true)
	{
		UI();
		cout << "请输入操作选项:" << endl;
		int option;
		cin >> option;
		Employee* e = nullptr;
		Salesman* s = nullptr;
		Salesmanager* m = nullptr;
		if (option == 0)
		{
			break;
		}
		switch (option)
		{
		case 1:
			e = new Employee;
			e->input();
			e->print();
			delete e;
			break;
		case 2:
			s = new Salesman;
			s->input();
			s->print();
			delete s;
			break;
		case 3:
			m = new Salesmanager;
			m->input();
			m->print();
			delete m;
			break;
		}
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值