答:
第一题
class Rect
{
public:
Rect() {_length=0; _width=0;}
Rect(double length, double width) {_length=length; _width=width;}
void setSize(double length, double width) {_length=length; _width=width;}
double getArea() {return _length*_width;}
double getPerimeter() {return 2*(_length+_width);}
private:
double _length;
double _width;
};
第二题
class Rect
{
public:
Rect() {_length=0; _width=0;}
Rect(double length, double width) {_length=length; _width=width;}
void setSize(double length, double width) {_length=length; _width=width;}
double getArea() {return _length*_width;}
double getPerimeter() {return 2*(_length+_width);}
private:
double _length;
double _width;
};
class Cubic : public Rect
{
public:
Cubic():Rect() {_height=0;}
Cubic(double length, double width, double height):Rect(length, width), _height(height) {}
void setSize(double length, double width, double height) {Rect::setSize(length, width); _height=height;}
void setHeight(double height) {_height=height;}
double getVolume() {return Rect::getArea() * _height;}
private:
double _height;
};
第三题
void copyFile(const char* src_file, const char* dst_file) {
FILE *src_fd=fopen(src_file, "r");
FILE *dst_fd=fopen(dst_file, "w");
char buf[1024]={0};
while(fgets(buf, sizeof buf, src_fd) != NULL){
fputs(buf, dst_fd);
memset(buf, 0, sizeof buf);
}
fclose(src_fd);
fclose(dst_fd);
}
第四题
class Line
{
public:
Line() {}
void setStartPoint(double x, double y) {_sp.first=x; _sp.second=y;}
void setEndPoint(double x, double y) {_ep.first=x; _ep.second=y;}
double getLength() {return sqrt(pow(abs(_sp.first-_ep.first), 2) + pow(abs(_sp.second-_ep.second), 2));}
private:
pair<double, double> _sp;
pair<double, double> _ep;
};
第五题
const double pi = 3.1415926535897932384626;
class shape
{
public:
virtual double getVolume() = 0;
virtual double getSurfaceArea() = 0;
};
class sphere: public shape
{
public:
sphere(double r):_r(r) {}
double getVolume() {return (4/3) * pi * pow(_r, 3);}
double getSurfaceArea() {return 4 * pi * pow(_r, 2);}
private:
double _r;
};
class column: public shape
{
public:
column(double r, double h):_r(r), _h(h) {}
double getVolume() {return pi * pow(_r, 2) * _h;}
double getSurfaceArea() {return 2 * pi * pow(_r, 2) + 2 * pi * _r * _h;}
private:
double _r;
double _h;
};
测试
#include <iostream>
#include <cstdio>
#include <cstring>
#include <utility>
#include <cstdlib>
#include <cmath>
using namespace std;
int main()
{
# 测试第一题
Rect r1(2, 5);
cout << "rect1 area:" << r1.getArea() << endl;
cout << "rect1 perimeter:" << r1.getPerimeter() << endl;
Rect r2;
r2.setSize(3, 6);
cout << "rect2 area:" << r2.getArea() << endl;
cout << "rect2 perimeter:" << r2.getPerimeter() << endl;
# 测试第二题
Cubic c1(2, 5, 3);
cout << "cubic1 volume:" << c1.getVolume() << endl;
Cubic c2;
c2.setSize(3, 6, 10);
cout << "cubic2 volume:" << c2.getVolume() << endl;
# 测试第三题
copyFile("1.txt", "2.txt");
# 测试第四题
Line l;
l.setStartPoint(2, 2);
l.setEndPoint(4, -4);
cout << "line length:" << l.getLength() << endl;
# 测试第五题
sphere s(5);
cout << "sphere volume:" << s.getVolume() << " surface area:" << s.getSurfaceArea() << endl;
column c(4, 10);
cout << "column volume:" << c.getVolume() << " surface area:" << c.getSurfaceArea() << endl;
}
测试结果:
-> g++ -Wall test.cpp -o test
-> ./test
rect1 area:10
rect1 perimeter:14
rect2 area:18
rect2 perimeter:18
cubic1 volume:30
cubic2 volume:180
line length:6.32456
sphere volume:392.699 surface area:314.159
column volume:502.655 surface area:351.858