简体   繁体   中英

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

My question is how to call class members functions in different switch cases for the same obj created in case 1?

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

when I call these member functions error occurs, "d is not declared in this scope" is there any way to call these member functions in switch statement?

You can use a unique pointer:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM