EDIT: Added an add class that accidentally forgot about it. How to separate classes from different files? I understand how classes work and how to create objects and the like. However, I find some confusion in the process of placing classes in different files. Thanks so much for any help! In addition, I use CodeBlocks as an IDE. Here is my understanding:
- Create a new class and CB will give you ".h" and a new ".cpp".
- The "Class Name :: Class Name" at the beginning of the source file is the Scope Resolution Operator.
- You use "#include classname.h" in your main source file to import its contents.
- You can call functions from "main" using the objects you declare.
This is my understanding so far, but I'm just confused about how to implement it. I created a simple calculator with all classes in one source file. It works great, and I think I'm very proud of it :) I know that there is a much easier way to do something like this, but instead I used classes and objects for the sole purpose of practice. Here is my calculator code. Also, I really apologize for the long post: / Thank you if you went this far reading it, and thanks, especially if you can help me a little!
Here is 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=xy; 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; }
source share