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

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

第一题:

#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.


#include<bits/stdc++.h>
using namespace std;

class counter
{
 private:
    int n;
 public:
 counter(){}
 counter(int p){n=p;}
 ~counter(){}
 int getn()
 {return n;
 }
 void setn(int p)
 {n=p;
 }
   counter operator+(counter &r)
   {return counter(n+r.getn());
   }

};
int main()
{counter a(1),b(2),c;
       c=a+b;
   cout<<c.getn()<<endl;
}

2.


#include<iostream>
using namespace std;
class Border{
private:
int num;
int *p;
public:
Border(int n=0):num(n),p(new int[n]){ //动态分配内存
for(int i=0;i<n;++i){ //初始化
p[i]=0;
}
}
~Border(){delete[] p;} //析构函数
int& operator [](int n){ //重载,返回引用,以便可以对数组赋值
if(n<num&&n>=0) return p[n];
else {
cerr<<"数组下标越界!";
exit(-1);
}
}
};
int main()
{
Border a(5);
cout<<a[6];
a[2]=5;
cout<<a[2];
}

3.

/*  完整代码  */
 
#include <iostream>
 
using namespace std;
#define MAX_SIZE 5 //数组大小
 
 struct stack {
    int top;
    int ans[MAX_SIZE];
};
typedef struct stack Stack;
 
void InitStack(Stack *ptr) {    //初始化栈
    ptr->top = 0;
}
 
bool StackFull (Stack *ptr) {   //判断栈是否满
    if (ptr->top < MAX_SIZE)
        return false;
    else 
        return true;
}
 
bool StackEmpty (Stack *ptr) {  //判断栈是否空
    if (ptr->top == 0)     
        return true;
    else  
        return false;   
}
 
void Push(Stack *ptr, int item) {   //入栈
    if (StackFull(ptr))
        cerr << "栈满,不能入栈!" << endl;
    else {
        ptr->ans[ptr->top] = item;
        ptr->top ++;
    }
}
 
int GetTop(Stack *ptr) {    //返回栈顶值
    return ptr->ans[ptr->top - 1];
}
 
int Pop(Stack *ptr) {  //出栈,返回栈顶值
    if (StackEmpty(ptr)) {
        cerr << "栈空,不能出栈!" << endl;
        return -1;
    }
    else 
        return ptr->ans[--ptr->top];
}
 
int StackSize (Stack *ptr) {    //返回栈长度
    if (ptr->top == 0)
        return 0;
    else  
        return ptr->top;
}
 
void ClearStack(Stack *ptr) {   //清空栈
    ptr->top = 0;
}
 
 
 
int main(){
    Stack *ptr = (Stack *)malloc(sizeof(Stack));
    InitStack(ptr); //初始化栈
    //入栈
    Push(ptr, 4);
    Push(ptr, 10);
    Push(ptr, 21);
    Push(ptr, 19);
    Push(ptr, 12);
    Push(ptr, 13);
    
    cout << "当前栈顶值为: " << GetTop(ptr) << endl;
    cout << "当前栈顶指针为: " << ptr->top << endl;   //输出栈顶值
 
    cout << "当前栈长度为: " << StackSize(ptr) << endl;
    cout << "栈顶出队: " << Pop(ptr) << endl;   //出栈
 
    cout <<  "当前栈长度为: " << StackSize(ptr) << endl;
 
    ClearStack(ptr);    //清空栈
    cout << "清空栈后栈的长度为: " << StackSize(ptr) << endl;
 
 
    return 0;    
}

随便写写


class Counter
{
    public:
        Counter(int val){this->n=val;}
        void Print(){printf("n = %d",n);}
        Counter operator+(Counter co){return Counter(this->n+co.n);}
    private:  
        int n=0;
};

class Border
{
    public:
        Border(int isize){this->m_size=isize;res.resize(m_size);}
        
        void insert(int val){res.push_back(val);}
        //【】这个没有重载运算符
        //定义一个函数用来实现这个功能吧
        bool isCrossBorder()
        {
            return res.size()>m_size;
        }

    private:
        int m_size=0;
        vector<int> res;
};



int main(void)
{
    // Counter c1(10);
    // Counter c2(20);
    // Counter c3=c1+c2;
    // c3.Print();

    Border bor(10);
    for(int i=0;i<9;++i)
    {
        bor.insert(i);
    }
    printf("越界\n");
    if(bor.isCrossBorder())
    {
        printf("数组越界\n");
    }
}