something is wrong with this program

I have studied the sections of class on the tutorial here.Now I made a program to do experiments .Can anyone please tell me what is wrong with this program?

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
34
35
36
37
#include<iostream>
using namespace std;
class wasim
{
	int x;
public:
	wasim (int a, int b);
	int function(int d,int e);
};
wasim::wasim(int a,int b)
{
	a=a*5;
	b=b*5;

};
int wasim::function(int d,int e)
{
	int z;
	z=d+e;
	return z;
};

int main()
{
	int f;
	int g;
	g=5;
	f=6;

	wasim tukhi (2,1);
	wasim masih(f,g);
	masih.wasim(f,g);

	cout<<tukhi.function(2,1);
	cout<<g<<"  "<<f;
	return 0;
}
What exactly is the problem?
You can't do masih.wasim(f,g); at line 32 because wasim(int, int) is a constructor of your class so it can only be called when you create an object of the class (lines 30 and 31)
How about you never use wasim::x, wasim::function has no relation to the class that it is defined in, and the wasim constructor doesn't do anything.

Oh, and you don't need semicolons after your method definitions (lines 15 and 21).
Last edited on
@ seymore .
Why wasim::function has no relation to the class and why is the constructor not doing its work ?
Last edited on
You declared a class with a variable x inside of it, then when people call your "constructor" x is still undefined, you simply modify random variables for no reason. And wasim::function() simply does random arithmetic and doesn't actually do anything to the class or relate to class in anyway. It might as well be a global function.
Topic archived. No new replies allowed.