[C++] OOP

There is a base class at the root of the hierarchy, from which the other class inherit, directly or indirectly. 

These inheriting classes are known as derived calsses.

The key ideas in Object-Oriented Programming are data abstraction, inheritance and dynamic binding.

Using data abstraction, we only expose the interface and hide the detail implementation.

Through inheritance, we define classes that model relationships among similar types.

Through dynamic binding, we can use the objects of these types and ignore the detail how they differ.

Dynamic binding

In C++, dynamic binding happens when a virtual function is called through a reference(or pointer) to a base class.

class Quote{
public:
    virtual double net_price(size_t n) const;
}

class Bulk_quote : public Quote{
public:
    double net_price(size_t n) const;
}

void print_total(ostream & os, const Quote & item, size_t n){
    os << item.net_price(n); 
}

print_total(cout, base, 3);    // base has Quote type, call Ouote version
print_total(cout, bulk, 3);    // bulk has Bulk_quote, call bulk_quote version

The decision as to which version to run depends on the type of argument at run time. Therefore, dynamic binding is known as runtime binding.

Basic class ordinarily should define a virtual destructor. Virtual destructor are needed even if they do not work.

Any non-static member function, other than constructor, maybe virtual. The virtual keyword only appear on the declaration inside the class, and may not appear on the definition that apper outside the class body.

A function that is virtual in base class is implicitly virtual in the derived classess as well.

Member functions that are not declared as virtual are resolved at compile time, not run time.

If a derived class do not override the virtual from its base, then like any other member, the derived class inherit the version defined in the base class.

A derived class contains subobject corresponding to its base class. We can an object of a derived class as if it was an object of its base class. We can bind an reference(or pointer) to the base-class part of a derived class.

    Quotes item;        // an object of base type
    Bulk_quote bulk;      // an object of derived type
    Quotes *p = &item;    // p points to a Quote object
    p = &bulk;         // p points the Quote part of bulck
    Quote & ref = bulk;    // ref bound to the Quote part of bulck

The fact that the derived object contains subobjects for its base classes is the key to how inheritance work.

Like any ohter code that create an objec to the base-class type, a derived class must use a base-class constructor to initialize its base-class part.

Bulk_quote(const string& bookNo, double price, size_t qty, double disc): Quote(bookNo, price), min_qty(qty), discount(dis) {}

Process flow:

  1. The Quote constructor initializes the base-class part of the Bulk_quote. E.g. the bookNO and price members
  2. execute the empty Quote constructor body
  3. Initilize the direct member min_qty and discount
  4. execute the empty function body of Bulk_quote constructor

The keyword final prevent a class from being used as a base class

    class NoDerived final { /*  */};
    class DerivedType : NoDerived { /* */}; // error NoDerived is final

The static type is the type of a variable or other expression. The static type of an express is always known at compile time - it is the type with which a variable is declared or an expression yields.

The dynamic type is the type of an object. The dynamic type is the type of object in momery that the variable or expression represents. The dynamic type may not know until run time.

There is a implicit(automatic) conversion from base to derived. A base object may not contain members defined by the derived class. 

    Quote base;
    Bulk_quote * bulk = &base;     // error: cannot convert base to derived
    Bulk_quote & bulkRef = base;    // error: cannot convert base to derived

If we know the conversion is safe, we can use static_cast to make the conversion.

When we initialize an object of base type from an object of derived type, only the base part of the derived object is copied, moved or assigned. The derived part of the derived object is ignored.

Reference:

C++ Primer, Fifth Edition, chapter 15 Object-Oriented Programming

原文地址:https://www.cnblogs.com/TonyYPZhang/p/6557698.html