1.定义一个计数器类Counter,包含私有成员int n、重载运算符“+”,实现对象的相加。
2.c++语言中不会检查数组是否越界。设计类Border,通过重载运算符“【】”检查数组是否越界。
3.数据的进栈,出栈的程序实现
这几道课后题该怎样正确程序实现
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
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; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
- ¥20 Java-Oj-桌布的计算
- ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
- ¥20 有人知道这种图怎么画吗?
- ¥15 pyqt6如何引用qrc文件加载里面的的资源
- ¥15 安卓JNI项目使用lua上的问题
- ¥20 RL+GNN解决人员排班问题时梯度消失
- ¥60 要数控稳压电源测试数据
- ¥15 能帮我写下这个编程吗
- ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路