【C++---06】实验报告: 运算符重载和虚函数

 

内容提要

  1. 对于类MyString,要求重载‘+’运算符后可以计算表达式:a=b+c;表示两个字符串连接。
  2. 其中a,b,c都是类MyString的对象。
  3. 使用虚函数编写程序求球体和圆柱体的体积及表面积。
  4. 由于球体和圆柱体都可以看作由圆继承而来,
  5. 所以可以定义圆类Circle作为基类。
  6. 在Circle类中定义一个数据成员radius和两个虚函数area()和volume()。
  7. 由Circle类派生Sphere类和Column类。
  8. 在派生类中对虚函数area()和volume()重新定义,分别求球体和圆柱体的体积及表面积。
  9. 基本要求重载相应的运算符并编写程序,能运用虚函数编写程序测试并提交程序。

 

运算符重载:

#pragma warning(disable:4996)
#include <iostream>
#include <cstring>
#define N 32

using namespace std;

class MyString
{
	char *data;
	int len;
public:
	MyString() { data = new char[N]; len = 0; }
	MyString(const char* a);
	MyString& operator + (MyString & a);
	MyString& operator + (char * a);
	MyString& operator + (const char *a);

	MyString& operator = (MyString & a);
	MyString& operator = (char * a);
	MyString& operator = (const char *a);

	int strLen(void) { return len; }
	char * Data(void) { return data; }
	friend ostream & operator << (ostream &os, MyString &a);
	//friend istream & operator >> (istream &is, MyString &a);
};

MyString::MyString(const char *a)
{
	data = new char[strlen(a) + 1];
	strcpy(data, a);
	len = strlen(a);
}

MyString& MyString::operator + (MyString & a)
{
	if (len + a.strLen() > (N - 1))
	{
		//分配一段新的控件并将两个字符串拼接起来
		char *tmp = new char[len + a.strLen() + 1];
		strcpy(tmp, data);
		strcat(tmp, a.Data());
		data = tmp;
	}
	else
	{
		strcat(data, a.Data());
	}
	len += a.strLen();
	return *this;
}

MyString& MyString::operator + (char * a)
{
	if (len + strlen(a) > (N - 1))
	{
		char *tmp = new char[len + strlen(a) + 1];
		strcpy(tmp, data);
		strcat(tmp, a);
		data = tmp;
	}
	else
	{
		strcat(data, a);
	}
	len += strlen(a);
	return *this;
}

MyString& MyString::operator + (const char *a)
{
	if (len + strlen(a) > (N - 1))
	{
		char *tmp = new char[len + strlen(a) + 1];
		strcpy(tmp, data);
		strcat(tmp, a);
		data = tmp;
	}
	else
	{
		strcat(data, a);
	}
	len += strlen(a);
	return *this;
}

MyString& MyString::operator = (MyString & a)
{
	if (&a != this && len > 0 && len < a.strLen())
	{
		delete[] data;
		data = new char[a.strLen() + 1];
	}
	strcpy(data, a.Data());
	len = a.strLen();
	return *this;
}

MyString& MyString::operator = (char * a)
{
	if (data != nullptr && len < strlen(a))
	{
		delete[] data;
		data = new char[strlen(a) + 1];
	}
	strcpy(data, a);
	len = strlen(a);
	return *this;
}



MyString& MyString::operator = (const char *a)
{
	if (data != nullptr && len < strlen(a))
	{
		delete[] data;
		data = new char[strlen(a) + 1];
	}
	strcpy(data, a);
	len = strlen(a);
	return *this;
}

ostream & operator << (ostream &os, MyString &a)
{
	os << a.Data();
	return os;
}



int main(void)
{
	MyString str("清风朗月 ");
	str = str + "辙思玄度";
	cout << str << endl;

	str = str + ".我与春风皆过客,谁共明月赴长生.";
	cout << str << endl;

	str = "你只见草木皆兵,我却看浩瀚苍穹";
	cout << str << endl;

	char tmp[] = "星河滚烫,你是人间理想;";
	str = tmp;

	cout << str << endl;

	system("pause");
	return 0;
}

虚函数的代码:

#include <iostream>
#define PIE 3.1415

using namespace std;

//圆,接口类
class Circle
{
protected:
	double radius;
public:
	//表面积
	virtual double area(void) = 0;
	//体积
	virtual double volume(void) = 0;
};

//球
class Sphere :public Circle
{
public:
	Sphere(double r = 0) { radius = r; }
	//表面积
	virtual double area(void)
	{
		return 4.0 * PIE * radius * radius;
	}
	//体积
	virtual double volume(void)
	{
		return (4.0 / 3.0) * PIE * radius * radius * radius;
	}
};

//圆柱
class Column :public Circle
{
private:
	double height;
public:
	Column(double r = 0, double h = 0) { radius = r; height = h; }
	//表面积
	virtual double area(void)
	{
		if (0 == height)
		{
			//底面积
			return PIE * radius * radius;
		}
		return 2.0* PIE * radius * radius + PIE * 2.0 * radius * height;
	}
	//体积
	virtual double volume(void)
	{
		if (0 == height)
		{
			return 0;
		}
		return PIE * radius * radius * height;
	}
};


int main(void)
{
	double rS, rC, h;
	while (true)
	{
		cout << "请输入球体的半径:";
		cin >> rS;
		if (rS <= 0)
		{
			cout << "请输入大于0的数据!\n\n";
		}
		else
			break;
	}

	while (true)
	{
		cout << "请输入圆柱体的半径:";
		cin >> rC;
		if (rC <= 0)
		{
			cout << "请输入大于0的数据!\n\n";
		}
		else
			break;
	}

	while (true)
	{
		cout << "请输入圆柱体的高:";
		cin >> h;
		if (h <= 0)
		{
			cout << "请输入大于0的数据!\n\n";
		}
		else
			break;
	}

	Sphere sphere(rS);
	Column column(rC, h);
	cout << "球体的半径为:" << rS << endl
		<< "球体的表面积为:" << sphere.area() << endl
		<< "球体的体积为:" << sphere.volume() << endl << endl;
	cout << "圆柱体的半径为:" << rC << endl
		<< "圆柱体的表面积为:" << column.area() << endl
		<< "圆柱体的体积为:" << column.volume() << endl << endl;

	system("pause");
	return 0;
}

 

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值