double angle; // the angle between a side and the x-axis
public:
Square(double i = 0, double j = 0, double d = 0, double a = 0) : Figure(i, j), side(d), angle(a) { } void draw( ) {
cout << \ square with center \ location( ).print( );
cout << \
<< \ }
void rotate(double a) {
angle += a;
cout << \ }
void vertices( ) {
cout << \ // calculate coordinates of the vertices of the square } };
int main( ) {
Circle c(1, 2, 3); Square s(4, 5, 6); Figure *f = &c, &g = s;
f -> draw( );
f -> move(Point(2, 2));
g.draw( ); g.rotate(1); s.vertices( );
// Cannot use g here since vertices( ) is not a member of Figure.
return 0; }
//////////////////////////////////////////////////////////////////// #include
class Thing
{ public:
virtual void what_Am_I( ) {cout << \
~Thing(){cout<<\};
class Animal : public Thing {
public:
virtual void what_Am_I( ) {cout << \
~Animal(){cout<<\};
void main( ) {
Thing t ; Animal x ; Thing* array[2];
array[0] = &t; // base pointer array[1] = &x;
for (int i=0; i<2; i++) array->what_Am_I( ) ;
return ; }
--------------------------------------------------------------------------------
多继承
#include
private: int a; public:
A(int i) : a(i) { }
virtual void print( ) {cout << a << endl;} int get_a( ) {return a;}
};
class B {
private: int b; public:
B(int j) : b(j) { }
void print( ) {cout << b << endl;} int get_b( ) {return b;} };
class C : public A, public B {
int c; public:
C(int i, int j, int k) : A(i), B(j), c(k) { }
void print( ) {A::print( ); B::print( );} // use print( ) with scope resolution
void get_ab( ) {cout << get_a( ) << \ // use get_a( ) and get_b( ) without scope resolution };
int main( ) {
C x(5, 8, 10); A* ap = &x;
B* bp = &x;
ap -> print( ); // use C::print( );
bp -> print( ); // use B::print( );
// bp -> A::print( ); // as if x is inherited from B only, // cannot access A::print( ); x.A::print( ); // use A::print( ); x.get_ab( );
return 0; }
--------------------------------------------------------------------------------
共同基类的多继承
#include
class R {int r; public:
R(int anInt){ r = anInt;};
printOn(){ cout<<\
class A : public R { int a;
public:
A(int int1,int int2):R(int2){ a = int1;};};
class B : public R { int b;
public:
B(int int1,int int2):R(int2){ b = int1;};};
class C : public A, public B {
int c; public:
C(int int1,int int2, int int3):A(int2,int3), B(int2,int3){ c = int1;} };
int main( ) { int i;
R rr(10); A aa(20,30); B bb (40,50);
C cc(5, 7, 9);
rr.printOn();
aa.printOn(); //inherits R printOn bb.printOn(); //inherits R printOn //cc.printOn(); //would give error return 0;}
--------------------------------------------------------------------------------
虚基类
#include
class R { int r; public:
R (int x = 0) : r(x) { } // constructor in R void f( ){ cout<<\ void printOn(){cout<<\};
class A : public virtual R { int a;
public:
A (int x, int y) : R(x), a(y) { } // constructor in A void f( ){ cout<<\};
class B : public virtual R {int b; public:
B(int x, int z) : R(x), b(z) { }// constructor in B void f( ){ cout<<\};
class C : public A, public B { int c; public:
// constructor in C, which constructs an R object first
C(int x, int y, int z, int w) : R(x), A(x, y), B(x, z), c(w) { }
void f( ){ cout<<\};
void main() { R rr(1000);
A aa(2222,444); B bb(3333,111);
C cc(1212,345,123,45);
cc.printOn(); //uses R printOn but only 1 R..no ambiguity cc.f(); // shows multiple call of the R::f() }
////////////////////////////////////////
C++学习笔记与开发技巧与典型列子
![](/skin/haowen/images/icon_star.png)
![](/skin/haowen/images/icon_star.png)
![](/skin/haowen/images/icon_star.png)
![](/skin/haowen/images/icon_star.png)