C++ implements dynamic binding code sharing

  • 2020-05-05 11:41:00
  • OfStack

C++ implements dynamic binding code sharing


#include <iostream>
#include<string>
using namespace std;
class BookItem
{
private:
  string bookName;
  size_t cnt;
public:
  BookItem(const string&s,size_t c,double p):
    bookName(s),cnt(c),price(p)
    {}
  ~BookItem(){}
protected:
  double price;
public:
  double bookPrice()
  {
    return this->price;
  }
  string getBookName()
  {
    return this->bookName;
  }
  size_t getBookCount()
  {
    return this->cnt;
  }
  virtual double money()
  {
     return cnt*price;
  }
  virtual void costMoney()
  {
    cout<<money()<<endl;
  }
};
class BookBatchItem:public BookItem
{
private:
  string bookName;
  size_t cnt;
public:
  BookBatchItem(const string&s,size_t c,double p,double discountRate):
    BookItem(s,c,p),cnt(c),discount(discountRate)
    {}
  ~BookBatchItem(){}
private:
  double discount;
public:
  double money()
  {
    if(cnt>=10)
      return cnt*price*(1.0-discount);
    else
      return cnt*price;
  }
  void costMoney()
  {
    cout<<money()<<endl;
//    cout<<cnt<<endl;
//    cout<<price<<endl;
//    cout<<discount<<endl;
//    cout<<"..."<<endl;
  }
};
int main()
{
  BookItem b1("Uncle Tom's house",11,12.5);
  b1.costMoney();
  BookBatchItem b2("Gone with wind",11,12.5,0.12);
  b2.costMoney();
  BookItem* pb=&b1;
  pb->costMoney();
  pb=&b2;
  pb->costMoney();
  return 0;
}

Only "pointer ->" is used Function () or reference. Function () calls virtual functions in C++ class to perform dynamic binding.

In the following code, an java or C# programmer is prone to making an error.


 class Base
 {
 public:
   Base() { p = new char ; }
   ~Base() { delete p; }
 private:
   char * p ;
 };
 
 class Derived:public Base
 {
 public:
   Derived() { d = new char[10] ; }
   ~Derived() { delete[] d; }
 private:
   char * d ;
 };
 
 int main()
 {
   Base *pA = new Derived();
   delete pA ;
   
   Derived *pA = new Derived();
   delete pA ;
 }

In code:
When delete pA is executed, the ~Base destructor is directly executed, and the ~Derived destructor is not executed, because the destructor is not a virtual function.
When executing delete pB, execute ~Derived() and then ~Base().
In contrast, all function calls in java and C# are dynamically bound.

Regarding the member function call and binding mode of C++, the following code can be tested:


 class Base
 {
 public:
   virtual void Func() { cout<<"Base"<<endl; }
 };
 
 class Derived:public Base
 {
 public:
   virtual void Func() { cout<<"Derived"<<endl; }
 };
 
 int main()
 {
   Derived obj;
   Base * p1 = &obj;
   Base & p2 = obj;
   Base obj2 ;
 
   obj.Func() ;  // Static binding, Derived the func
   p1->Func();   // Dynamic binding, Derived the func
   (*p1).Func();  // Dynamic binding, Derived the func
   p2.Func();   // Dynamic binding, Derived the func
   obj2.Func();  // Static binding, Base the func
 
   return 0 ;
 }

You can see that "object name.function ()" belongs to the static binding, of course, the way to use Pointers to objects should fall into the category of pointer calls, as "class name :: function ()" belongs to the static binding without a doubt.


Related articles: