c++編程題參考模板_第1頁
c++編程題參考模板_第2頁
c++編程題參考模板_第3頁
c++編程題參考模板_第4頁
c++編程題參考模板_第5頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、4.1測試一個名為rectangle的矩形類,其屬性為矩形的左下角與右上角兩個點的坐標,能計算矩形的面積。解: 源程序:#include <iostream.h>class Rectanglepublic: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()

2、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

3、 left, int bottom, int right)itsTop = top;itsLeft = left;itsBottom = bottom;itsRight = right;int Rectangle:GetArea() constint Width = itsRight-itsLeft;int Height = itsTop - itsBottom;return (Width * Height);int main()1 / 11Rectangle MyRectangle (100, 20, 50, 80 );int Area = MyRectangle.GetArea();cou

4、t << "Area: " << Area << "n"return 0;程序運行輸出:Area: 3000Upper Left X Coordinate: 204.2設(shè)計一個程序。 設(shè)計一個立方體類Box,它能計算并輸出立方體的體積和表面積。#include <iostream>using namespace std;   class Box  public:  float L;

5、0;float getBMJ()return L*L*6;  float getTJ()return L*L;  Box(float in)L=in;      void main()      Box r(10);   cout<<"邊長:10n表面積:"<<r.getBMJ()<<

6、;"n體積:"<<r.getTJ();  4.3設(shè)計一個汽車類vehicle,包含的數(shù)據(jù)成員有車輪個數(shù)wheels和車重weight。小車類car是它的派生類,其中包含載人數(shù)passenger_load。每個類都有相關(guān)數(shù)據(jù)的輸出方法。在主程序中定義一個car類對象,對其車輪個數(shù)、車重、載人數(shù)進行設(shè)置并顯示。#include <iostream.h>#include <iomanip.h>class vehicle  /汽車類,包含車輪數(shù)和車重 public:  vehi

7、cle(int, float);  int get_wheels();  float get_weight();  void show(); protected:  int wheels;    /車輪數(shù)  float weight;   /車重量,單位噸;class car:private vehicle  /小車類是汽車類的私有派生類,包含載客數(shù) public:  

8、;car(int wheels, float weight, int passengers);  int get_passengers();  void show(); private:  int passenger_load;  /額定載客數(shù);class truck:private vehicle  /卡車類是汽車類的私有派生類,包含載人數(shù)和載重量 public:  truck(int wheels,float weight,int passengers

9、,float max_load);  int get_passengers();  void show(); private:  int passenger_load;  /額定載人數(shù)  float payload;   /額定載重量,單位噸;vehicle:vehicle(int wl,float wt) wheels=wl; weight=wt;int vehicle:get_wheels() return wheels;

10、float vehicle:get_weight() return weight;void vehicle:show() cout<<"車輪數(shù):"<<setw(3)<<wheels<<" 個n車身重:"<<setw(3)<<weight<<" 噸n"car:car(int wheels, float weight, int passengers):vehicle(wheels, weight) passenger_load

11、=passengers;int car:get_passengers () return passenger_load;void car:show() cout<<"車  型:小汽車n" vehicle:show(); cout<<"載客數(shù):"<<setw(3)<<passenger_load<<" 人nn"truck:truck(int wheels, float weight, int passengers, float

12、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<<"載人數(shù):"<<setw(3)<<passenger_load<

13、<" 人n" cout<<"載重量:"<<setw(3)<<payload<<" 噸nn"void main () cout<<"私有派生類相關(guān)驗證程序  Microsoft Visual C+構(gòu)建n"  <<setw(45)<<"(C) 2009/10/28 郭呈祥nn" car car(4, 3.6, 5);   

14、  /小車類參數(shù)分別為“車輪數(shù)、車身重以及額定載客數(shù)” truck tru(10, 10.0, 3, 35);   /卡車類參數(shù)分別為“車輪數(shù)、車身重、載人數(shù)以及額定載重量” cout<<"數(shù)據(jù)驗證結(jié)果如下:n" car.show(); tru.show();4.4定義一個日期類Date,包含年、月、日三個數(shù)據(jù)成員,以及一個求第二天日期的成員函數(shù)和輸出日期的成員函數(shù)。#include <iostream.h> class Date private:int yea

15、r,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 totaldays212=31,28,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31;da

16、y+;int leap=(year%400=0|year%4=0&&year%100!=0);if(day>totaldaysleapmonth-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<<&q

17、uot;明天是:"d1.display(); 4.5設(shè)計一個程序。定義一個圓類,有數(shù)據(jù)成員半徑radis(半徑),計算圓的面積和周長,寫出主函數(shù)測試你編寫的類。#include <iostream>  using namespace std;  float pi = 3.14;  class R  public: float radis;  float getMJ()retu

18、rn radis*radis*pi;  float getZC()return radis*2*pi;  R(float in)radis=in;     void main()      R r(10);   cout<<"半徑:10n周長:"<<r.getZC()<<"n面積:"

19、<<r.getMJ();  4.6 編寫一個程序。用名為max的函數(shù)模板計算三個參數(shù)中的最大值,用整數(shù)、字符和浮點數(shù)測試所編寫的程序。 #include <iostream>  using namespace std;  template <typename O>  O Max(O a,O b, O c)return a>b?a>c?a:c:b>

20、;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');  4.7編寫一個程序計算“三角形、正方形、圓形”三種圖形的面積。要求:a)抽象出一個基類base;b)在其

21、中說明一個虛函數(shù)用來求面積;c)利用派生類定義“三角形、正方形、圓形”;d)編寫主函數(shù)并測試。#include <iostream>  using namespace std; class base public: virtual float getMJ()return H*W; float H,W;   class R:public base  public: &

22、#160;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

23、0;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<<"圓:邊長:10n面積:"<<r.getM

24、J()<<endl  <<"n三角:高:10,底:5n面積:"<<a.getMJ()<<endl 35.   <<"n正方形:邊長:10n面積:"<<s.getMJ(); 4.8編程實現(xiàn)抽象類Employee,派生類Manger和HourlyWorker,Employee有數(shù)據(jù)成員姓名name和工號ID,Manger有數(shù)據(jù)成員sal,代表經(jīng)理的月工資,HourlyWorker有wage和hours,分別代表鐘點工的

25、每小時的工資數(shù)和月工作時數(shù),定義的所有類中必須包含構(gòu)造函數(shù)、析構(gòu)函數(shù)、修改和獲取所有數(shù)據(jù)成員的成員函數(shù),以及虛函數(shù)來計算職員的工資、輸出職員的姓名name和工號ID。(#include <iostream> #include <string> using namespace std;  class Employee   public:  string name;  int id; &

26、#160;virtual int getSal()return 0;  Employee():name("未命名"),id(0);   Employee()cout<<"析構(gòu)n"  void set(string N,int I)id=I; name=N;  void showSal()      cout<&

27、lt;"n姓名:"<<name<<endl    <<"工號:"<<id<<endl    <<"工資:"<<getSal()<<endl;        class Manger:public Employee  public: M

28、anger(int S)sal=S;  int getSal()return sal;  int sal;   class HourlyWorker:public Employee   public:  HourlyWorker(int W,int H)wage=W;hours=H; int getSal()return wage*hours; int

29、60;wage,hours;     void main()   HourlyWorker h(10,20);  h.set("小時工A",777);  h.showSal();   Manger m(999); m.set("經(jīng)理A",888); m.showSal(); 4.9定義一個處理日期的類TDate,它有3個私有數(shù)據(jù)成員:Month,D

30、ay,Year和若干個公有成員函數(shù),并實現(xiàn)如下要求:構(gòu)造函數(shù)重載;成員函數(shù)設(shè)置缺省參數(shù);定義一個友元函數(shù)來打印日期;定義一個非靜態(tài)成員函數(shù)設(shè)置日期;可使用不同的構(gòu)造函數(shù)來創(chuàng)建不同的對象。include <iostream> using namespace std;   class TDate   public: TDate():Year(1900),Month(1),Day(1); TDate(int Y, int M=1,

31、 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; riend void show(TDate& in); private:  int Month,Day,Year;    void show(TDate& in)cout<<in.Year<<"年"

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論