1.設計乙個類rect,要求如下:
(1)該類中的私有資料成員length,width存放它的長和寬,並且設定它們的預設值是0。
(2)通過成員函式設定其長和寬,並確保長和寬都在(0,50)範圍之內。
(3)實現求周長函式getperimeter()。
解:程式設計如下:
#include
using namespace std;
class rect
rect(rect&r):length(
~rect(){}
rect input();
float getperimeter();
void show();
};rect rect::input()while(l<=0||l>=50);
dowhile(w<=0||w>=50);
return rect(l,w);
}void rect::show()
float rect::getperimeter()
int main()
2.定義乙個二維座標類vector2d, 二個資料成員為double型x, y 為private屬性。定義代二個引數的建構函式和乙個show( ) 函式用以輸出x, y的值, 另外作為成員函式過載的運算苻」+」的功能是將此類二個物件的資料成員x和y對應相加。這些成員函式的屬性均為public.
請用c++編寫此程式, 並編寫測試程式進行測試
解:程式如下:
#include
using namespace std;
class vector2d
vector2d(vector2d&v):x(
void show();
vector2d operator+(vector2d v);
~vector2d(){}
};void vector2d::show()
vector2d vector2d::operator+(vector2d v)
3. 程式設計實現抽象類employee,派生類manger和hourlyworker,employee有資料成員姓名name和工號id,manger有資料成員sal,代表經理的月工資,hourlyworker有wage和hours,分別代表鐘點工的每小時的工資數和月工作時數,定義的所有類中必須包含建構函式、析構函式、修改和獲取所有資料成員的成員函式,以及虛函式來計算職員的工資、輸出職員的姓名name和工號id。
解:程式設計如下:
#include
#include
using namespace std;
class employee
employee(){}
~employee(){}
virtual float countpays()
virtual void show(){}
string getname()
string getid()
void changen(string n)
void changei(string id)
employee input();
employee set();
};employee employee::input()
employee employee::set()
class manger:public employee
manger():employee(){}
~manger(){}
float countpays();
void show();
manger input();
manger set();
};float manger::countpays()
void manger::show()
manger manger::input()
manger manger::set()
class hourlyworker:public employee
hourlyworker():employee(){}
~hourlyworker(){}
float countpays();
void show();
hourlyworker input();
hourlyworker set();
};float hourlyworker::countpays()
void hourlyworker::show()
hourlyworker hourlyworker::input()
hourlyworker hourlyworker::set()
int main()
4. 編寫乙個函式模板,用於求引數的絕對值,並編寫測試程式進行測試。
解:程式設計如下:
#include
using namespace std;
template
t abs(t x)
int main()
5. 設計乙個類dateinfo,要求其滿足下述要求:
(1)要求有乙個無參的建構函式,其初始的年、月、日分別為:2010,8,8。
(2)要求有乙個帶引數的建構函式,其引數分別對應年、月、日。
(3)要求用乙個成員函式實現日期的設定。
(4)要求用乙個成員函式實現輸出日期。
請用c++編寫此程式, 並編寫測試程式進行測試。
解:程式設計如下:
#include
C綜合程式設計題上機實踐指導
c語言專案設計指導 一 題目 通訊錄管理系統 二 目的 通過此次綜合程式設計實踐,使學生進一步熟悉和掌握c語言的基本知識 基本程式設計原理 學習用c語言開發應用程式的方法 進一步熟悉計算機程式設計的基本方法 步驟,培養學生設計 除錯 測試程式能力。三 預備知識 1 熟練掌握c語言的基本知識和使用c語...
結構體與鍊錶程式設計題及解答
程式1 題目 編寫input 和output 函式輸入,輸出5個學生的資料記錄。用結構體設計,學生記錄中包括學號 姓名 四門課程成績 程式源 include define n 5 struct student stu n void input struct student stu void prin...
實驗4 迴圈結構程式設計1題目及解答
4.3 迴圈結構程式設計1 1 題中頁碼均為 c語言程式設計教程 中的頁碼 2 題後為原程式檔名 3 僅提交名為 c 的源程式檔案,可一起提交,也可逐題提交 1 按照右邊給出的流程圖用while語句求1 100之間的偶數之和。007張三t411.c,007張三t412.c 提示 參見p89 例4.1...