如何从班级中获取数字?C++
How to get a number from a class? C++
假设我有一个项目。此项目使用两个类。第一类是菜单类,第二类是五角大楼类。
假设我在菜单类中得到输入,我想移动或复制,无论你说什么,到五角大楼类。
你是怎么做到的?这是我的MenuClass中的一段代码:
Menu::Menu( void ) {
userMenuSelection = Quit;
} // Constructor Menu
// =====================
Menu::~Menu( void ) {
cout << "====================================" << endl;
} // Destructor ~Menu
// =====================
// ==============================
// Accessor Member-Function Get()
// ==========================
MenuChoices Menu::Get( ) {
return userMenuSelection;
} // Accessor Method Get
// ========================
// =============================
// Mutator Member-Function Set()
// ========================================
void Menu::Set( MenuChoices newValue ) {
userMenuSelection = newValue;
} // Mutator Method Set
// =======================
// ==========================
// Member-Function Display( )
// ==========================
void Menu::Display( ) {
cout << "======================================" << endl;
cout << " MENU SELECTION " << endl;
cout << "======================================" << endl;
cout << "1: Calculate the Perimeter of Pentagon" << endl;
cout << "2: Calculate the Area of Pentagon" << endl;
cout << "3: Quit" << endl;
cout << "======================================" << endl;
cout << endl;
} // Member-Function Display
// ============================
// =========================
// Member-Function QueryUser
// =========================
void Menu::QueryUser( ) {
int selection;
cout << "Enter Menu Selection: ";
cin >> selection;
switch (selection){
case 1: userMenuSelection = Perimeter;
break;
case 2: userMenuSelection = Area;
break;
case 3: userMenuSelection = Quit;
default: userMenuSelection = Quit;
} // switch
// ===========
cout << endl;
} // Method QueryUser()
// =======================
// =================
// Method Continue()
// ========================
bool Menu::Continue( ) {
return userMenuSelection != Quit;
} // Method Continue
// ====================
// ==============================
// Member-Function ProcessCommand
// ==============================
void Menu::ProcessCommand( ) {
int numberA; // Length of Sides
if (userMenuSelection == Quit ){
cout << "Thank you for using this type of program. Have a nice day!" << endl;
}
else if (userMenuSelection != Quit) {
cout << "Please enter an integer value for the length of the sides: ";
cin >> numberA;
因此,每当我将这些输入移动/复制到五角大楼类时,我都希望它这样做,例如:
void Pentagon::ProcessCommand( ) {
int numberA; // Length of Sides
if (userMenuSelection == Quit ){
cout << "Thank you for using this type of program. Have a nice day!" << endl;
}
else if (userMenuSelection != Quit) {
cout << "Please enter an integer value for the length of the sides: ";
cin >> numberA;
// ==============================
switch ( userMenuSelection ) {
case Perimeter:
cout << "Perimeter = " << (5 * numberA) << endl;
break;
case Area:
cout << "Area = " << numberA * numberA * sqrt(25.0 + 10.0 * sqrt(5.0)) / 4.0;
break;
default: cout << "Warning: error state encountered." << endl;
}
cout << endl;
}
}
然后在main()中输出这个区域和东西;
有什么想法吗?谢谢!
看来你的问题是程序设计的问题之一。如果希望菜单类表示 UI,则将任何代码从 Menu 类复制到五角形类是多余的。将它们分开,然后您可以重用/扩展菜单类以包括圆形、矩形、圆柱体等。
然而,第一件事是第一件事 - 如何做到这一点。
你可以让菜单类有一个五边形成员变量,并做类似的事情
cout << "Please enter an integer value for the length of the sides: ";
cin >> numberA;
thePentagon.SetSidesLength(numberA)
哪里
void Pentagon::SetSidesLength(int length)
{
this.length = length;
}
或者你可以在main中声明一个菜单和一个五边形,并将指向五边形的指针传递给菜单类,如下所示
class Menu
{
private:
//some other stuff omitted for brevity
Pentagon* thePentagon;
public:
//constructor
Menu(Pentagon* pentaPointer) : thePentagon(pentaPointer) {};
}
//main
int main()
{
Pentagon myPentagon;
Menu myMenu(&myPentagon);
.
.
.
}
使用五边形的成员函数时,如果您不知道,请使用 -> 运算符
thePentagon->SetSidesLength(a);
相关文章:
- QChar 如果 'isDigit()' 则获取数字值
- 从(排序的)数字列表中获取数字范围
- 如何以正确的时区从XTS索引中获取数字时间?
- 如何获取数字的更改
- 如何仅从字符串中获取数字
- 获取数字小数部分的最佳方法
- 获取数字最左边两个位的值的最便宜的方法是什么?
- 从.txt文件中获取数字,并将其放入C 中的向量
- 如何从文件中获取数字行并忽略第一个数字
- 无法从字符串获取数字输入
- 欧拉项目 #3 获取数字的最大质因数
- 从 JNI 中的原语获取数字的简单方法
- 如何按位获取数字而不是求反符号位
- C 如何在文件中的特定字符串之后获取数字
- 获取数字序列的功能
- 使用sscanf c++检查格式并获取数字
- 如何从 9 和 90 中提取 2.9 和 2.09,即获取数字等于最大编号的小数部分.二分位数之后的数字
- 如何从班级中获取数字?C++
- 如何从字符串中获取数字
- 从用户输入中获取数字,直到用户类型"quit"