Note that there are some explanatory texts on larger screens.

plurals
  1. POSeparate classes into separate files in C++
    text
    copied!<p>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:</p> <ol> <li>Create new class, and CB gives you a ".h" and a new ".cpp".</li> <li>The "Classname::Classname" at the beginning of the source file is a Scope Resolution Operator.</li> <li>You use "#include classname.h" in your main source file to import its contents. </li> <li>You can call functions from "main" by using the objects that you have declared. </li> </ol> <p>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!</p> <p>Here's my code in one source file:</p> <pre class="lang-c++ prettyprint-override"><code>#include &lt;iostream&gt; 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&lt;&lt;"Welcome to C++ Calculator v2!"&lt;&lt;endl; cout&lt;&lt;"Select the number for which operation you want to use: "&lt;&lt;endl; cout&lt;&lt;"1-Addition"&lt;&lt;endl; cout&lt;&lt;"2-Subtraction"&lt;&lt;endl; cout&lt;&lt;"3-Mutliplication"&lt;&lt;endl; cout&lt;&lt;"4-Division"&lt;&lt;endl; cin&gt;&gt;op; if (op==1) { float num1; float num2; Addition addobj; cout&lt;&lt;"You have chosen Addition!"&lt;&lt;endl; cout&lt;&lt;"Enter the first number you want to add: "&lt;&lt;endl; cin&gt;&gt;num1; cout&lt;&lt;"Enter the second number you wat to add: "&lt;&lt;endl; cin&gt;&gt;num2; float ans=addobj.add(num1, num2); cout&lt;&lt;"The sum is "&lt;&lt;ans&lt;&lt;endl; cout&lt;&lt;"Do you wish to continue? Y/N"&lt;&lt;endl; cin&gt;&gt;cont; } if (op==2) { float num1; float num2; Subtraction subobj; cout&lt;&lt;"You have chosen Subtraction!"&lt;&lt;endl; cout&lt;&lt;"Enter the first number you want to subtract: "&lt;&lt;endl; cin&gt;&gt;num1; cout&lt;&lt;"Enter the second number you want to subtract: "&lt;&lt;endl; cin&gt;&gt;num2; float ans=subobj.subtract(num1, num2); cout&lt;&lt;"The difference is "&lt;&lt;ans&lt;&lt;endl; cout&lt;&lt;"Do you wish to continue? Y/N"&lt;&lt;endl; cin&gt;&gt;cont; } if (op==3) { float num1; float num2; Multiplication multobj; cout&lt;&lt;"You have chosen Multiplication!"&lt;&lt;endl; cout&lt;&lt;"Enter the first number you want to multiply: "&lt;&lt;endl; cin&gt;&gt;num1; cout&lt;&lt;"Enter the second number you want to multiply: "&lt;&lt;endl; cin&gt;&gt;num2; float ans=multobj.multiply(num1, num2); cout&lt;&lt;"The product is "&lt;&lt;ans&lt;&lt;endl; cout&lt;&lt;"Do you wish to continue? Y/N"&lt;&lt;endl; cin&gt;&gt;cont; } if (op==4) { float num1; float num2; Division divobj; cout&lt;&lt;"You have chosen Division!"&lt;&lt;endl; cout&lt;&lt;"Enter the first number you want to divide: "&lt;&lt;endl; cin&gt;&gt;num1; cout&lt;&lt;"Enter the second number you want to divide: "&lt;&lt;endl; cin&gt;&gt;num2; float ans=divobj.divide(num1, num2); cout&lt;&lt;"The quotient is "&lt;&lt;ans&lt;&lt;endl; cout&lt;&lt;"Do you wish to continue? Y/N"&lt;&lt;endl; cin&gt;&gt;cont; } } while (cont=='Y'||cont=='y'); if (cont=='N'||'n') { cout&lt;&lt;"Thanks for using my program, goodbye!"&lt;&lt;endl; } return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload