. .
( ) (), , . , virtual. , . , , virtual. base void vfun(int). dirl, dir2 (override), .. -:
//-
#include "stdafx.h"
#include <iostream>
using namespace std;
struct base {
virtual void vfun(int i) {cout <<"\nbase::i - "<<i;}
};
void print_(base& pb){pb.fun(1);}
struct dir1: public base {
void vfun (int i){cout << "\ndirl::i - " << i; }
};
struct dir2: public base {
void vfun (int i){ cout << "\ndir2::i = " << i; }
};
void main(void)
{
base B, *bp = &B;
dir1 D1, *dp1 = &D1;
dir2 D2, *dp2 = &D2;
bp->vfun(1); // : base::i = 1
dp1->vfun(2); // : dirl::i = 2
dp2->vfun(3); // : dir2::i = 3
bp = &D1;
bp->vfun(4); // :dir1::i = 4
bp = &D2;
bp->vfun(5); // : dir2::i = 5
print_(B); // : base::i = 1
print_(D1); // : dir1::i = 1
}
:
vfun() b . b base, . b &Dl, &D2, vfun() .
, , .. , . ( fun()).
, - . , ( ) , virtual .
|
|
, , . .
, , , . ( virtual ).
:
#include "stdafx.h"
#include <iostream>
using namespace std;
struct base {
virtual void f1(void) { cout << "\nbase::f1"; }
virtual void f2(void) { cout << "\nbase::f2"; }
virtual void f3(void) { cout <<"\nbase::f3"; }
};
struct dir: public base {
// :
void fl(void) { cout << "\ndir::fl"; }
// :
// int f2(void) { cout << "\ndir::f2"; }
// :
void f3(int i) { cout << "\ndir::f3::i = "<< i; }
};
void main (void) { base B, *pb = &B;
dir D, *pd = &D;
pb->f1();
pb->f2();
pb->f3();
pd->fl();
pd->f2();
//
dir::f3(); // pd->f3(); pd->f3(0); pb = &D; pb->fl(); pb->f2(); pb->f3();
// , :
pb->f3(3);
}
:
base::f1 base::f2 base::f3 dir::f1 base::f2 dir::f3::i = 0 dir::f1 base::f2 base::f3
, - . dir::fl() - , base::fl(). base::f2() dir , base::f3 (). dir::f3(int) - , . f3 (int) . base::f2() base::f3() dir. f3() . dir::f3(int) , , , .
, , , .
, . . , , virtual, . , .. . (friend) .
|
|
. , ( ) . ,
struct base {
virtual int f(int j) { return j * j; }
};
void print_(base* pb){pb->f(1);}
struct dir: public base {
int f(int i) { /* */ return base::f(i * 2); }
};
void main (){
dir D;
print_(&D);
}
. , () . , :
virtual _(__) = 0;
"= 0" " ". :
virtual void fpure(void) = 0;
" " . - . . . . , :
class {
protected:
virtual void func(char)=0;
void sos (int);
};
// - :
class D: public {
void func(char){};
};
class E: public {
void sos (int);
};
d func () . B::sos() D . D . B::sos(), B::func() . E .
, . , .
, . , , , , .
, "" "", "" .. Figura, .
// C/++ -> ; /MTd
// ->-> ; uafxcwd.lib
#include "stdafx.h"
#include "afxwin.h"
#include "iostream"
using namespace std;
HWND hwnd = 0;
HDC hdc = 0;
void InitGraphic(){system("mode con cols=168 lines=55"); system("pause >> void");
hwnd=FindWindow(_T("ConsoleWindowClass"),_T("C:\\Windows\\system32\\cmd.exe"));hdc=GetWindowDC(hwnd);}
void CloseGraphic(){ReleaseDC(hwnd, hdc); CloseHandle(hwnd);}
//-------------------------------------------------------------------------- IFigure
class IFigure{ //
protected:
int fMove; //0 - ; 1 - ; 2 - ;
int fClr; //0 - ; 1 -
public:
|
|
IFigure(): fMove(0), fClr(0){/*cout<<"\n IFigure()";*/}
virtual void show()=0;
virtual void hide()=0;
virtual void move(int x, int y)=0;
virtual ~IFigure(){}
};
//-------------------------------------------------------------------------Square
class Square:virtual public IFigure {
POINT pt[5];
COLORREF color;
public:
Square(POINT* p): color(RGB(255,0,0)){ for(int i =0; i <5; i++) pt[i] = p[i]; }
void SetColor(COLORREF cl){color = cl;}
void show(){
CPen pen(PS_SOLID,2,color);
SelectObject(hdc,pen);
Polyline(hdc,pt,5);
}
void hide(){
CPen pen;
pen.CreatePen(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;} }
virtual ~Square(){/*cout<<"\t ~Square()";*/}
};
//---------------------------------------------------------------------------ClsEllipse
class ClsEllipse: virtual public IFigure {
CPoint pt1,pt2;
public:
ClsEllipse():pt1(100,100),pt2(200,200) {}
virtual void show() {
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);
}
virtual void hide() {
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);
}
virtual void move(int x, int y) { pt1.x+=x,pt1.y+=y,pt2.x+=x,pt2.y+=y; }
virtual ~ClsEllipse(){/*cout<<"\t ~ClsEllipse()";*/}
};
//-------------------------------------------------------------------------Rectan
class Rectan: public IFigure {
Square* pSq;
public:
virtual void show(){pSq->show();}//
virtual void move(int x, int y){pSq->move(x,y);}//
virtual void hide(){pSq->hide();}//
void SetColor(COLORREF cl){pSq->SetColor(cl);}
Rectan (Square& p){pSq = new Square(p);}
virtual ~Rectan(){delete pSq;}
};
//-------------------------------------------------------------------------DrowTxt
class DrowTxt{
CString str;
public:
DrowTxt(CString s):str(s){}
void show(){
CDC* pCDC = CDC::FromHandle(hdc);
pCDC->SetTextColor(RGB(255,0,0));
pCDC->SetBkColor(RGB(0,0,0));
pCDC->TextOutW(300,100,str); pCDC->TextOutW(0,0," ");
}
};
//-------------------------------------------------------------------------Heir
class Heir: public Square, public ClsEllipse{ //
public: //( )
Heir(POINT *p):Square(p),ClsEllipse(){/*cout<<"\t Heir()";*/ }
void show(){Square::show(); ClsEllipse::show();}
void move(int x, int y){Square::move(x,y); ClsEllipse::move(x,y);}
void hide(){Square::hide(); ClsEllipse::hide();}
virtual ~Heir(){/*cout<<"\n ~Heir()";*/}
};
//------------------------------------------------------------------------RecordPlayer
class RecordPlayer{ // , IFigure
IFigure**pFig;// IFigure*
int n; //
int N; //
public:
void Insert(IFigure* pF){if (n<N) pFig[n++] =pF; }
RecordPlayer(int Nfig): N(Nfig), n(0) { pFig = new IFigure*[N]; }
virtual void show(){ for(int i = 0; i < n; i++) pFig[i]->show(); }//
virtual void hide(){ for(int i = 0; i < n; i++) pFig[i]->hide(); }//
virtual void move(int x, int y){ for(int i = 0; i < n; i++) pFig[i]->move(x,y); }//
|
|
void PlayMyObject(int x, int y){ for(int i = 0; i <150; i++){show();Sleep(24);hide(); move(x,y);} show();}
virtual ~RecordPlayer(){delete []pFig;}
};
void main(){
POINT pt1[5];
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;
InitGraphic();
DrowTxt dtxt("");
dtxt.show();
getchar();
Heir hr(pt1);
for(int i = 0; i <100; i++){
hr.show(); Sleep(24); hr.hide(); hr.move(0,3);
}
getchar();
ClsEllipse elp;
Square sq1(pt1), sq2(pt1), sq3(pt1);
sq1.SetColor(RGB(255,255,0)); sq2.SetColor(RGB(0,255,0));
sq3.SetColor(RGB(0,0,255)); hr.SetColor(RGB(0,255,255));
sq2.move(20,20); sq3.move(40,30); hr.move(0,-150);
Rectan rec(sq3);
RecordPlayer RPlayer(5);
RPlayer.Insert(&elp);
RPlayer.Insert(&sq1);
RPlayer.Insert(&sq2);
RPlayer.Insert(&rec);
RPlayer.Insert(&hr);
RPlayer.PlayMyObject(3,0);
getchar();
CloseGraphic();
}
. , , , . , . , , . , . . . .