Hi, can you guys tell me if this is the right way to write or layout the codes? It's just a simple project and I finally got it to work. I just don't know if this is the right way to lay things out...
Code:
//SIMPLE CALCULATOR
//In main.cpp
#include <cstdlib>
#include <iostream>
#include "header2.h"
using namespace std;
int main(int argc, char *argv[])
{
int choice;
cout << "Please select an option from the menu:\n\n";
cout << "1. Add\n";
cout << "2. Subtract\n";
cout << "3. Multiply\n";
cout << "4. Divide\n";
cout << "\nEnter choice: ";
cin >> choice;
cout << "\n\n\n\n";
switch(choice)
{
case 1: myFunc();
break;
case 2: myFunc2();
break;
case 3: myFunc3();
break;
case 4: myFunc4();
break;
default: cout << "Please select one of the options in the menu.\n";
break;
}
cout << "\n\n\n\n";
system("PAUSE");
return EXIT_SUCCESS;
}
//In header2.h
//This is where I list my functions
#include <cstdlib>
#include <iostream>
int myFunc();
int myFunc2();
int myFunc3();
int myFunc4();
//In header1.h
//The class
#include <cstdlib>
#include <iostream>
using namespace std;
class Add
{
public:
int getAnswer() const {return one + two;}
void setAnswer(int a, int b) {one = a, two = b;}
private:
int one;
int two;
};
class Subtract
{
public:
int getsubAnswer() const {return subone - subtwo;}
void setsubAnswer(int a, int b) {subone = a, subtwo = b;}
private:
int subone;
int subtwo;
};
class Multiply
{
public:
int getmultAnswer() const {return multone * multtwo;}
void setmultAnswer(int a, int b) {multone = a, multtwo = b;}
private:
int multone;
int multtwo;
};
class Divide
{
public:
int getdivAnswer() const {return divtone / divtwo;}
void setdivAnswer(int a, int b) {divtone = a, divtwo = b;}
private:
int divtone;
int divtwo;
};
//In myFunc.cpp
//for addition
#include <cstdlib>
#include <iostream>
#include "header1.h"
using namespace std;
int myFunc()
{
int numbOne, numbTwo;
cout << "Enter a number: ";
cin >> numbOne;
cout << "Enter another number: ";
cin >> numbTwo;
Add myAdd;
myAdd.setAnswer(numbOne, numbTwo);
cout << "Answer: " << myAdd.getAnswer() << "\n\n";
return 0;
}
//In myFunc2.cpp
//For subtraction
#include <cstdlib>
#include <iostream>
#include "header1.h"
using namespace std;
int myFunc2()
{
int numbOne, numbTwo;
cout << "Enter a number: ";
cin >> numbOne;
cout << "Enter another number: ";
cin >> numbTwo;
Subtract mySubtract;
mySubtract.setsubAnswer(numbOne, numbTwo);
cout << "Answer: " << mySubtract.getsubAnswer() << "\n\n";
return 0;
}
//In myFunc3.cpp
//For multiplication
#include <cstdlib>
#include <iostream>
#include "header1.h"
using namespace std;
int myFunc3()
{
int numbOne, numbTwo;
cout << "Enter a number: ";
cin >> numbOne;
cout << "Enter another number: ";
cin >> numbTwo;
Multiply myMultiply;
myMultiply.setmultAnswer(numbOne, numbTwo);
cout << "Answer: " << myMultiply.getmultAnswer() << "\n\n";
return 0;
}
//In myFunc4.cpp
//For division
//here I forgot to set the variables as float - i'll do different next time
#include <cstdlib>
#include <iostream>
#include "header1.h"
using namespace std;
int myFunc4()
{
int numbOne, numbTwo;
cout << "Enter a number: ";
cin >> numbOne;
cout << "Enter another number: ";
cin >> numbTwo;
Divide myDivide;
myDivide.setdivAnswer(numbOne, numbTwo);
cout << "Answer: " << myDivide.getdivAnswer() << "\n\n";
return 0;
}
I set these codes on separate files and linked them.
Is this how I should be laying out the codes? And if you find anything to correct, I'd really appreciate it. Thanks!