Damn classes not working

Well, I made a simple program to exercise my OOP learnings, but when I compile it, I get an error.

Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;

class CNumbers {
protected:
	int x, y;
public :
	void Passer(int a, int b) {
		x = a;
		x = b; }
	virtual int Result() = 0;
};

class CAddition: public CNumbers {
	int Result() { return x+y;}
};

class CSubtraction: public CNumbers {
	int Result() { return x-y;}
};

int main() {
	CNumbers *pointer1, *pointer2;
	int a, b;
	cin >> a >> b;
	Passer(a, b);
	pointer1 = new CAddition;
	pointer2 = new CSubtraction;
	cout << pointer1->Result() << endl;
	cout << pointer2->Result() << endl;
	delete pointer1;
	delete pointer2;
}


ERROR: error C3861: 'Passer': identifier not found.

I tried using the scope sign (::), but then it just genrated another error.
Help me please!
Thanks in advance
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main() {
	CNumbers *pointer1, *pointer2;
	int a, b;
	cin >> a >> b;
	pointer1 = new CAddition;
	pointer2 = new CSubtraction;
	pointer1->Passer(a, b);
	pointer2->Passer(a, b);
	cout << pointer1->Result() << endl;
	cout << pointer2->Result() << endl;
	delete pointer1;
	delete pointer2;
	return 0;
}
Passer is a member function just like Result. You have to call it the same way as you
call Result -- that is, with an instance.

But what you are trying to do is essentially write a useful constructor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class CNumbers {
  public:
     CNumbers( int a, int b ) : x( a ), y( b ) {}
};

class CAddition : public CNumbers {
  public:
     CAddition( int a , int b ) : CNumbers( a, b ) {}
};

int main() {
    CAddition adder( 3, 4 );
    std::cout << adder.Result() << std::endl;
}


Topic archived. No new replies allowed.