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

1. 定义一个Rectangle类,它包含两个数据成员length和width;以及包含用于求长方形面积的成员函数。再定义Rectangle的派生类Rectangular,它包含一个新数据成员height和用来求长方体体积的成员函数。在main函数中,使用两个类,求某个长方形的面积和某个长方体的体积。

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

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

第1个回答  2008-12-24
#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:
void input();
int S()
{
return length *width;
}
};
class Rectangular: public Rectangle
{
int height;
public:
void input()
{
Rectangle::input();
cout<<"输入高\n";
cin>>height;
}
int V()
{
return S()*height;
}
};
int main()
{
int t;
Rectangular R;
R.input();
cout<<"面积"<<R.S()<<endl;
cout<<"体积"<<R.V()<<endl;
}
void Rectangle::input()
{
cout<<"输入长\n";
cin>>length;
cout<<"输入宽\n";
cin>>width;
}
第一全
尘封梦想 20:09:13
#include<iostream>
#include<cmath>
using namespace std;
class Point
{
int x,y;
public:
Point(int a,int b):x(a),y(b)
{};
Point(const Point &s)
{
x=s.x;y=s.y;
}
float norm()
{
return sqrt(x *x+y*y);
}
void print()
{
cout<<"x为"<<x<<" y为"<<y<<endl;
}
};
int main()
{
Point p(23,15),ss(p);
cout<<"原点距离"<<ss.norm()<<endl;
ss.print();
}

尘封梦想 20:09:18
第三个
尘封梦想 20:10:26
第二个 #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;
}
};
相似回答