C++题目x答案.doc
文本预览下载声明
设计一个程序: 定义一个圆类,有数据成员半径radis 半径),计算圆的面积和周长,写出主函数测试你编写的类。
#include
using namespace std;
float pi 3.14;
class R
public:
float radis;
float getMJ return radis*radis*pi;
float getZC return radis*2*pi;
R float in radis in;
;
void main R r 10 ;
cout 半径:10\n周长: r.getZC \n面积: r.getMJ ; 2编写一个程序。用名为max的函数模板计算三个参数中的最大值,用整数、字符和浮点数测试所编写的程序。
#include
using namespace std;
template
O Max O a,O b, O c return a b?a c?a:c:b c?b:c;
void main cout Max 9,1,8 endl;
cout Max 7.0,3.2,9.9 endl;
cout Max a,z,b ; 设计一个立方体类Box,它能计算并输出立方体的体积和表面积。
#include
using namespace std;
class Box
public:
float L;
float getBMJ return L*L*6;
float getTJ return L*L;
Box float in L in;
;
void main Box r 10 ;
cout 边长:10\n表面积: r.getBMJ \n体积: r.getTJ ; a 抽象出一个基类base;
#include
using namespace std;
class base
public:
virtual float getMJ return H*W;
float H,W;
;
class R:public base
public:
float getMJ return H*H*3.14;
R float in H in;
;
class A:public base
public:
float getMJ return H*W /2;
A float in_H,float in_w H in_H;W in_w;
;
class S:public base
public:
float getMJ return H*H;
S float in H in;
;
void main R r 10 ;
A a 10,5 ;
S s 10 ;
cout 圆:边长:10\n面积: r.getMJ endl \n三角:高:10,底:5\n面积: a.getMJ endl \n正方形:边长:10\n面积: s.getMJ ; 5、定义一个处理日期的类TDate,它有3个私有数据成员:Month,Day,Year和若干个公有成员函数,并实现如下要求:①构造函数重载;②成员函数设置缺省参数;③定义一个友元函数来打印日期;④定义一个非静态成员函数设置日期;⑤可使用不同的构造函数来创建不同的对象。
include
using namespace std;
class TDate public:
TDate :Year 1900 ,Month 1 ,Day 1 ;
TDate int Y, int M 1, int D 1 Month M;Day D;Year Y;
void set int Y 1990, int M 1, int D 1 Month M;Day D;Year Y;
friend void show TDate in ;
private:
int Month,Day,Year;
;
void show TDate in cout in.Year 年 in.Month 月 in.Day 日\n;
void main TDate t1;
TDate t2 2014 ;
TDate t3 2015,6,5 ;
show t1 ;
show t2 ;
show t3 ;
t3.set 1999 ;
show t3 ; 6、编程实现抽象类Employee,派生类Manger和HourlyWorker,Employee有数据成员姓名name和工号ID,Manger有数据成员sal,代表经理的月工资,HourlyWorker有wage和h
显示全部