c++编程题

  • 格式:doc
  • 大小:37.50 KB
  • 文档页数:10

4.1测试一个名为rectangle的矩形类,其属性为矩形的左下角与右上角两个点的坐标,能计算矩形的面积。

解:源程序:#include <iostream.h>class Rectangle{public:Rectangle (int top, int left, int bottom, int right);~Rectangle () {}int GetTop() const { return itsTop; }int GetLeft() const { return itsLeft; }int GetBottom() const { return itsBottom; }int GetRight() const { return itsRight; }void SetTop(int top) { itsTop = top; }void SetLeft (int left) { itsLeft = left; }void SetBottom (int bottom) { itsBottom = bottom; }void SetRight (int right) { itsRight = right; }int GetArea() const;private:int itsTop;int itsLeft;int itsBottom;int itsRight;};Rectangle::Rectangle(int top, int left, int bottom, int right){itsTop = top;itsLeft = left;itsBottom = bottom;itsRight = right;}int Rectangle::GetArea() const{int Width = itsRight-itsLeft;int Height = itsTop - itsBottom;return (Width * Height);}int main(){Rectangle MyRectangle (100, 20, 50, 80 );int Area = MyRectangle.GetArea();cout << "Area: " << Area << "\n";return 0;}程序运行输出:Area: 3000Upper Left X Coordinate: 204.2设计一个程序。

设计一个立方体类Box,它能计算并输出立方体的体积和表面积。

#include <iostream>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();}4.3设计一个汽车类vehicle,包含的数据成员有车轮个数wheels和车重weight。

小车类car是它的派生类,其中包含载人数passenger_load。

每个类都有相关数据的输出方法。

在主程序中定义一个car类对象,对其车轮个数、车重、载人数进行设置并显示。

#include <iostream.h>#include <iomanip.h>class vehicle //汽车类,包含车轮数和车重{public:vehicle(int, float);int get_wheels();float get_weight();void show();protected:int wheels; //车轮数float weight; //车重量,单位吨};class car:private vehicle //小车类是汽车类的私有派生类,包含载客数{public:car(int wheels, float weight, int passengers);int get_passengers();void show();private:int passenger_load; //额定载客数};class truck:private vehicle //卡车类是汽车类的私有派生类,包含载人数和载重量{public:truck(int wheels,float weight,int passengers,float max_load);int get_passengers();void show();private:int passenger_load; //额定载人数float payload; //额定载重量,单位吨};vehicle::vehicle(int wl,float wt){wheels=wl;weight=wt;}int vehicle::get_wheels(){return wheels;}float vehicle::get_weight(){return weight;}void vehicle::show(){cout<<"车轮数:"<<setw(3)<<wheels<<" 个\n车身重:"<<setw(3)<<weight<<" 吨\n";}car::car(int wheels, float weight, int passengers):vehicle(wheels, weight){passenger_load=passengers;}int car::get_passengers (){return passenger_load;}void car::show(){cout<<"车型:小汽车\n";vehicle::show();cout<<"载客数:"<<setw(3)<<passenger_load<<" 人\n\n";}truck::truck(int wheels, float weight, int passengers, float max_load):vehicle(wheels, weight){passenger_load=passengers;payload=max_load;}int truck::get_passengers(){return passenger_load;}void truck::show(){cout <<"车型:大卡车\n";vehicle::show();cout<<"载人数:"<<setw(3)<<passenger_load<<" 人\n";cout<<"载重量:"<<setw(3)<<payload<<" 吨\n\n";}void main (){cout<<"私有派生类相关验证程序Microsoft Visual C++构建\n"<<setw(45)<<"(C) 2009/10/28 郭呈祥\n\n";car car(4, 3.6, 5); //小车类参数分别为“车轮数、车身重以及额定载客数”truck tru(10, 10.0, 3, 35); //卡车类参数分别为“车轮数、车身重、载人数以及额定载重量”cout<<"数据验证结果如下:\n";car.show();tru.show();}4.4定义一个日期类Date,包含年、月、日三个数据成员,以及一个求第二天日期的成员函数和输出日期的成员函数。

#include <iostream.h>class Date{private:int year,month,day;public:Date(int y, int m, int d){year=y;month=m;day=d;}void nextday();void display(){cout<<year<<"/"<<month<<"/"<<day<<endl;}};void Date::nextday(){int totaldays[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};day++;int leap=(year%400==0||year%4==0&&year%100!=0);if(day>totaldays[leap][month-1]){day=1; month++;if(month>12){month=1;year++;}}}void main(){int d,m,y;cout<<"请输入年、月、日:\n";cin>>y>>m>>d;Date d1(y,m,d);cout<<"今天是:";d1.display();d1.nextday();cout<<"明天是:";d1.display();}4.5设计一个程序。

定义一个圆类,有数据成员半径radis(半径),计算圆的面积和周长,写出主函数测试你编写的类。

#include <iostream>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();}4.6 编写一个程序。