C++程序设计2018年10月考试设计题代码参考

设计一个圆类Circle和一个桌子类Table,类Circle包含私有数据成员半径radius和求圆面积的成员函数getarea(),类Table包含私有数据成员高度height和成员函数getheight(),另设计一个圆桌类Roundtable类Circle和类Table两个类派生,私有数据成员color和成员函数getcolor(),要求输出一个圆桌的面积、高度和颜色等数据.
 

VC++6.0解题方法代码: 

#include <iostream>
#include <string>
using namespace std;
const double PI = 3.14159;
class Circle
{
private:
    double radius;
public:
    Circle(double r){ radius = r;}
    double getarea(){return radius * radius * PI;}
};
class Table
{
private:
    double height;
public:
    Table(double h){ height = h;}
    double getheight(){ return height; }
};
class Roundtable:public Circle,public Table
{
private:
    char color[8];
public:
    Roundtable(double r,double h,char *c):Circle(r),Table(h)
    { strcpy(color,c);}
    char* getcolor(){return color;}
};
int main()
{
    Roundtable rt(10.0,1.8,"黑色");
    cout << rt.getarea()   << endl;
    cout << rt.getheight() << endl;
    cout << rt.getcolor()  << endl;
    return 0;
}

输出结果: 

314.159
1.8
黑色

VS code 解题方法代码:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
using namespace std;
const double PI = 3.14159;
class Circle
{
private:
    double radius;
public:
    Circle(double r){ radius = r;}
    double getarea(){return radius * radius * PI;}
};
class Table
{
private:
    double height;
public:
    Table(double h){ height = h;}
    double getheight(){ return height; }
};
class Roundtable:public Circle,public Table
{
private:
    char color[8];
public:
    Roundtable(double r,double h,const char *c):Circle(r),Table(h)
    { strcpy(color,c);}
    char* getcolor(){return color;}
};
int main()
{
    Roundtable rt(10.0,1.8,"黑色");
    cout << rt.getarea()   << endl;
    cout << rt.getheight() << endl;
    cout << rt.getcolor()  << endl;
    return 0;
}

VS 2017解题方法代码:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;
const double PI = 3.14159;
class Circle
{
private:
	double radius;
public:
	Circle(double r) { radius = r; }
	double getarea() { return radius * radius * PI; }
};
class Table
{
private:
	double height;
public:
	Table(double h) { height = h; }
	double getheight() { return height; }
};
class Roundtable :public Circle, public Table
{
private:
	string color;
public:
	Roundtable(double r, double h, string c) :Circle(r), Table(h)
	{
		color = c;
	}
	string getcolor() { return color; }
	friend ostream &operator << (ostream &,Roundtable);		//使用string类需要运算符重载
};
ostream &operator << (ostream &stream,Roundtable shuchu)
{
	stream << shuchu.color ;
	return stream;
}
int main()
{
	Roundtable rt(10.0, 1.8, "黑色");
	cout << rt.getarea()   << endl;
	cout << rt.getheight() << endl;
	cout << rt.getcolor()  << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37592750/article/details/83317414