const Loc NK(0,0); //
.
:
const;
:
1) ;
2) .
, .
static. , , , .
class Name
{
public:
static int count; }
, , , , :
Name::count+=2;
, .
, . : Name::Add1();
:
1)
. :
::
2) this, .. .
3) , .
4) .
- , ( ) , .
, , (, ..) . , , .
:
virtual _;
, , , - . .
. Point
( Location). void Show(). , , void Show() . , . , virtual.
|
|
class Point: public Location{
protected:
Boolean vis;
public:
Point (int nx,int ny);
virtual void Show();
virtual void Hide();
virtual void Drag(int by);
Boolean Isvis() { return vis;}
void MoveTo (int nx,int ny); };
, - . , ( ) , virtual .
, , . .
, , , . ( virtual ).
void Show() :
class Circle: public Point {
protected:
int R;
public:
Circle (int nx,int ny, int nr);
void Show();
void Hide();
void Expand(int by);
void Contract(int by); };
, virtual .
. , ( ) .
:
1) static;
2) , . virtual .
3) .
, :
virtual _ ( )=0;
=0 . :
virtual void F()=0;
, . . , .
. , .
, . . , .
|
|
, . , .
, . , , , , .
, .
:
//
# include <iostream.h>
class area
{double dim1, dim2; //
public:
void setarea(double dim1,double dim2)
{dim1=d1;
dim2=d2;
}
void getdim(double &dim1,double &dim2)
{d1=dim1;
d2=dim2;
}
virtual double getarea()=0; //
};
class rectangle: public area
{ public:
double getarea()
{double d1,d2;
getdim(d1,d2);
return d1*d2;
}
};
class triangle: public area
{ public:
double getarea()
{double d1,d2;
getdim(d1,d2);
return 0.5*d1*d2;
}
};
int main()
{area *p;
rectangle r;
triangle t;
r.setarea(3.3,4.5);
t.setarea(4.0,5.0);
p=&r;
cout<< :<<p->getarea()<<\n;
p=&t;
cout<< :<<p->getarea()<<\n;
return 0;
}
, getarea() , .
C |
B |
D |
A D B C ( ). , B C .
class B: virtual public A
{ }
class C: virtual public A
{ }
class D: public C, public B
D : ( ), , D. D .
, . . , .
:
template < >
{ }
. .
<class 1,
class n>
1, n . .
.
, :
template <class T>
void Obmen(T A[], int i, int j)
{T temp;
temp=A[i];
A[i]=A[j];
A[j]=temp;}
main()
{int Z[10];
Obmen (Z,5,7);
float x[100];
Obmen (x,6,7);
}
, .. , , . :
|
|
1) , .
,
2) , .
,
3) .
, . .
template < >
class NameClass
{ }
< class 1,
class n,
k >
1 - n .
k .
:
NameClass <int> A ( )
, int. :
class
(int, double ..), .
.
.
:
template <class T>
class TSteck
{int n; T *item;
public:
TSteck(int S=10)
{n=0; item=new T[S];}
void push(T t);
T pop();
}
template <class T>
void TSteck:: push(T t)
{item[n++]=t;}
T TSteck:: pop()
{return item[n--];}
TSteck <float> A(100); //
, , .
C++
C++ , . , .. , . ( , ) .
:
try, catch, throw. .
try , , .. , (, , ..).
, try, try-. try- .
main()
{
try{
f1();
f2(); }
}
f1 f2 . try- ( ).
catch try-. , . .
catch (int i) //
{ cout<< : ;
cout<<i<<\n;
}
catch- .
|
|
:
catch ()
{
}
.
, , . . , . .
throw . , . . .
, throw, .
throw , , . catch-, .
void f1()
{
if (<>)
throw ;
}
void f2()
{
if (<>)
{ x=3;
throw x;}
}
throw
1) ;
2) , , ;
3) catch-, , . .
, . - C++.
#include <graphics.h> //
#include <iostream.h> // -
#include <conio.h> //
enum Boolean{false, true}; //
class Location
{protected:
int X;int Y; //
public: //
// Location (); // .
// .
Location (int nx, int ny) { X=nx;Y=ny;} // ,
// .
// , .
int GetX() {return X;}
int GetY() { return Y;}
};
//
class Point: public Location
{protected:
Boolean vis;
public:
Point (int nx,int ny); //
//
virtual void Show(); //,
virtual void Hide(); //,
virtual void Drag(int by);// by
Boolean Isvis() { return vis;}
void MoveTo (int nx,int ny); //
};
//
class Circle: public Point
{protected:
int R;
public:
Circle (int nx,int ny, int nr); //
void Show(); // Circle Point,
void Hide(); // Show() Hide()
void Expand(int by); //
void Contract(int by);//
};
Boolean GetDelta (int &dx,int &dy); //
//
Point::Point(int nx,int ny):Location(nx,ny)
{ vis=false; }
void Point:: Show()
{ vis= true;putpixel(X,Y,getcolor());}// getcolor() ,
//
void Point:: Hide()
{vis=false;putpixel(X,Y,getbkcolor());}//getbkcolor()
void Point:: MoveTo(int nx,int ny)
{Hide();X=nx;Y=ny;Show(); }//
//
Boolean GetDelta(int& dx,int& dy)
{ char Keych;
Boolean Quit;
dx=0;dy=0;
do
{Keych=getch();
if (Keych == 13)
return (false);
if (Keych == 0)
{Quit=true;
Keych=getch();
switch (Keych){
case 72:dy=-1;break;
case 80: dy=1; break;
case 75:dx=-1; break;
case 77: dx=1; break;
default: Quit=false;}; };
} while (!Quit);
return (true); }
// by
// ,
// x y 45
void Point::Drag(int by)
{ int dlx,dly;
int fx,fy;
Show();
fx= GetX();fy= GetY();
while (GetDelta(dlx,dly))
{ fx+=dlx*by;
fy+=dly*by;
MoveTo(fx,fy);}; }// ,
|
|
Circle::Circle(int nx,int ny,int nr):Point(nx,ny)
{ R=nr;}
void Circle:: Show()
{ vis=true;circle(X,Y,R); }
void Circle:: Hide()
{ unsigned tcol; // tcol -
tcol=getcolor();
setcolor(getbkcolor());//
vis=false;
circle(X,Y,R); //
setcolor(tcol); }// ,
void Circle:: Expand(int by) //
{Hide();
R+=by;
if (R<0)
R=0;
Show(); }
void Circle:: Contract(int by) //
{Expand(-by);}
//
class Ring: public Circle
{protected:
int R; int R1;
public:
Ring(int nx,int ny, int nr, int nr1); //
void Show();
void Hide();
void Expand(int by);
void Contract(int by);
};
Ring::Ring(int nx,int ny, int nr, int nr1):Circle(nx,ny,nr)
{R=nr;R1=nr1;}
void Ring::Show()
{vis=true;circle(X,Y,R);circle(X,Y,R1);}
void Ring::Hide()
{ unsigned tcol;
tcol=getcolor();
setcolor(getbkcolor());
vis=false;
circle(X,Y,R);
circle(X,Y,R1);
setcolor(tcol);
}
void Ring:: Expand(int by)
{Hide();
R+=by;
R1+=by;
if (R<0)
R=0;
if (R1<0)
R1=0;
Show();
}
void Ring:: Contract(int by)
{Expand(-by);}
//
class Audi: public Ring
{protected:
int R;int R1;
public:
Audi(int nx,int ny, int nr, int nr1);
void Show();
void Hide();
void Expand(int by);
void Contract(int by);
};
Audi::Audi(int nx,int ny, int nr, int nr1):Ring(nx,ny,nr,nr1)
{R=nr;R1=nr1;}
void Audi::Show()
{vis=true;
circle(X,Y,R);circle(X,Y,R1); //
circle(X+120,Y,R);circle(X+120,Y,R1);
circle(X+240,Y,R);circle(X+240,Y,R1);
circle(X+360,Y,R);circle(X+360,Y,R1);
}
void Audi::Hide()
{ unsigned tcol;
tcol=getcolor();
setcolor(getbkcolor());
vis=false;
circle(X,Y,R);circle(X,Y,R1);
circle(X+120,Y,R);circle(X+120,Y,R1);
circle(X+240,Y,R);circle(X+240,Y,R1);
circle(X+360,Y,R);circle(X+360,Y,R1);
setcolor(tcol);
}
void Audi:: Expand(int by)
{Hide();
R+=by;
R1+=by;
if (R<0)
R=0;
if (R1<0)
R1=0;
Show();
}
void Audi:: Contract(int by)
{Expand(-by);}
//
void main()
//
{ int graphdriver=DETECT,graphmode;
initgraph(&graphdriver,&graphmode,"c:\\borlandc\\bgi");
//
Point P(100,50);
P.Show(); getch();
P.MoveTo(300,100); getch();
P.Hide();
//
Circle C(200,200,100);
C.Show(); getch();
C.MoveTo(300,200); getch();
C.Contract(50);getch();
C.Expand(50);getch();
C.Drag(5);
C.Hide();
//
Ring R(300,300,100,80);
R.Show();getch();
R.MoveTo(200,200); getch();
R.Contract(50);getch();
R.Expand(50);getch();
R.Drag(10);
R.Hide();
//
Audi A(100,300,80,70);
A.Show();getch();
A.MoveTo(150,200); getch();
A.Contract(50);getch();
A.Expand(50);getch();
A.Drag(10);
A.Hide();
//
closegraph();
}