m0_63444449 2022-09-03 01:44 采纳率: 79.1%
浏览 101
已结题

这几道课后题该怎样正确程序实现

1.定义一个计数器类Counter,包含私有成员int n、重载运算符“+”,实现对象的相加。
2.c++语言中不会检查数组是否越界。设计类Border,通过重载运算符“【】”检查数组是否越界。
3.数据的进栈,出栈的程序实现

  • 写回答

3条回答 默认 最新

  • guishangppy 2022-09-03 15:06
    关注

    第一题:

    #include <iostream>
    
    using namespace std;
    
    class Count
    {
        public:
            Count(int n):n(n){}           //构造函数
            int operator+(Count obj);       //定义重载'+'号运算符函数
        private:
            int n;
    };
    int Count::operator+(Count obj)         //实现重载函数
    {
        return this->n+obj.n;
    }
    
    int main()
    {
        Count A(10),B(20);
        cout << A+B;
    }
    
    
    

    第二题:

    #include <iostream>
    
    using namespace std;
    
    class Border
    {
        public:
            Border(int size):size(size){}                                //构造函数
            bool operator[](int index);                                 //重载'[]'运算符
        private:
            int size;
    };
    
    bool Border::operator[](int index)                          //重载函数的实现
    {
        if(index >= size)
            return false;              //false代表越界
        else 
            return true;                  //true代表未越界
    }
    
    int main()
    {
        Border A(10);
        cout << A[9] << endl;
    }
    
    
    

    第三题:

    #include <iostream>
    #define SIZE 5
    using namespace std;
    
    
    struct Stack
    {
        int top;
        int arr[SIZE];
    }stack;
    
    
    void Push_stack(int num);           //入栈
    void Pop_stack();                   //出栈
    void Print_stack();                //导出栈
    
    int main()
    {
        stack.top = -1;
        Push_stack(1);
        Push_stack(2);
        Push_stack(3);
        Push_stack(4);
        Push_stack(5);
        Push_stack(6);
        Print_stack();
        Pop_stack();
        Pop_stack();
        Pop_stack();
        Pop_stack();
        
    }
    
    void Push_stack(int num)
    {
        if(stack.top < SIZE-1)
        {
            stack.top++;
            stack.arr[stack.top] = num;   
        }
        else 
            cout << "stack over flow!" << endl;
    }
    
    void Pop_stack()
    {
        if(stack.top != -1)
        {
            stack.top--;
        }
        else
            cout << "stack under flow!" << endl;
    }
    
    void Print_stack()
    {
        for(int i = stack.top; i>=0; i--)
        {
            printf("%d ",stack.arr[i]);
        }
        cout << endl;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 9月16日
  • 已采纳回答 9月8日
  • 创建了问题 9月3日

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路