赞
踩
1.对于类MyString,要求重载‘+’运算符后可以计算表达式:a=b+c;表示两个字符串连接。其中a,b,c都是类MyString的对象。
2.使用虚函数编写程序求球体和圆柱体的体积及表面积。由于球体和圆柱体都可以看作由圆继承而来,所以可以定义圆类Circle作为基类。在Circle类中定义一个数据成员radius和两个虚函数area()和volume()。由Circle类派生Sphere类和Column类。在派生类中对虚函数area()和volume()重新定义,分别求球体和圆柱体的体积及表面积。
重载相应的运算符并编写程序,能运用虚函数编写程序测试并提交程序。
#include <iostream> #include <cstring> #define N 32 using namespace std; class MyString { char *d; int len; public: MyString() { d = 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 * D(void) { return d; } friend ostream & operator << (ostream &os, MyString &a); }; MyString::MyString(const char *a) { d = new char[strlen(a) + 1]; strcpy(d, 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, d); strcat(tmp, a.D()); d = tmp; } else { strcat(d, a.D()); } 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, d); strcat(tmp, a); d = tmp; } else { strcat(d, 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, d); strcat(tmp, a); d = tmp; } else { strcat(d, a); } len += strlen(a); return *this; } MyString& MyString::operator = (MyString & a) { if (&a != this && len > 0 && len < a.strLen()) { delete[] d; d = new char[a.strLen() + 1]; } strcpy(d, a.D()); len = a.strLen(); return *this; } MyString& MyString::operator = (char * a) { if (d != NULL && len < strlen(a)) { delete[] d; d = new char[strlen(a) + 1]; } strcpy(d, a); len = strlen(a); return *this; } MyString& MyString::operator = (const char *a) { if (d != NULL && len < strlen(a)) { delete[] d; d = new char[strlen(a) + 1]; } strcpy(d, a); len = strlen(a); return *this; } ostream & operator << (ostream &os, MyString &a) { os << a.D(); return os; } int main(void) { MyString str("我从海边来 "); str = str + "深蓝的是天"; cout << str << endl; str = str + " 浅蓝的是水 灰蓝的是海岸线"; cout << str << endl; return 0; }
#include <iostream> #define P 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 * P * radius * radius; } virtual double volume(void) { return (4.0 / 3.0) * P * radius * radius * radius; } }; class Column :public Circle { private: double high; public: Column(double r = 0, double h = 0) { radius = r; high = h; } virtual double area(void) { if (0 == high) { return P * radius * radius; } return 2.0* P * radius * radius + P * 2.0 * radius * high; } virtual double volume(void) { if (0 == high) { return 0; } return P * radius * radius * high; } }; int main(void) { double rS, rC, h; while (1) { cout << "请输入球体的半径:"; cin >> rS; if (rS <= 0) { cout << "数据错误!\n\n"; } else break; } while (1) { cout << "请输入圆柱体的半径:"; cin >> rC; if (rC <= 0) { cout << "数据错误!\n\n"; } else break; } while (1) { cout << "请输入圆柱体的高:"; cin >> h; if (h <= 0) { cout << "数据错误!\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; return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。