C 程式設計課後習題及解答(第八章)

2021-07-09 06:56:55 字數 1310 閱讀 1982

(1).定義乙個point類,對其過載++、­- -、<<、>>運算子,並在主函式中對其進行測試。 答:

#includeusing namespacestd;

class point

point& operator++(); //前置單目運算,點的x,y座標都發生變化

point& operator--(); //前置單目運算,點的x,y座標都發生變化

friend ostream &operator<<(ostream &out,const point &p); //運算子《過載

friend istream &operator>>(istream &in,point &p); //運算子》過載

public:

int x,y;};

point&point::operator ++ ()

point&point::operator -- ()

ostream &operator<<(ostream &out,const point &p)

int main()

; private:

intx,y;};

class point

; point(intx,int y):x(x),y(y)

point(point&r)

intget_x() const ;

intget_y() const ;

point&operator=(point &t)

private:

intx,y;};

class rectangle:public shape

; rectangle(point&p1, point &p2)

double get_area() const; //要和父類的宣告一致

double get_perim() const; //呼叫此函式的display是const 這裡也要定義為const

voiddisplay()const; //覆蓋基類的虛函式

private:

pointpoint1,point2;

} ;

double rectangle::get_area() const

double rectangle::get_perim() const

void rectangle::display() const

第八章 課後習題

1.掌握格式化的輸入輸出方法。2.熟悉系統提供的輸入操作函式。3.掌握磁碟檔案的輸入輸出方法。1.程式sy9 1.cpp用以列印表中的資料,但程式中存在邏輯錯誤。上機除錯後寫出正確的 原程式如下 修改後的程式如下 sy9 1.cpp include include using namespace s...

C Primer 課後習題第八章

第一章 第二章第三章 第四章第五章 第六章第七章 include includeusing namespace std istream f istream in in.close 輸入完畢,關閉檔案 vector const iterator it words.begin 迭代器 while it ...

第八章 習題

一 填空題 1 在c 的輸入輸出系統中,最核心的物件是 流 執行輸入和輸出操作的類體系叫做 流類 2 當實際進i o操作時,cin與 標準輸入裝置相關聯。3 c 的流類庫預定義了4個流,它們是 cin cout cerr 和 clog 4 使用檔案流類庫的程式必須用 include編譯指令將標頭檔案...