【问题标题】:Separate classes into separate files in C++在 C++ 中将类分成单独的文件
【发布时间】:2012-08-09 13:38:31
【问题描述】:

编辑:添加了“addition”类,不小心忘记了。 如何将类分成不同的文件?我了解类如何工作以及如何制作对象和类似的东西。但是,我在将类放入不同文件的过程中发现了一些混乱。非常感谢您的帮助!另外,我使用 CodeBlocks 作为 IDE。到目前为止,这是我的理解:

  1. 创建新类,CB 为您提供一个“.h”和一个新的“.cpp”。
  2. 源文件开头的“Classname::Classname”是范围解析运算符。
  3. 您在主源文件中使用“#include classname.h”来导入其内容。
  4. 您可以使用您已声明的对象从“main”调用函数。

这是我目前的理解,但我只是对如何实现它感到困惑。我创建了一个简单的计算器,所有类都在一个源文件中。它工作得很好,我想我对此感到非常自豪:) 我知道有很多更简单的方法可以制作这样的东西,但我使用类和对象只是为了练习。这是我的计算器代码。另外,我真的为这篇长文道歉:/谢谢,如果你能读到这里,特别感谢你能帮我一点忙!

这是我在一个源文件中的代码:

#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;

}

【问题讨论】:

  • 字数过多,请编辑您的帖子以便更好地阅读
  • 抱歉,由于某种原因,我无法让编号列表工作,知道怎么做吗?
  • 你需要分开声明(.h)文件和实现(.cpp)文件。
  • 既然您是初学者,这里有一个提示:每次当您复制/粘贴(在您的代码中很多)时,请三思而后行:您不是在复制功能吗?如果答案是肯定的,则将该功能放在单独的函数或类中。
  • 查看编号列表的编辑:在它们之前需要一个额外的换行符

标签: c++ class


【解决方案1】:

好的,我将通过您的示例向您展示:

减法.h

class Subtraction 
{
public:
 float subtract (float x, float y);
};

减法.cxx

#include "subtraction.h"

float Subtraction::subtract (float x, float y)
{     
  float dif;
  dif=x-y;
  return dif;    
}

乘法.h

class Multiplication 
{
public:
  float multiply (float x, float y);
};

乘法.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*/

【讨论】:

  • 哇非常感谢!!!这清除了很多事情!感谢您花时间为我写这篇文章。而且,我假设“.cxx”和“.cpp”是一样的?
  • @Carpetfizz,是的,您可以使用 .cc、.cpp、.cxx、.c++ 等等,但这些是主要的扩展名。
  • 好的,所以我尝试了您所说的仅用于一次操作(加法)的操作,并且几乎完全按照您所做的操作。但是,我主要删除了 Addition 类(以前存在),因为它现在有自己的类。但是,CodeBlocks 指向我的“#include
  • @Carpetfizz 它应该是 #include "Addition.h" 而不是 #include
  • 现在完美运行!非常感谢乔纳森·汉森!我也向 stjin 提出了这个问题,是否可以将两个答案标记为已解决?你们俩都给了我很多知识,为我清理了一些东西。
【解决方案2】:

这里有类似 Jonathan 的示例(为简洁起见,我没有添加包含保护,但绝对要这样做!),但删除了一些重复项并添加了一些 OO 以供您学习。请注意,虽然这肯定不是实际计算器的实现方式,但如果您学得足够好,它有望让您对 C++ 有更多的了解。

数学运算.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;
};

减法.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;
}

【讨论】:

  • 我认为这是针对有关设计、继承或多态性的不同问题的合理建议。不确定它在这里是否有帮助,因为它可能会使事情变得混乱。无论如何,我喜欢它,这是一个很好的建议。 +1
  • 非常感谢 stijn!这对我来说将是一个很好的学习工具,我可以在 SO 中“检查”多个答案吗?
  • 不,只能有一个答案。但无论如何都要选择@JonathanHenson 的答案,而不是我的:他是第一个并且准确地回答了你的问题。我只是添加了一些建议等,因为我有点无聊,而您似乎很想学习:]
  • 嘿 stijn,非常感谢你让我感觉更好,因为不能同时选择你们两个 :) 我希望你知道你给了我和约翰一样多的知识,并且会回来当我准备好学习有关课程的更高级概念时,您的答案。再次感谢你们!
【解决方案3】:

最好以与类相同的方式命名文件。在您的情况下,这不是一个好主意,因为您的类很短,但每个类都应该有自己的 2 个文件 - 例如 Subtraction.h 和 Subtraction.cpp。在 .h 文件中,您应该只放置声明 - 在您的情况下: class Subtraction { public: float subtract (float , float); }

在 .cpp 文件中,您应该包含 .h 文件并放置实现。在你的情况下:

float Substraction::subtract (float x, float y) { float dif; dif=x-y; return dif; }

另见C++ Header Files, Code SeparationWhy have header files and .cpp files in C++?

希望这会有所帮助! :)

【讨论】:

  • 嗯,这让事情变得更清楚了,谢谢!我会尝试实现这个,看看它带我去哪里。
  • float Substraction::subtract (float x, float y) { return x-y; } } 也可以。或者至少,立即初始化临时对象,而不是先声明它,然后再分配一行
【解决方案4】:

每个文件只有一个公共类是很好的。 按照约定,文件名与类名相同(如果在文件中完成了某些定义,则扩展名为 .h .hh 或 .hpp)。

如果您想将不同的类放在不同的文件中,一个简单的文本编辑器将帮助您做到这一点。

【讨论】:

  • 感谢您的提示,我该如何使用文本编辑器呢?你的意思是复制和粘贴?
  • 他们在 ide 中的两个主要不同的东西,编辑,然后为编译语言编译和执行代码。对于编辑部分,您可以使用任何文本编辑器,从记事本到 emacs(vi、np++、geny 等...)。或其他 ide... 对于编译,请使用单独的编译器或 ide 中的编译器... 就个人而言,我通常使用 makefile 或 cmake 和 emacs 进行 c++(但它确实是过时的时尚,我很不情愿)。跨度>
猜你喜欢
  • 1970-01-01
  • 2014-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-09
  • 1970-01-01
相关资源
最近更新 更多