编辑:添加了“加法”类,不小心忘记了。如何将类分成不同的文件?我了解类的工作原理以及如何制作对象和类似的东西。但是,我在将类放入不同文件的过程中发现了一些混乱。非常感谢您的帮助!另外,我使用 CodeBlocks 作为 IDE。到目前为止,这是我的理解:
到目前为止,这是我的理解,但我只是对如何实现它感到困惑。我创建了一个简单的计算器,其中包含一个源文件中的所有类。它工作得很好,我想我为此感到非常自豪:) 我知道有很多更简单的方法来制作这样的东西,但我使用类和对象来代替练习的唯一目的。这是我的计算器代码。另外,我真的为这篇长篇文章道歉:/谢谢,如果你读了这么远,特别感谢你能帮我一点!
这是我在一个源文件中的代码:
#include <iostream>
using namespace std;
class Addition {
public:
float add (float x, float y) {
float sum;
sum=x+y;
return sum;
}
};
class Subtraction {
public:
float subtract (float x, float y) {
float dif;
dif=x-y;
return dif;
}
};
class Multiplication {
public:
float multiply (float x, float y) {
float prod;
prod=x*y;
return prod;
}
};
class Division {
public:
float divide (float x, float y) {
float quot;
quot=x/y;
return quot;
}
};
int op;
char cont;
int main()
{
do {
cout<<"Welcome to C++ Calculator v2!"<<endl;
cout<<"Select the number for which operation you want to use: "<<endl;
cout<<"1-Addition"<<endl;
cout<<"2-Subtraction"<<endl;
cout<<"3-Mutliplication"<<endl;
cout<<"4-Division"<<endl;
cin>>op;
if (op==1) {
float num1;
float num2;
Addition addobj;
cout<<"You have chosen Addition!"<<endl;
cout<<"Enter the first number you want to add: "<<endl;
cin>>num1;
cout<<"Enter the second number you wat to add: "<<endl;
cin>>num2;
float ans=addobj.add(num1, num2);
cout<<"The sum is "<<ans<<endl;
cout<<"Do you wish to continue? Y/N"<<endl;
cin>>cont;
}
if (op==2) {
float num1;
float num2;
Subtraction subobj;
cout<<"You have chosen Subtraction!"<<endl;
cout<<"Enter the first number you want to subtract: "<<endl;
cin>>num1;
cout<<"Enter the second number you want to subtract: "<<endl;
cin>>num2;
float ans=subobj.subtract(num1, num2);
cout<<"The difference is "<<ans<<endl;
cout<<"Do you wish to continue? Y/N"<<endl;
cin>>cont;
}
if (op==3) {
float num1;
float num2;
Multiplication multobj;
cout<<"You have chosen Multiplication!"<<endl;
cout<<"Enter the first number you want to multiply: "<<endl;
cin>>num1;
cout<<"Enter the second number you want to multiply: "<<endl;
cin>>num2;
float ans=multobj.multiply(num1, num2);
cout<<"The product is "<<ans<<endl;
cout<<"Do you wish to continue? Y/N"<<endl;
cin>>cont;
}
if (op==4) {
float num1;
float num2;
Division divobj;
cout<<"You have chosen Division!"<<endl;
cout<<"Enter the first number you want to divide: "<<endl;
cin>>num1;
cout<<"Enter the second number you want to divide: "<<endl;
cin>>num2;
float ans=divobj.divide(num1, num2);
cout<<"The quotient is "<<ans<<endl;
cout<<"Do you wish to continue? Y/N"<<endl;
cin>>cont;
}
} while (cont=='Y'||cont=='y');
if (cont=='N'||'n') {
cout<<"Thanks for using my program, goodbye!"<<endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
好的,我将通过您的示例向您展示:
减法.h
class Subtraction
{
public:
float subtract (float x, float y);
};
Run Code Online (Sandbox Code Playgroud)
减法.cxx
#include "subtraction.h"
float Subtraction::subtract (float x, float y)
{
float dif;
dif=x-y;
return dif;
}
Run Code Online (Sandbox Code Playgroud)
乘法.h
class Multiplication
{
public:
float multiply (float x, float y);
};
Run Code Online (Sandbox Code Playgroud)
乘法.cxx
#include "multiplication.h"
float Multiplication::multiply (float x, float y)
{
float prod;
prod=x*y;
return prod;
}
Run Code Online (Sandbox Code Playgroud)
等等...
主文件
#include "subtraction.h"
#include "multiplication.h"
int main()
{
//use the classes just as before.
}
Run Code Online (Sandbox Code Playgroud)
此外,为了简单起见,我没有将它放在此处的代码中,但是继续并养成确保您的声明只包含一次的习惯。在大型项目中,如果您不采取这些保护措施,这会变得非常糟糕。
#ifndef SUBTRACTION_H
#define SUBTRACTION_H
class Subtraction
{
....
};
#endif /*SUBTRACTION_H*/
Run Code Online (Sandbox Code Playgroud)