*************
C++
topic: 类的嵌套
*************
Hi there, too many works last week and have some doubts about the nesting of the classes. Talk about the calculator progran since I have write the little program brfore my 优快云.
#include <iostream>
int main() {
int num1, num2;
std::cout << "请输入第一个整数: ";
std::cin >> num1;
std::cout << "请输入第二个整数: ";
std::cin >> num2;
std::cout << "两个整数的和为: " << num1 + num2 << std::endl;
std::cout << "两个整数的差为: " << num1 - num2 << std::endl;
std::cout << "两个整数的积为: " << num1 * num2 << std::endl;
if(num2 != 0)
std::cout << "两个整数的商为: " << num1 / num2 << std::endl;
else
std::cout << "除数不能为0" << std::endl;
return 0;
}
the code above is easy to understand wthout aesthetic. Saperate the function from main code is a smart way to write code.
The main code is something about the interface of the program, which plays roal of communicating with uses. The main programe tells the compiler what to do.
int main()
{
int num1, num2;
char operation;
cout << "欢迎使用高级计算器!\n";
cout << "请输入两个整数(用空格分隔):";
cin >> num1 >> num2;
cout << "请输入运算符 (+, -, *, /):";
cin >> operation;
calculate(num1, num2, operation);
return 0;
}
And the unction calculate is do the specific algorithm.
int main()
{
int num1, num2;
char operation;
cout << "欢迎使用高级计算器!\n";
cout << "请输入两个整数(用空格分隔):";
cin >> num1 >> num2;
cout << "请输入运算符 (+, -, *, /):";
cin >> operation;
calculate(num1, num2, operation);
return 0;
}
// 执行计算并调用结果打印函数
void calculate(int a, int b, char op)
{
int result;
switch (op)
{
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
if (b != 0)
{
result = a / b;
}
else
{
cout << "错误:除数不能为零!" << endl;
return;
}
break;
default:
cout << "非法运算符!" << endl;
return;
}
printResult(result, a, op, b);
}
In the program, declare the variables of the functions.
#include <iostream>
using namespace std;
// 函数声明
void calculate(int a, int b, char op);
void printResult(int result, int a, char op, int b);
int main()
{
int num1, num2;
char operation;
cout << "欢迎使用高级计算器!\n";
cout << "请输入两个整数(用空格分隔):";
cin >> num1 >> num2;
cout << "请输入运算符 (+, -, *, /):";
cin >> operation;
calculate(num1, num2, operation);
return 0;
}
// 执行计算并调用结果打印函数
void calculate(int a, int b, char op)
{
int result;
switch (op)
{
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
if (b != 0)
{
result = a / b;
}
else
{
cout << "错误:除数不能为零!" << endl;
return;
}
break;
default:
cout << "非法运算符!" << endl;
return;
}
printResult(result, a, op, b);
}
// 打印计算结果
void printResult(int result, int a, char op, int b)
{
cout << "计算结果:";
cout << a << " " << op << " " << b << " = " << result << endl;
}
Here is the whole code to run calculating.
![]() |
add a class is very easy. Name the class calculator.
#include <iostream>
using namespace std;
class Calculator
{
private:
int num1;
int num2;
char operation;
public:
// 获取用户输入
void getInputs()
{
cout << "请输入两个整数(用空格分隔):";
cin >> num1 >> num2;
cout << "请输入运算符 (+, -, *, /):";
cin >> operation;
}
// 执行计算
void calculate()
{
int result;
switch (operation)
{
case '+':
result = num1 + num2;
printResult(result, operation);
break;
case '-':
result = num1 - num2;
printResult(result, operation);
break;
case '*':
result = num1 * num2;
printResult(result, operation);
break;
case '/':
if (num2 != 0)
{
result = num1 / num2;
printResult(result, operation);
}
else
{
cerr << "错误:除数不能为零!" << endl;
}
break;
default:
cerr << "非法运算符!" << endl;
}
}
// 打印结果
void printResult(int result, char op)
{
cout << "计算结果:";
cout << num1 << " " << op << " " << num2 << " = " << result << endl;
}
};
int main()
{
Calculator calc;
calc.getInputs();
calc.calculate();
return 0;
}
Tell you something interesting. The main function is outside the calculator class.When calling functions, need to creat an object of the class. Then the object call the functions. Donot think about why use objects.
![]() |
nesting a class in calculator seems difficult. Swich another sample.
假设我有一个“在别处商业KTV”(ElseWhereCommercialKTV
)的外部类,里面不仅有各种公主,还有一个隐藏功能(HiddenFeatures
)的嵌套类。隐藏功能通常只有在操作KTV对象时(充卡成为SSSVIP)才需要使用,可以通过嵌套类来实现。
#include <iostream>
#include <string>
using namespace std;
// 定义外部类:CommercialKTV
class ElseWhereCommercialKTV
{
private:
// 公主们的名字
string princessNames[3] = {"Princess A", "Princess B", "Princess C"};
// 定义嵌套类:隐藏功能
class HiddenFeatures
{
private:
// 特殊工具的详细信息
string specialTool1 = "Play with me ba"; // 快来和妲己玩耍吧
string specialTool2 = "heiheihei"; // 污妖王
public:
// 获取工具名称
string getToolName(int toolIndex)
{
if (toolIndex == 1)
{
return specialTool1;
}
else if (toolIndex == 2)
{
return specialTool2;
}
else
{
return "Unknown Tool";
}
}
// 使用工具的功能
void useTool(int toolIndex)
{
if (toolIndex == 1)
{
cout << "Using " << specialTool1 << " Play with me ba." << endl;
}
else if (toolIndex == 2)
{
cout << "Using " << specialTool2 << " heiheihei." << endl;
}
else
{
cout << "Unknown Tool Selected." << endl;
}
}
};
// 定义一个对象成员变量,该成员属于嵌套类
HiddenFeatures SSSVIP;
public:
// 在外部类中调用嵌套类的方法
void activateHiddenFunction(int toolIndex)
{
cout << "Activating Hidden Function... " << endl;
cout << "Tool Name: " << SSSVIP.getToolName(toolIndex) << endl;
SSSVIP.useTool(toolIndex);
}
// 公主的欢迎语
void welcome()
{
cout << "Welcome toElseWhere Commercial KTV!" << endl;
cout << "Available Princesses: " << endl;
for (int i = 0; i < 3; ++i)
{
cout << "- " << princessNames[i] << endl;
}
}
};
int main()
{
// 创建KTV对象
ElseWhereCommercialKTV ktv;
// 调用公主欢迎语
ktv.welcome();
cout << endl;
// 调用隐藏功能
ktv.activateHiddenFunction(1); // 使用工具1
ktv.activateHiddenFunction(2); // 使用工具2
return 0;
}