急求高手帮我编两道C++题目

来源:百度知道 编辑:UC知道 时间:2024/06/05 06:01:14
1. 定义一个Rectangle类,它包含两个数据成员length和width;以及包含用于求长方形面积的成员函数。再定义Rectangle的派生类Rectangular,它包含一个新数据成员height和用来求长方体体积的成员函数。在main函数中,使用两个类,求某个长方形的面积和某个长方体的体积。

2 . 使用成员函数编程序重载运算符“+”,使该运算符能实现两个字符串的连接。

3. 实现一个二维点(x,y)的Point类,类中包含一个构造函数,一个拷贝构造函数,一个求到原点(0,0)距离的函数norm()和一个输出坐标的函数print()。在main函数中,使用Point()类,输出一个点的坐标并求出该点到原点的距离。

#include<iostream>
#include<cmath>
using namespace std;
class Str{
private:
char str[120];

int length;
public:
Str &operator+(Str &s2);
void print()
{
cout<<str;
}
Str(const char *words)
{
int i;
for(i=0;words[i];i++)
str[i]=words[i];
str[i]='\0';
length=i;
}
};

int main()
{
int t;

char gh[50],gg[50];
cout<<"输入两句话\n";
cin>>gh>>gg;
Str ss1(gh),ss2(gg);
ss1+ss2;
ss1.print();
}

Str &Str::operator+(Str &s2)
{
int f;
for(f=0;f<=s2.length;f++)
{

str[length]=s2.str[f];
length+=1;
}
return *this;
}

#include<iostream>
#include<cmath>
using namespace std;
class Rectangle
{
int length,width;
public