, , union. , .
, Figure Square (). Square Figure:
(public, ):
show() ( 1.3);
hide() ( 1.4);
move(int x, int y) ( 1.5);
Figure() ( 1.2);
InitGraphic() ( 1.6). (protected, ):
static HDC hdc ;
display() ( 1.1);
(private):
static HWND hwnd.
Figure, , , Figure , . , , Figure Square, ClsEllipse. ( ) show(), hide(), move(int x, int y), ( 1.11 1.13, 1.16-1.18) , .
, . , . , ( Figure) ( Square ClsEllipse).
. , Square Figure public, Square Figure , .. () Figure Square. , Square Figure display(). , display() Figure, Square ( 1.10) , (Figure::display()) . Square display() .
|
|
// Figura.cpp:
#include "stdafx.h"
#include "afxwin.h"
#include "iostream"
using namespace std;
class Figure{
static HWND hwnd;
protected:
static HDC hdc;
void display(){cout<<" \n Figure ";} //1.1
public:
Figure(){/*cout<<"\n Figure()";*/} //1.2
void show(){} //1.3
void hide(){} //1.4
void move(int x, int y){} //1.5
static void InitGraphic(){hwnd=FindWindow(_T("ConsoleWindowClass"),_T("C:\\Windows\\system32\\cmd.exe"));hdc=GetWindowDC(hwnd);} //1.6
static void CloseGraphic(){ReleaseDC(hwnd, hdc); CloseHandle(hwnd);} //1.7
~Figure(){/*cout<<"\t ~Figure()";*/} //1.8
};
HWND Figure::hwnd = 0;
HDC Figure::hdc = 0;
//
// Square Figure ()
class Square: public Figure {
POINT pt[5]; //
public:
// , //
Square(POINT* p){for(int i =0; i <5; i++){pt[i].x = p[i].x;pt[i].y = p[i].y;}} //1.9
void display(){cout<<" \n Square ->"; Figure:: display(); } //1.10
void show(){ //1.11
CPen pen(PS_SOLID,2,RGB(255,0,0));
SelectObject(hdc,pen);
Polyline(hdc,pt,5);
}
void hide(){ //1.12
CPen pen(PS_SOLID,2,RGB(0,0,0));
SelectObject(hdc,pen);
Polyline(hdc,pt,5);
}
void move(int x, int y){for(int i = 0; i<5;i++){ pt[i].x+=x;pt[i].y+=y;} } //1.13
~Square(){/*cout<<"\t ~Square()";*/} //1.14
};
//
// ClsEllipse Figure ()
class ClsEllipse: public Figure {
CPoint pt1, pt2; // 堠
public:
ClsEllipse(){/*cout<<"\t ClsEllipse()";*/ //1.15
pt1.x=100; pt1.y=100;
pt2.x=200; pt2.y=200;
}
void show(){ //1.16
CPen pen(PS_SOLID,2,RGB(0,255,0));
SelectObject(hdc,pen);
Arc(hdc,pt1.x,pt1.y,pt2.x,pt2.y,100,200,0,100);
}
void hide(){ //1.17
CPen pen(PS_SOLID,2,RGB(0,0,0));
SelectObject(hdc,pen);
Arc(hdc,pt1.x,pt1.y,pt2.x,pt2.y,100,200,0,100);
}
void move(int x, int y){ pt1.x+=x,pt1.y+=y,pt2.x+=x,pt2.y+=y; } //1.18
~ClsEllipse(){/*cout<<"\t ~ClsEllipse()";*/} //1.19
};
//
// MyObject ( )
class MyObject{
|
|
Square sq1, sq2; // ( ) //1.20
ClsEllipse& elp; // ( ) //1.21
public:
MyObject(const Square& p1,const Square& p2,ClsEllipse& el):sq1(p1),sq2(p2), elp(el) //1.22 {/*cout<<"\t MyObject()";*/}
void show(){sq1.show(); sq2.show();elp.show();} //1.23
void move(int x, int y){sq1.move(x,y); sq2.move(x,y);elp.move(x,y);}//1.24
void hide(){sq1.hide(); sq2.hide(); elp.hide();} //1.25
~MyObject(){/*cout<<"\n ~MyObject()";*/} //1.26
};
//
// Heir ( )
class Heir: public Square, public ClsEllipse{ //1.27
public:
Heir(POINT *p):Square(p),ClsEllipse(){/*cout<<"\t Heir()";*/ } //1.28
void show(){Square::show(); ClsEllipse::show();} //1.29
void move(int x, int y){Square::move(x,y); ClsEllipse::move(x,y);}//1.30
void hide(){Square::hide(); ClsEllipse::hide();} //1.31
~Heir(){/*cout<<"\n ~Heir()";*/} //1.32
};
void ShowMyObject(MyObject obj){ //1.33
for(int i = 0; i <100; i++){obj.show(); Sleep(24); obj.hide(); obj.move(4,0);}
}
( ). , MyObject Square ( ) ClsEllipse ( 1.20, 1.21). , Square MyObject, ClsEllipse .
() . , MyObject , ( Square) ClsEllipse. , Square MyObject , , ClsEllipse MyObject . MyObject () ClsEllipse, () MyObject, ClsEllipse () MyObject.
, MyObject show(), hide(), move(int x, int y) 1.23-1.25. , Square ClsEllipse. ( ) MyObject , .
, - .
Heir, Square, ClsEllipse ( 1.27). , . . , , , . , Square , ClsEllipse . Heir . , Heir show(), hide(), move(int x, int y) 1.29-1.31, , Square ClsEllipse. , Heir .
|
|
, , , Heir (), Square () ClsEllipse (), .
void main(){
POINT pt1[5]; //2.1
pt1[0].x = 40;pt1[0].y=40;
pt1[1].x = 40;pt1[1].y=140;
pt1[2].x = 140;pt1[2].y=140;
pt1[3].x = 140;pt1[3].y=40;
pt1[4].x = 40;pt1[4].y=40; //2.2
Figure::InitGraphic(); //2.3
{ //2.4
Square sq1(pt1); ClsEllipse elp; //2.5
for(int i = 0; i <100; i++){ //2.6
sq1.show(); elp.show(); Sleep(24);
sq1.hide(); elp.hide(); //2.7
sq1.move(1,1); elp.move(2,2); //2.8
} //2.9
} //2.10
ClsEllipse elp;
Square sq2(pt1);
sq2.move(20,20);
MyObject obj(pt1, sq2, elp); //2.11
getchar();
ShowMyObject(obj); //2.12
{
Heir hr(pt1); //2.13
getchar();
for(int i = 0; i <100; i++){
hr.show(); Sleep(24); hr.hide(); hr.move(0,3);
}
}
Figure::CloseGraphic(); //2.14
}
main, , . 2.1 2.2 pt1 POINT, Square. 2.3 , .
, 2.4 2.10 . , Square, ClsEllipse 2.5 .
2.5 sq1, elp Square ClsEllipse. ( 1.9 1.15). Square(POINT* p) , . 2.11 pt1 POINT obj. sq1 . Square sq1(pt1), , :
Square(POINT* p)/* Figure() */ {for(int i =0; i <5; i++){pt[i].x = p[i].x;pt[i].y = p[i].y;}},
POINT.
Square, Figure() ( 1.2). , -, .. ( ), , .. . Square Figure . . Square
Square(POINT* p): Figure() {for(int i =0; i <5; i++){pt[i].x = p[i].x;pt[i].y = p[i].y;}}
, , . , MyObject
|
|
MyObject(const Square& p1,const Square& p2,ClsEllipse& el):sq1(p1),sq2(p2), elp(el){}
( ) , . , :
MyObject(const Square& p1,const Square& p2,ClsEllipse& el)
/* sq1, sq2, elp */
{
/* , sq1, sq2 elp , */
for(int i =0; i <5; i++){ sq1.pt[i] = p1[i]; sq2.pt[i] = p2[i];}
elp.pt1= el.pt1; elp.pt2= el.pt2;
/* sq1, sq2, elp*/
}
Heir
Heir(POINT *p):Square(p),ClsEllipse(){/*cout<<"\t Heir()";*/ }
, , Heir Square, ClsEllipse. ClsEllipse() , .
2.6 2.9 sq1 elp . for, show() , sleep(24) 24, hide() move(2,2) , for.
2.10 , , sq1 elp . . , .. , .
, 2.10 ~Square(), ~Figure() sq1. elp ~ClsEllipse(), ~Figure().
. , () . , 2.11 obj MyObject, . main, obj . ~MyObject(), sq1, sq2 ~Square()->~Figure(),~Square()->~Figure(). elp , .
. , , . , MyObject elp ClsEllipse, . : ~MyObject(){elp.~ ClsEllipse();} , , , , MyObject ClsEllipse elp, .
, ShowMyObject(obj); 2.12. ( 1.33) , , , , obj , MyObject sq1, sq2 ( 1.20) Square. MyObject elp ClsEllipse ( 1.21) .
|
|
|
|
: 2018-10-15; !; : 214 |
:
!
.
.
==> ...