C++代码编程学习:基于对象的编程风格——习题5.1(Essential C++ 第五章)

C++中面向对象编程风格的学习,非常有难度,难度指数拉满,这里面虚函数真是一个非常让人头疼的东西,我一般不用,所以学起来很吃力,这道习题帮了我很多,多做多学吧,学海无涯!

一、前言

  C++中面向对象编程风格的学习(Essential C++ 第五章)。

二、例题

-P238 练习 5.1
  实现一个两层的 stack(堆栈)类体系。其基类是个纯抽象类 stack,只提供最简单的接口: pop()、push()、size()、empty()、full()、peek()和print() 。两个派生类则为LIFO_stack 和Peekbackstack。peekback stack可以让用户在不更改stack元素的前提下,访问任何一个元素。

先分析一下,一共有这几件事情要做:

    1. 基类是个纯抽象类 stack,只提供最简单的接口: pop()、push()、size()、empty()、full()、peek()和print()
    1. 派生类LIFO_stack;
    1. 派生类Peekbackstack;
    1. peekback stack可以让用户在不更改stack元素的前提下。

原书作者解析到:两个派生类都以vector来负责元素储存任务。为了打印vector的内容,我采用一个const_reverse_iterator,此iterator可以逆向方式由尾端至前端遍历整个vector。

代码如下:


#include <string>
#include <iostream>
#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)
    {
        // 逆向方式由尾端至前端遍历整个vector
        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 index, elemType&) { return false; }

private:
    vector<elemType> _stack;
    int _top;

};


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;
}

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;
}


class Peekback_Stack : public Stack
{
public:
    Peekback_Stack(int capacity = 0) : _top(0)
    {
        // 逆向方式由尾端至前端遍历整个vector
        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 index, elemType& elem);

private:
    vector<elemType> _stack;
    int _top;

};

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::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;
}


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

void peek(Stack& st, int index)
{
    cout << endl;
    string t;
    if (st.peek(index, t))
        cout << "peek: " << t;
    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 << '\n' << "About to call peek() with Peekback_Stack" << endl;
    peek(pst, pst.top() - 1);
    cout << pst;


}

在这里插入图片描述

代码是在 visual studio 中编写的,该软件还是比较好用的,我安装的是2022专业版;

共勉!

相关推荐

  1. C语言——习题

    2024-07-16 08:20:06       25 阅读
  2. C++Primer Plus结构编程练习8

    2024-07-16 08:20:06       30 阅读
  3. C++ primer plus 编程练习

    2024-07-16 08:20:06       98 阅读
  4. C++Primer Plus编程题()

    2024-07-16 08:20:06       24 阅读
  5. 2.面向对象编程风格

    2024-07-16 08:20:06       46 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-16 08:20:06       70 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-16 08:20:06       74 阅读
  3. 在Django里面运行非项目文件

    2024-07-16 08:20:06       62 阅读
  4. Python语言-面向对象

    2024-07-16 08:20:06       72 阅读

热门阅读

  1. Spring的启动过程

    2024-07-16 08:20:06       24 阅读
  2. 并发编程-锁的分类

    2024-07-16 08:20:06       29 阅读
  3. Gmsh教程

    2024-07-16 08:20:06       31 阅读
  4. Redis是什么

    2024-07-16 08:20:06       28 阅读
  5. 机器学习——机器学习概述

    2024-07-16 08:20:06       22 阅读
  6. 深入理解 Vue.js 的生命周期:从创建到销毁

    2024-07-16 08:20:06       27 阅读
  7. 2024.7.10 day 3 比赛总结

    2024-07-16 08:20:06       21 阅读
  8. 大模型 GPT 到 GPT-3.5 知识点总结

    2024-07-16 08:20:06       23 阅读