1. 程式人生 > >c++繼承經典例子

c++繼承經典例子


#include <iostream.h>

class Base
{
private:
        int b_number;
public:
        Base( ){}
        Base(int i) b_number (i) }


        int get_number( {return b_number;}
        void print( {cout << b_number << endl;}        
};

 

class Derived public Base
{
private:
        int d_number;

public:
// constructor, initializer used to initialize the base part of Derived object.
        Derived( int i, int Base(i), d_number(j) };        


        // new member function that overrides the print( function in Base
        void print( 
        {
                cout << get_number( << ";        
                // access number through get_number( )
                cout << d_number << endl;
        }
};

int main( )
{
        Base a(2);
        Derived b(3, 4);

        cout << "a is ";
        a.print( );                // print( in Base
        cout << "b is ";
        b.print( );                // print( in Derived
        cout << "base part of is "; 
        b.Base::print( );                // print( in Base

        return 0;
}

 

c++ <wbr>繼承(太詳細了!!!)


 沒有虛解構函式,繼承類沒有析構

//Example:  non- virtual destructors for dynamically allocated objects. 

#include <iostream.h>
#include <string.h>

class Thing
public:
virtual void what_Am_I( {cout << "I am Thing./n";}
~Thing(){cout<<"Thing destructor"<<endl;}
};

class Animal public Thing
 
public:
virtual void what_Am_I( {cout << "I am an Animal./n";}
~Animal(){cout<<"Animal destructor"<<endl;}
};

void main( )
{
   Thing *t =new Thing;      
   Animal*x new Animal;
   Thing* array[2];

   array[0] t;                                // base pointer
   array[1] x;               

    for (int i=0; i<2; i++)  array->what_Am_I( ;

   delete array[0];
   delete array[1];
   return ;
}

 

c++ <wbr>繼承(太詳細了!!!)


純虛擬函式,多型#include <iostream.h>

#include <math.h>

class Point
{
private:
        double x;
        double y;
public:
        Point(double i, double j) x(i), y(j) 
        void print( const
        cout << "(" << << ", << << ")"; }
};

class Figure
{
private:
        Point center;
public:
        Figure (double 0, double 0) center(i, j)         
        
Point& location( )
{
return center;
                 // return an lvalue
   void move(Point p)
{
center p;
draw( );
}

        virtual void draw( 0; // draw the figure
        virtual void rotate(double) 0; 
// rotate the figure by an angle                
};

class Circle public Figure
{
private:
        double radius;
public:
        Circle(double 0, double 0, double 0) Figure(i, j), radius(r) }
        void draw( )
        {
                cout << "A circle with center ";
                location( ).print( );
                cout << and radius << radius << endl;
        }
        void rotate(double)
        {
                cout << "no effect./n";
               // must be defined
};

class Square public Figure
{
private:
        double side;        // length of the side
        double angle;        // the angle between side and the x-axis
public:
        Square(double 0, double 0, double 0, double 0)        Figure(i, j), side(d), angle(a) }
   void draw( )
        {
                cout << "A square with center ";
                location( ).print( );
                cout << side length << side << "./n"  
                << "The angle between one side and the X-axis is << angle << endl;
        }
        void rotate(double a)
        {
               angle += a;
                cout << "The angle between one side and the X-axis is << angle << endl;
        }
        void vertices( )
        {
                cout << "The vertices of the square are:/n";
                // 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;

        -> draw( );
        -> move(Point(2, 2));

        g.draw( );
          g.rotate(1);
        
s.vertices( );
// Cannot use here since vertices( is not member of Figure.

        return 0;
}
////////////////////////////////////////////////////////////////////
#include <iostream.h>
#include <string.h>

class Thing

public:
virtual void what_Am_I( {cout << "I am Thing./n";}

~Thing(){cout<<"Thing destructor"<<endl;}
};

class Animal public Thing

public:
virtual void what_Am_I( {cout << "I am an Animal./n";}

~Animal(){cout<<"Animal destructor"<<endl;}
};

void main( )
{
   Thing 
        Animal ;
   Thing* array[2];

   array[0] &t;                        // base pointer
   array[1] &x;        
          for (int i=0; i<2; i++)  array->what_Am_I( ;

   return ;
}

 

c++ <wbr>繼承(太詳細了!!!)

多繼承

#include <iostream.h>

class A
{
private:
        int a;
public:
        A(int i) a(i) }
        virtual void print(        {cout << << endl;}
        int get_a( {return a;}
};

class B
{
private:
        int b;
public:
        B(int j) b(j) }
        void print(        {cout << << endl;}
        int get_b( {return b;}
};

class 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( << << get_b( << endl;}
        // use get_a( and get_b( without scope resolution
};

int main( )
{
        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 is inherited from only,
                                                // cannot access A::print( );
        x.A::print( );                // use A::print( );
        x.get_ab( );

        return 0;
}

 

c++ <wbr>繼承(太詳細了!!!)

共同基類的多繼承

#include <iostream.h>
class R
{int r;
public:
        R(int anInt){ anInt;};
       printOn(){ cout<<"r="<<r<<endl;} };

class public R
{
int a;
public:
        A(int int1,int int2):R(int2){ int1;};};

class public R
{
int b;
public:
        B(int int1,int int2):R(int2){ int1;};};

class public A, public B
{
int c;
public:
C(int int1,int int2, int int3):A(int2,int3), B(int2,int3){ int1;}
};


int main( )
   
  int i;
        rr(10);      
aa(20,30);      
bb (40,50);
        cc(5, 7, 9);
        rr.printOn();    
aa.printOn();                  //inherits printOn  
bb.printOn();                   //inherits printOn
        //cc.printOn();                  //would give error
        return 0;}

 

c++ <wbr>繼承(太詳細了!!!)


虛基類 

#include <iostream.h>

class R
int r;
public:
        (int 0) r(x)   // constructor in R
        void f( ){ cout<<"r="<<r<<endl;}     
        void printOn(){cout<<"printOn R="<<r<<endl;}
};

class public virtual R
 int a;
public:
        (int x, int y) R(x), a(y)  // constructor in A
        void f( ){ cout<<"a="<<a<<endl;R::f();}
};

class public virtual R
{int b;
public:
        B(int x, int z) R(x), b(z) }// constructor in B
        void f( ){ cout<<"b="<<b<<endl;R::f();}
};

class public A, public B
int c;
public:
// constructor in C, which constructs an object first
C(int x, int y, int z, int w) R(x), A(x, y), B(x, z), c(w) }
        
void f( ){ cout<<"c="<<c<<endl;A::f(); B::f();}
};

void main()
 rr(1000);
   aa(2222,444);
   bb(3333,111);
   cc(1212,345,123,45);
   cc.printOn();     //uses printOn but only R..no ambiguity
   cc.f();                // shows multiple call of the R::f()
}


////////////////////////////////////////

#include <iostream.h>

class R
int r;
public:
        (int 0) r(x)   // constructor in R
        void f( ){ cout<<"r="<<r<<endl;}
};

class virtual public R
int ;
protected:
        void fA( ){cout<<"a="<<a<<endl;};

public:
        (int x, int y) R(x), a(y)  // constructor in A
        void f( {fA( ); R::f( );}
};

class virtual public R
 int b;
protected:
        void fB( ){cout<<"b="<<b<<endl;};
public:
        (int x, int y) R(x), b(y)  // constructor in A
        void f( {fB( ); R::f( );}
};


class public A, public B
int c;

protected:
        void fC( ){ cout<<"c="<<c<<endl;};        
public:
C(int x, int y, int z, int w) R(x), A(x, y), B(x, z), c(w) }

void f( )
         
              &nbs