# OOP

This topic is 3183 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

New to OOP, try to do it myself after i read a tutorial on cprogramming.com(http://www.cprogramming.com/tutorial/lesson12.html) but after a good while I still can't seem to get mine working.
#include <iostream>
using namespace std;
class mathManager
{
public:
mathManager();
~mathManager();
int main();
int x;
int y;
int z;
}
mathManager::mathManager()
{
}
mathManager::~mathManager()
{
}
{
z = x + y;
return z;
}
int mathManager::main()
{
mathManager doMath;
cout << "Please enter two numbers: " << endl;
cin >> x;
cin >> y;
cout << x << " plus " << y << " equals " << doMath.add << endl;
}


Errors:
1>c:\users\anthony\documents\visual studio 2005\projects\oop on own\oop on own\main.cpp(15) : error C2533: 'mathManager::{ctor}' : constructors not allowed a return type
1>c:\users\anthony\documents\visual studio 2005\projects\oop on own\oop on own\main.cpp(27) : error C2264: 'mathManager::mathManager' : error in function definition or declaration; function not called
1>c:\users\anthony\documents\visual studio 2005\projects\oop on own\oop on own\main.cpp(31) : error C3867: 'mathManager::add': function call missing argument list; use '&mathManager::add' to create a pointer to member



##### Share on other sites
You forgot the ; after the class definition.

##### Share on other sites
Add a ; after the } at the end of your class definition.
Edit - ninja'd

##### Share on other sites
Also:
cout << x << " plus " << y << " equals " << doMath.add << endl;

Should be:
cout << x << " plus " << y << " equals " << doMath.add() << endl;

(To call a function with no parameters, you write 'functionName()' not 'functionName')

[Edit:] You are creating a new 'mathManager' inside your current mathManager's main() function? That's not a good idea.

Or even just:

Otherwise, you are creating a different mathManager variable, with its own x, y, and z variables, which haven't yet been set to anything, and it'd return the wrong result.

##### Share on other sites
Thanks servant, but now it says I do not have an entry point, which is supposed to be my main

##### Share on other sites
main() can't be a member function. It must be a free function in the root namespace.

##### Share on other sites
Quote:
 Original post by SiCranemain() can't be a member function. It must be a free function in the root namespace.

Oh okay, but if I make it independent then x and y are undeclared

##### Share on other sites
You probably want to use doMath.x and doMath.y instead of just x and y.

##### Share on other sites
Quote:
 Original post by SiCraneYou probably want to use doMath.x and doMath.y instead of just x and y.

Ah thank you

##### Share on other sites
But now that you have it working... what you're doing has basically nothing to do with OOP. :)

Some rules of thumb for OO design:

1) Don't use member variables to store intermediate results of calculations. Use local variables inside the member functions. This is the same idea as not using globals when you could use a local inside a free function: restrict scope as much as possible. The purpose of member variables is to remember data "between" calls to the member functions. It forms the logical 'state' of the object.

2) Don't use names like "Manager" for classes. Figure out what it is that the class does, and give it an appropriate name. Your class adds two numbers, so Adder is a reasonable name.

3) Encapsulate an object's state. This does NOT mean making getters and setters for everything. It means designing an interface that makes sense.

4) Don't construct an object until you have all the information you need to put it into a valid state. Then give all that information to the constructor, so the object can be in a valid state right away.

5) Don't write things that you don't have to write. In C++, it's actually very rare to have to write a destructor yourself, if you are using modern tools. (One notable exception: if you are going to use inheritance for polymorphism, you need a 'virtual' destructor defined in the base class.)

Taking these principles into account, and using some modern C++ idioms, we can transform even this simple example into something quite different (although still very useless ;) ):

#include <iostream>class adder {  private:  int x, y;  public:  adder(int x, int y) : x(x), y(y) {}  int sum();}; // LOL, I initially forgot the semicolon, too!// Been working in Java too much recently, I guess.// You don't even need 'z' at all.int adder::sum() { return x + y; }int main() {  using namespace std;  cout << "Please enter two numbers: " << endl;  cin >> x;  cin >> y;  // We don't need to give the adder instance a name, because it's only  // used once:  cout << x << " plus " << y << " equals " << adder(x, y).sum() << endl;}

##### Share on other sites
@zahlman

Old code, I have changed it alot sense last time, though there may still be problems with my code.

#include <iostream>#include <math.h>using namespace std;class mathManager{public:	mathManager();	~mathManager();	int add();	int subtract();	int multiply();	int divide();	int square();	int x;	int y;	int choice;	int sqNum;};mathManager::mathManager(){}mathManager::~mathManager(){}int mathManager::add(){	return x + y;}int mathManager::subtract(){	return x - y;}int mathManager::multiply(){	return x * y;}int mathManager::divide(){	return x / y;}int mathManager::square(){	return sqNum * sqNum;}int main(){	mathManager doMath;	cout << "Please enter two numbers: " << endl;	cout << "....   ";	cin >> doMath.x;	cout << "....   ";	cin >> doMath.y;	cout << "1 Addition" << endl;	cout << "2 Subtraction" << endl;	cout << "3 Multiplication" << endl;	cout << "4 Division" << endl;	cout << "5 Number Squared" << endl;	cout << "Please select a menu option: ";	cin >> doMath.choice;	switch(doMath.choice)	{	case 1:		cout << doMath.x << " plus " << doMath.y << " equals " << doMath.add() << endl;	break;	case 2:		cout << doMath.x << " subtracted by " << doMath.y << " equals " << doMath.subtract() << endl;	break;	case 3:		cout << doMath.x << " multiplied by " << doMath.y << " equals " << doMath.multiply() << endl;	break;	case 4:		cout << doMath.x << " divded by " << doMath.y << " equals " << doMath.divide() << endl;	break;	case 5:		cout << "Enter a number to get squared: ";		cin >> doMath.sqNum;		cout << doMath.sqNum << " squared is " << doMath.square() << endl;	break;	default:		cout << "Bro, thats an incorrect choice!" << endl;	break;	}}

Now it does more than just add, so I think mathManager is an more appropriate name.

Also with that 'z', my friend told me over teamviewer that I could just return x + y instead of putting them into z and returning that :D