[英]Separate classes into separate files in C++
編輯:添加了“添加”類,意外忘記了它。 如何將類分成不同的文件? 我理解類如何工作以及如何制作這樣的對象和事物。 但是,我發現在將類放入不同文件的過程中存在一些混淆。 非常感謝您的幫助! 此外,我使用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;
}
好的,我會通過你的例子告訴你:
subtraction.h
class Subtraction
{
public:
float subtract (float x, float y);
};
subtraction.cxx
#include "subtraction.h"
float Subtraction::subtract (float x, float y)
{
float dif;
dif=x-y;
return dif;
}
multiplication.h
class Multiplication
{
public:
float multiply (float x, float y);
};
multiplication.cxx
#include "multiplication.h"
float Multiplication::multiply (float x, float y)
{
float prod;
prod=x*y;
return prod;
}
等等...
main.cxx
#include "subtraction.h"
#include "multiplication.h"
int main()
{
//use the classes just as before.
}
此外,為了簡單起見,我沒有把它放在這里的代碼中,但是請繼續並養成確保聲明僅包含一次的習慣。 在一個大型項目中,如果你沒有放入這些保護措施,這可能會非常討厭。
#ifndef SUBTRACTION_H
#define SUBTRACTION_H
class Subtraction
{
....
};
#endif /*SUBTRACTION_H*/
這里有Jonathan的例子(為了簡潔,我沒有添加包含警衛,但是確實這樣做了!),但是刪除了一些重復,並為您的學習樂趣添加了一些OO。 請注意,雖然這肯定不是實際計算器的實現方式,但如果您對它進行充分研究,它將有助於您對C ++有更多的了解。
mathoperation.h:
//a base class!
class MathOperation
{
public:
virtual float doit( float x, float y ) const = 0;
};
subrtaction.h:
class Subtraction : public MathOperation
{
public:
float doit( float x, float y ) const;
};
addition.h:
class Addition : public MathOperation
{
public:
float doit( float x, float y ) const;
};
subtraction.cpp:
#include "subtraction.h"
float Subtraction::doit( float x, float y ) const { return x - y; }
addition.cpp:
#include "addition.h"
float Subtraction::doit( float x, float y ) const { return x + y; }
main.cpp中:
#include <iostream>
#include <string>
//yes this saves typing, but there reasons not to use it.. search SO!
using namespace std;
//this one avoids you having to copy/paste the similar parts
void DoIt( const MathOperation& op, const std::string& opName, const std::string& verb, const std::string& resultName )
{
cout << "You have chosen " << opName << "!" << endl;
cout<<"Enter the first number you want to " << verb << ": "<< endl;
//here you should actually check if the user really types in a number, and not "blablabla"
//and off course, put it in a seperate function so you can reuse it for num2
float num1;
cin>>num1;
float num2;
cout<<"Enter the second number you wat to " << verb << ": "<< endl;
cin>>num2;
cout<<"The " << resultName << " is " << op.doit( num1, num2 ) <<endl;
}
int main()
{
int op;
char cont = 'n';
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;
//see how much shorter this is?
if( op == 1 )
DoIt( Addition(), "Addition", "add", "sum" );
else if (op==2)
DoIt( Subtraction(), "Subtraction", "subtract", "difference" );
else
cout << "Sorry I don't know this number" << endl;
cout<<"Do you wish to continue? Y/N"<<endl;
cin>>cont;
} while (cont=='Y'||cont=='y');
cout<<"Thanks for using my program, goodbye!"<<endl;
return 0;
}
以與課程相同的方式命名文件是件好事。 在你的情況下,這不是一個好主意,因為你的類很短,但每個類應該有自己的2個文件 - 例如Subtraction.h和Subtraction.cpp。 在.h文件中,你應該只放入decaration - 在你的情況下: class Subtraction { public: float subtract (float , float); }
class Subtraction { public: float subtract (float , float); }
在.cpp文件中,您應該包含.h文件並放置實現。 在你的情況下:
float Substraction::subtract (float x, float y) { float dif; dif=xy; return dif; }
另請參閱C ++頭文件,代碼分離以及為什么在C ++中有頭文件和.cpp文件?
希望這個對你有幫助! :)
每個文件只有一個公共類是很好的。 按照慣例,filename與classname相同(如果在文件中完成某些定義,則擴展名為.h .hh或.hpp)。
如果你想將不同的類放在不同的文件中,一個簡單的文本編輯器將幫助你做到這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.