22 從Point繼承的Rectangle

2021-10-19 09:31:32 字數 1783 閱讀 8798

實驗目的:學習繼承的程式設計方法

實驗內容:

定義乙個point類,包括:

兩個私有資料成員:int x, int y,它們分別表示乙個點的x和y座標。

建構函式:

point(int x, int y),即傳兩個引數,構造乙個點物件。

注意,本題要求point類不得定義預設建構函式,也就是只定義以上所說的建構函式。

成員函式:

move(int dx, int dy),將point物件移動dx,dy的距離。 

定義乙個rectangle類,繼承point類,基類物件的x和y表示長方形左上角的座標。

rectangle類包括:

兩個私有資料成員:int width, int height,它們分別表示長方形的橫向的寬度和縱向的高度。

建構函式:

rectangle(int x, int y, int width, int height)。

成員函式:

int getarea ()    //獲取該圖形的面積

bool isin(point p)   //判斷傳入的點是否在該圖形之內(不包括邊界),如果在內部返回true,否則返回false

使用main函式測試以上getarea方法和isin方法。main函式使用如下**:

int main()

int topleftx, toplefty, width, height;

int px, py, dx, dy;

cin>>topleftx>>toplefty>>width>>height;

cin>>px>>py;

cin>>dx>>dy;

point p(px, py);

rectangle r(topleftx, toplefty, width, height);

coutcout<<"in"cout<<"not in"if(r.isin(p))

cout<<"in"cout<<"not in"第一行輸入長方形r的資訊,包括左上角頂點x座標、左上角頂點y座標、寬度、高度。

第二行輸入乙個點p的資訊,包括其x座標和y座標。

第三行輸入長方形移動的距離,分別為在x方向和在y方向移動的距離。

所有輸入都為整數,之間以乙個空格分隔。無多餘空格或空行。

輸出三行:

第一行輸出長方形面積。

第二行輸出點p是否位於長方形r之內,如果在內部,則輸出「in」,否則輸出「not in」。

第三行輸出點p移動後是否位於長方形r之內,如果在內部,則輸出「in」,否則輸出「not in」。

1 0 10 9

11 3

2 0

90

not in

in

#include#include#include#include#include#include#include#include#includeusing namespace std;

class point;

point::point(int x,int y)

void point::move(int dx,int dy)

class rectangle:public point;

rectangle::rectangle(int x, int y, int width, int height):point(x,y)

int rectangle::getarea ()

bool rectangle::isin(point p)

Python入門課程2 2 類的繼承

1 繼承 即允許基於乙個現有的類作為起點定義新的類的一種技術 2 父類與子類 在物件導向的術語中,通常描述現有的類為基類 bass class 父類 parent class 或者超類 superclass 基於現有類新定義的類為子類 subclass或childclass 下面以課程2.1中cre...

C 深度剖析教程22 繼承的概念和意義

今天我們來學習c 中的繼承的概念和意義。問題 類之間是否存在直接的關聯關係?回答 類之間存在組合的關係,整體與部分的關係。可以看一下生活中的例子 下面我們以乙個簡單的程式來看一下類組合的關係 include include using namespace std class memory memor...

從類的記憶體結構看C 繼承

先看下面這個簡單的例子,我們建立乙個基類 base class 通過簡單的繼承 inherit 產生兩個派生類 derived class 然後通過乙個基類的指標去呼叫這兩個派生類,並且呼叫類中的函式。class base void show2 class derived1 public base ...