如何在不同的开关大小写语句上使用对象的类成员函数?

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

本文关键字:对象 函数 成员 语句 大小写 开关      更新时间:2023-10-16

>我的类-: 用于创建、显示对角矩阵

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

具有嵌套 do-while 循环和开关情况的主要功能:

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 中创建的同一 obj 在不同的开关情况下调用类成员函数?

  1. 如何调用 d.get((; 在案例 2 中
  2. 如何在案例 3 中调用 d.set((

当我调用这些成员函数时发生错误,"d未在此范围内声明" 有没有办法在开关语句中调用这些成员函数?

您可以使用唯一的指针:

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