Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate classes into separate files in C++

Tags:

c++

class

EDIT: Added the "addition" class in, accidentally forgot about it. How do I separate classes into different files? I understand how classes work and how to make objects and things like that. However, I'm finding some confusion in the process of putting classes in different files. Thanks much for any help! Also, I am using CodeBlocks as an IDE. Here is my understanding so far:

  1. Create new class, and CB gives you a ".h" and a new ".cpp".
  2. The "Classname::Classname" at the beginning of the source file is a Scope Resolution Operator.
  3. You use "#include classname.h" in your main source file to import its contents.
  4. You can call functions from "main" by using the objects that you have declared.

This is my understanding so far, but I'm just confused on how to implement it. I have created a simple calculator with all the classes in one source file. It works fine, and I guess I'm pretty proud of it :) I know there is a lot easier way to make something like this, but I used classes and objects instead for the sole purpose of practice. Here's my code for the calculator. Also, I really apologize for the long post :/ Thanks, if you have come this far in reading it, and thanks especially if you can help me out a bit!

Here's my code in one source file:

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

}
like image 584
Carpetfizz Avatar asked Aug 09 '12 13:08

Carpetfizz


People also ask

Can you create separate classes as separate files?

Of course you can separate out 2 classes and still they will work fine. Just make sure you import one class in another using import statement. All you need to do is move AnotherClass class into a separate source file named with name same as class name ie "AnotherClass.

Should all classes be in separate files?

If you have a class that really needs to be a separate class but is also only used in one place, it's probably best to keep it in the same file. If this is happening frequently, though, you might have a bigger problem on your hands.

Should classes be in header files?

Classes are no different. Class definitions can be put in header files in order to facilitate reuse in multiple files or multiple projects. Traditionally, the class definition is put in a header file of the same name as the class, and the member functions defined outside of the class are put in a .


2 Answers

Ok, I will show you by doing your example:

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

and so on...

main.cxx

#include "subtraction.h"
#include "multiplication.h"

int main()
{
 //use the classes just as before.
}

Also, I didn't put it in the code here, for simplicity, but go ahead and get into the habit of ensuring that your declarations are only included once. On a large project, this can get very nasty if you don't put these safeguards in.

#ifndef SUBTRACTION_H
#define SUBTRACTION_H

class Subtraction
{
     ....
};
#endif /*SUBTRACTION_H*/
like image 131
Jonathan Henson Avatar answered Oct 17 '22 18:10

Jonathan Henson


here's something like Jonathan's example (I did not add the include guards for brevity, definitely do so though!), but with some duplication removed and some OO added for your learning pleasure. Note that while this is certainly not how an actual calculator would be implemented, it will hopefully give you some more understanding of C++ if you study it well enough.

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;
}
like image 2
stijn Avatar answered Oct 17 '22 19:10

stijn