在C ++中将类分成单独的文件

时间:2012-08-09 13:38:32

标签: c++ class

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

  1. 创建新课程,CB会为您提供“.h”和新的“.cpp”。
  2. 源文件开头的“Classname :: Classname”是范围解析运算符。
  3. 您在主源文件中使用“#include classname.h”来导入其内容。
  4. 您可以使用已声明的对象从“main”调用函数。
  5. 到目前为止,这是我的理解,但我对如何实现它感到困惑。我创建了一个简单的计算器,其中包含一个源文件中的所有类。它工作正常,我想我很自豪它:)我知道有更简单的方法来制作这样的东西,但我使用类和对象而不是为了练习的唯一目的。这是我的计算器代码。另外,我真的为长篇帖子道歉:/谢谢你,如果你已经读到这么远了,特别感谢你能帮助我一点!

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

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

4 个答案:

答案 0 :(得分:3)

好的,我会以你的榜样告诉你:

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*/

答案 1 :(得分:1)

这里有类似乔纳森的例子(为了简洁,我没有添加包含警卫,但确实这样做了!),但是删除了一些复制品并添加了一些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 :(得分:0)

以与课程相同的方式命名文件是件好事。在你的情况下,这不是一个好主意,因为你的类很短,但每个类应该有自己的2个文件 - 例如Subtraction.h和Subtraction.cpp。在.h文件中,你应该只放入decaration - 在你的情况下:     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++?

希望这有点帮助! :)

答案 3 :(得分:0)

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

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