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 a Derived object.
        Derived( int i, int j ) : Base(i), d_number(j) { };       
        // a 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 b is ";
        b.Base::print( );                // print( ) in Base

        return 0;
}

 



没有虚析构函数,继承类没有析构
//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 a 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 ;
}

 



纯虚函数,多态

#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 << "(" << x << ", " << y << ")"; }
};

class Figure
{
private:
        Point center;
public:
        Figure (double i = 0, double j = 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 i = 0, double j = 0, double r = 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 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 << "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;

        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 <iostream.h>
#include <string.h>

class Thing
{
public:
virtual void what_Am_I( ) {cout << "I am a 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 ;
        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 <iostream.h>

class A
{
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( ) << " " << get_b( ) << endl;}
        // 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 <iostream.h>
class R
{int r;
public:
        R(int anInt){ r = anInt;};
       printOn(){ cout<<"r="<<r<<endl;} ; };

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 <iostream.h>

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

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

class B : 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 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<<"c="<<c<<endl;A::f(); B::f();}
};

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()
}


#include <iostream.h>

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

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

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

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


class C : 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( )
        {
                   R::f( );                    // acts on R stuff only
                A::fA( );            //acts on A stuff only
                B::fB( );                   // acts on B stuff only
                fC( );                  // acts on C stuff only
        }
};

void main()
{ R rr(1000);
   A aa(2222,444);
   B bb(3333,111);
   C cc(1212,345,123,45);
   cc.f();
}

 


 

私有继承


// Access levels

#include <iostream.h>

class Base
{
private:
        int priv;
protected:
        int prot;
        int get_priv( ) {return priv;}
public:
        int publ;
        Base( );
        Base(int a, int b, int c) : priv(a), prot(b), publ(c) { }
        int get_prot( ) {return prot;}
        int get_publ( ) {return publ;}
};

class Derived1 : private Base        // private inheritance
{
public:
        Derived1 (int a, int b, int c) : Base(a, b, c) { }
        int get1_priv( ) {return get_priv( );}
        // priv not accessible directly
        int get1_prot( ) {return prot;}
      int get1_publ( ) {return publ;}
};

class Leaf1 : public Derived1
{
public:
        Leaf1(int a, int b, int c) : Derived1(a, b, c) { }
        void print( )
        {
                cout << "Leaf1 members: " << get1_priv( ) << " "
//                        << get_priv( )        // not accessible
                        << get1_prot( ) << " "
//                        << get_prot( )         // not accessible
//                        << publ         // not accessible
                        << get1_publ( ) << endl;
        } // data members not accessible. get_ functions in Base not accessible
};

class Derived2 : protected Base // protected inheritance
{
public:
        Derived2 (int a, int b, int c) : Base(a, b, c) { }
};

class Leaf2 : public Derived2
{
public:
        Leaf2(int a, int b, int c) : Derived2(a, b, c) { }
        void print( )
        {
                cout << "Leaf2 members: " << get_priv( ) << " "
//                        << priv                 // not accessible
                        << prot << " "
                        << publ << endl;
        } // public and protected data members accessible. get_ functions in Base accessible.
};

class Derived3 : public Base // public inheritance
{
public:
        Derived3 (int a, int b, int c) : Base(a, b, c) { }
};

class Leaf3 : public Derived3
{

public:
        Leaf3(int a, int b, int c) : Derived3(a, b, c) { }
        void print( )
        {
                cout << "Leaf3 members: " << get_priv( ) << " "
                        << prot << " "
                        << publ << endl;
        } // public and protected data members accessible. get_ functions in Base accessible
};

int main( )
{
        Derived1 d1(1, 2, 3);
        Derived2 d2(4, 5, 6);
        Derived3 d3(7, 8, 9);

//        cout << d1.publ;                // not accessible
//        cout << d1.get_priv( );        // not accessible
//        cout << d2.publ;                // not accessible
//        cout << d2.get_priv( );        // not accessible
        cout << d3.publ;                // OK
        cout << d3.get_prot( );        // OK

        Leaf1 lf1(1, 2, 3);
        Leaf2 lf2(4, 5, 6);
        Leaf3 lf3(7, 8, 9);

//         cout << lf1.publ << endl;                    // not accessible
//         cout << lf2.publ << endl;                // not accessible
        cout << lf3.publ << endl;                 // OK

        return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中,继承、封装和多态是面向对象编程的三个基本特征。销售管理系统是一个典型的面向对象程序设计的例子,可以使用C++中的继承、封装和多态来实现。 1. 继承 继承是指一个类可以从另一个类中继承属性和方法。在销售管理系统中,可以定义一个基类Sales,包含一些通用的属性和方法,如销售日期、销售数量、销售金额等。然后定义一些派生类,如ProductSales、ServiceSales等,继承Sales类,并添加一些特定的属性和方法。 2. 封装 封装是指将数据和方法封装在一个类中,对外部隐藏实现细节,只提供公共接口。在销售管理系统中,可以将Sales类的属性和方法封装起来,只提供一些公共的接口,如添加销售记录、查询销售记录等。 3. 多态 多态是指同一种操作作用于不同的对象上面,可以产生不同的执行结果。在C++中,可以通过虚函数来实现多态。在销售管理系统中,可以将Sales类的某些方法定义为虚函数,然后在派生类中重写这些虚函数,以实现不同的执行结果。 下面是一个简单的C++销售管理系统的示例代码: ```cpp #include <iostream> #include <string> using namespace std; class Sales { public: Sales(string date, int quantity, double amount) { this->date = date; this->quantity = quantity; this->amount = amount; } virtual void print() { cout << "Date: " << date << endl; cout << "Quantity: " << quantity << endl; cout << "Amount: " << amount << endl; } protected: string date; int quantity; double amount; }; class ProductSales : public Sales { public: ProductSales(string date, int quantity, double amount, string product) : Sales(date, quantity, amount) { this->product = product; } void print() { cout << "Product: " << product << endl; Sales::print(); } private: string product; }; class ServiceSales : public Sales { public: ServiceSales(string date, int quantity, double amount, string service) : Sales(date, quantity, amount) { this->service = service; } void print() { cout << "Service: " << service << endl; Sales::print(); } private: string service; }; int main() { Sales* sales1 = new ProductSales("2021-01-01", 10,100.0, "Product A"); Sales* sales2 = new ServiceSales("2021-01-02", 5, 50.0, "Service B"); sales1->print(); sales2->print(); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值