C++ 如何从类中获取数字?C++;
那么,假设我有一个项目。这个项目使用两个类。第一类菜单类和第二类是五角大楼类 假设我在菜单类中得到输入,我想移动或复制,不管你们说什么,到五角大楼类 你是怎么做到的?下面是我的菜单类的一段代码:C++ 如何从类中获取数字?C++;,c++,C++,那么,假设我有一个项目。这个项目使用两个类。第一类菜单类和第二类是五角大楼类 假设我在菜单类中得到输入,我想移动或复制,不管你们说什么,到五角大楼类 你是怎么做到的?下面是我的菜单类的一段代码: Menu::Menu( void ) { userMenuSelection = Quit; } // Constructor Menu // ===================== Menu::~Menu( v
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;
菜单::菜单(无效){
userMenuSelection=退出;
}//构造函数菜单
// =====================
菜单::~菜单(无效){
cout看来您的问题在于程序设计。如果您希望菜单类代表UI,那么将任何代码从菜单类复制到五角大楼类都是多余的。将它们分开,然后您可以重新使用/扩展菜单类,以包括圆、矩形、圆柱体等
然而,第一件事是——如何做到这一点
您可以让menu类有一个五角大楼成员变量,并执行如下操作
cout << "Please enter an integer value for the length of the sides: ";
cin >> numberA;
thePentagon.SetSidesLength(numberA)
或者您可以在main中声明一个菜单和一个五角大楼,并将指向五角大楼的指针传递给menu类,如下所示
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);
你可以让userMenuSelection成为一个公共静态成员。然后你可以用Menu::userMenuSelection访问它,但是你只能有一个Menu实例。你能提供更多关于这方面的信息吗?@JerryJeremiahMeh pointers。每次我必须使用这样的API时,我都想用一个cluestick猛击作者的头。这是为什么真的吗?我觉得大家都喜欢的空指针访问异常非常有趣。你必须手动确保myMenu
不会在myPentagon
出现之前被销毁,这既繁琐又容易出错。在这个示例中,如果代码变得更复杂,可能更难看到可能发生的潜在问题。如果destmyMenu
的构造函数不调用myPentagon`指针上的delete,并且在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);