如何在不同的 switch case 语句中使用对象的 class 成员函数?

How to use class member function of an object on different switch case statement?

My class-: For Creating, Displaying Diagonal Matrix

class Diagonal {
private:
    int *A;
    int n;
public:
    Diagonal(){
        n=2;
        A = new int[n];
    }
    Diagonal(int n){
        this->n = n;
        A = new  int[n];
    }
    void Create(){
        cout<<"Enter the Elements : "
        for(int i =0; i<=n; i++){
            cin>>A[i-1];
        }
    }
    void Set(int i, int j,  int x){
        if(i==j){
            A[i-1] = x;
        }
    }
    int Get(int i, int j){
    if(i == j){
        return A[i-1];
        }
    else{
        return 0;
        }
    }
    void display(){
        for(int i=1; i<n; i++){
            for(int j=1; j<n; j++){
                if(i==j){
                    cout<<A[i-1]<<" ";
                }
                else{
                    cout<<"0 ";
                }
            }
            cout<<endl;
        }
    }
~Diagonal(){
delete []A;
}
};
void functionName(){
cout<<"----- Functions ------"<<endl;
cout<<"1. Create "<<endl;
cout<<"2. Get "<<endl;
cout<<"3. Set "<<endl;
cout<<"4. Display "<<endl;
cout<<"5. Exit "<<endl;}

Main function with nested do-while loop and switch case :

int main(){
int ch,fun;
 do{
        cout<<"------ Menu --------"<<endl;
        cout<<"1. Diagonal "<<endl;
        cout<<"2. Lower Tri-angular "<<endl;
        cout<<"3. Upper Tri-angular "<<endl;
        cout<<"4. Tri-diagonal"<<endl;
        cout<<"5. Toplitz"<<endl;
        cout<<"6. Exit"<<endl;

        cout<<endl;

        cin>>ch;

        do{
            int n;
        switch(ch)
        {
            case 1: functionName();
            cin>>fun;
                switch(fun){

                     case 1:
                       {
                        cout<<"Enter the size of matrix : " ;
                        cin>>n;
                        Diagonal d(n);
                        d.Create();
                       }
                        break;
                    case 2:
                        //how to call d.get();
                        break;
                    case 3:
                        //how to call d.set();
                        break;
                    case 4:
                        //how to call d.display();
                        break;
                    case 5:
                        break;
            }
            break;
            case 2: functionName();

            break;
            case 3: functionName();

            break;
            case 4: functionName();

            break;
            case 5: functionName();

            break;
        }

    }while(fun<4);

  }while(ch<=5);
 return 0;
 }

我的问题是如何为案例 1 中创建的同一个对象在不同的​​ switch 案例中调用 class 成员函数?

  1. how to call d.get(); in case 2
  2. how to call d.set(); in case 3

当我调用这些成员函数时出现错误,"d is not declared in this scope" 有没有办法在switch语句中调用这些成员函数?

您可以使用唯一指针:

#include <iostream>
#include <memory>
#include <vector>
using std::cin;
using std::cout;
using std::endl;

class Diagonal {
private:
    std::vector<int> A;
public:
    Diagonal() : A(2) {};
    Diagonal(int n) : A(n) {}

    void Create() {
        cout<<"Enter the Elements : ";
        for(std::size_t i = 0; i < A.size(); ++i) {
            cin >> A[i];
        }
    }
    void Set(std::size_t i, std::size_t j,  int x){
        if(i==j){
            A[i-1] = x;
        }
    }
    int Get(std::size_t i, std::size_t j) {
        if(i == j){
            return A[i-1];
        }
        return 0;
    }
    void display() {
        for (std::size_t i = 0; i < A.size(); ++i) {
            for (std::size_t j = 0; j < A.size(); ++j) {
                if (i == j) {
                    cout << A[i] << " ";
                } else {
                    cout<<"0 ";
                }
            }
            cout << endl;
        }
    }
};

void functionName(){
cout<<"----- Functions ------"<<endl;
cout<<"1. Create "<<endl;
cout<<"2. Get "<<endl;
cout<<"3. Set "<<endl;
cout<<"4. Display "<<endl;
cout<<"5. Exit "<<endl;
}

int main(){
    int ch;
    do {
        cout << "------ Menu --------" << endl;
        cout << "1. Diagonal " << endl;
        cout << "2. Lower Tri-angular " << endl;
        cout << "3. Upper Tri-angular " << endl;
        cout << "4. Tri-diagonal" << endl;
        cout << "5. Toplitz" << endl;
        cout << "6. Exit" << endl;
        cout << endl;
        cin >> ch;
        int fun;
        switch (ch) {
        case 1: {
            auto d = std::make_unique<Diagonal>();
            do {
                functionName();
                cin >> fun;
                switch (fun) {
                case 1:
                    cout << "Enter the size of matrix : ";
                    std::size_t n;
                    cin >> n;
                    d = std::make_unique<Diagonal>(n);
                    d->Create();
                    break;
                case 2:
                    //how to call d.get();
                    //d->Get();
                    break;
                case 3:
                    //how to call d.set();
                    //d->Set();
                    break;
                case 4:
                    //how to call d.display();
                    d->display();
                    break;
                case 5:
                    break;
                }
            } while (fun <= 4);
            break;
        }
        case 2:
            functionName();
            break;
        case 3:
            functionName();
            break;
        case 4:
            functionName();
            break;
        case 5:
            functionName();
            break;
        }
    } while (ch <= 5);
    return 0;
}