# "Essential C ++" chapter study notes # object-oriented programming style

Basics

Inheritance mechanism defines the parent-child (parent / child) relationship. Parent (parent) defines all subclasses (children) there are common interfaces (public interface) and private implementation (private implementation). Each subclass can be increased or coverage (override) inherited things, to achieve their own unique behavior. In C ++, the base class is called the parent class (base class), is referred to as a subclass (derive class). The relationship between parent and child classes is called inheritance hierarchies (inheritance hierarchy).

Polymorphism: let pointer or reference to the base class is transparent (transparently) any of its points to an object of a derived class. Prior to the implementation of the program had parse out which function to call, in this way is called static binding (static binding); but in object-oriented programming, the compiler can not know what a particular function is called, when the parsing operation will be delayed until run (run-time) was carried out, this is called dynamic binding (dynamic binding).

Defines an abstract class first step is to find all subclasses of a common operating behavior, then that is trying to figure out what type of operation associated with the behavior (type-dependent), a different class that is derived which must be based on the operating behavior and there are different ways to achieve these operating behavior should be the entire inheritance hierarchy of virtual function (virtual function).

When designing an abstract base class, we need to identify the access level of each operational behavior (access level). If an operation should be allowed to conduct general procedure Jieneng visit, we should declare it as public; but if an operation behavior need not be used outside of the base class, we will declare it as private, even if the base derived classes, they can not access private member in the base class; a level of access is protected, this level of behavior allows derived classes to access, does not allow the general program.

Each virtual function, or must have a defined, can be set to either "pure" virtual function (pure virtual function), if for the class is concerned, this virtual functions and no real meaning, the virtual function assigned to 0, meaning another is that it is a pure virtual function. If there is a declaration of any class (or more) pure virtual function, then, because of their integrity interface (pure virtual function has no definitions, that is not complete), the program can not generate any object it, only this kind (subobject.txt) as a child object of a derived class, the derived class and provided that these must provide a precise definition for all virtual functions. Further according to the general rules, where there is a base class defines (or more) virtual functions, to which should be declared destructor virtual.

Derived class consists of two parts: First, the base class subobject constituted by the non-static data base class member-- if any - composition; second part of a derived class (the class derived from the non

-static data member composition). Before class inheriting declarations, definitions of its base class must already exist.

If a data member is a reference, it must be initialized in the member initialization list constructor's. Once initialized, we can not point to another object. If a data member is a pointer, it is no such restriction: we can be initialized in the constructor, it can first initialized to null, and then later on the other it points to a valid memory address. Our program is based on the design process of these different properties to decide to use reference or pointer.

When we define a derived class, we must decide whether you want to base class virtual function overwritten, or be inherited intact, if we inherited pure virtual functions (pure virtual function), then the derived class will is regarded as an abstract class, it can not be defined as any object it. If we decide to cover the base class virtual function provided, then the new definition of the derived class provides its function runs must be in full compliance function prototype base class declaration, including: the list of parameters, return type, const (const-ness). And when declared operational, does not necessarily have to add the keyword virtual, the compiler will be based on two prototype declaration of a function, a function to decide whether to overwrite the same name as a function of its base class.

The answer exercises

Exercise 5.1 to achieve a two-layer stack (Stack) class system. Which is a pure abstract base class class Stack, only provides simple interface: pop (), push (), size (), empty (), full (), peek () and print (). Two derived classes was LIFO_Stack and Peekback_Stack. Peekback_Stack () allows users without changing stack elements premise access to any element.

#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef string elemType;

class Stack
{
public:
    virtual ~Stack(){}
    virtual bool pop(elemType&) = 0;
    virtual bool push(const elemType&) = 0;
    virtual bool peek(int index, elemType&) = 0;
    virtual int top() const = 0;
    virtual int size() const = 0;
    virtual bool empty() const = 0;
    virtual bool full() const = 0;
    virtual void print(ostream& = cout) const = 0;
};

ostream& operator<<(ostream& os, const Stack& rhs)
{
    rhs.print();
    return os;
}

class LIFO_Stack :public Stack
{
public:
    LIFO_Stack(int capacity = 0) :_top(0)
    {
        if (capacity)
            _stack.reserve(capacity);
    }
    int size() const { return _stack.size(); }
    bool empty()const { return !_top; }
    bool full() const { return size() >= _stack.max_size(); }
    int top() const { return _top; }
    void print(ostream& os = cout) const;
    bool pop(elemType& elem);
    bool push(const elemType& elem);
    bool peek(int, elemType&) { return false; }

private:
    vector<elemType> _stack;
    int _top;
};

bool LIFO_Stack::pop(elemType& elem)
{
    if (empty()) return false;
    elem = _stack[--_top];
    _stack.pop_back();
    return true;
}

bool LIFO_Stack::push(const elemType& elem)
{
    if (full())    return false;
    _stack.push_back(elem);
    ++_top;
    return true;
}

void LIFO_Stack::print(ostream& os) const
{
    vector<elemType>::const_reverse_iterator rit = _stack.rbegin(),
        rend = _stack.rend();
    os << "\n\t";
    while (rit != rend)
    {
        os << *rit++ << "\n\t";
    }
    os << endl;
}

class Peekback_Stack :public Stack
{
public:
    Peekback_Stack(int capacity = 0) :_top(0)
    {
        if (capacity)
            _stack.reserve(capacity);
    }
    int size() const { return _stack.size(); }
    bool empty()const { return !_top; }
    bool full() const { return size() >= _stack.max_size(); }
    int top() const { return _top; }
    void print(ostream& os = cout) const;
    bool pop(elemType& elem);
    bool push(const elemType& elem);
    bool peek(int, elemType&);
private:
    vector<elemType> _stack;
    int _top;
};

bool Peekback_Stack::pop(elemType& elem)
{
    if (empty()) return false;
    elem = _stack[--_top];
    _stack.pop_back();
    return true;
}

bool Peekback_Stack::push(const elemType& elem)
{
    if (full())    return false;
    _stack.push_back(elem);
    ++_top;
    return true;
}

void Peekback_Stack::print(ostream& os) const
{
    vector<elemType>::const_reverse_iterator rit = _stack.rbegin(),
        rend = _stack.rend();
    os << "\n\t";
    while (rit != rend)
    {
        os << *rit++ << "\n\t";
    }
    os << endl;
}

bool Peekback_Stack::peek(int index, elemType& elem)
{
    if (empty())
        return false;
    if (index < 0 || index >=size ())
         return  to false ; 
    elem = _stack The [index];
     return  to true ; 
} 

// non-Member function PEEK () accepts a "Stack of abstract class reference" as a parameter,
 // and call objects in the Stack function virtual function peek () - this is the virtual function specific factions derived classes. 
void PEEK (Stack & ST, int index) 
{ 
    COUT << endl;
     String T;
     IF (st.peek (index, T)) 
        COUT << " PEEK: " << T;
     the else 
        COUT << " PEEK failed! ";
    cout << endl;
}

int main()
{
    LIFO_Stack st;
    string str;
    while (cin >> str && !st.full())
        st.push(str);
    cout << '\n' << "About to call peek() with LIFO_Stack" << endl;
    peek(st, st.top() - 1);
    cout << st;
    
    Peekback_Stack pst;
    while (!st.empty())
    {
        string t;
        if (st.pop(t))
            pst.push(t);
    }
    cout << "About to call peek() with Peekback_Stack" << endl;
    peek(pst, pst.top() - 1);
    cout << pst;

    return 0;
}

end。

"Knowledge deliberative, discernment resourceful."

Guess you like

Origin www.cnblogs.com/zhuifeng17/p/12307402.html