OOP

Started by
9 comments, last by Powell 14 years, 10 months ago
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 add();
	int main();
	int x;
	int y;
	int z;
}
mathManager::mathManager()
{
}
mathManager::~mathManager()
{
}
int mathManager::add()
{
	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

Advertisement
You forgot the ; after the class definition.
Add a ; after the } at the end of your class definition.
Edit - ninja'd
"All you have to decide is what to do with the time that is given to you." - Gandalf
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.

You should do this instead:
this->add()

Or even just:
add()

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.
Thanks servant, but now it says I do not have an entry point, which is supposed to be my main
main() can't be a member function. It must be a free function in the root namespace.
Quote:Original post by SiCrane
main() 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
You probably want to use doMath.x and doMath.y instead of just x and y.
Quote:Original post by SiCrane
You probably want to use doMath.x and doMath.y instead of just x and y.


Ah thank you
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;}

This topic is closed to new replies.

Advertisement