Need help on a program

I have a simple program that I can not get to work. This is what I have so far:

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class testClass
{
public:
	int sum();
		//Postcondition: Returns the sum of the
		//				 private data numbers
	void print() const;
		//Prints the values of the private data
		//members
	testClass();
		//defualt constructor
		//Postcondition: x =0; y =0
	testClass(int a, int b);
		//constructor with parameters
		//Initializes the private data members to the
		//values specified by the parameters.
		//Postcondition: x = a; y= b
private:
	int x;
	int y;
};

#include <iostream>

using namespace std;

testClass::testClass()
{
	x = 0;
	y = 0;
}

testClass::testClass(int a, int b)
{
	x = a;
	y = b;
}

void testClass::print() const
{
	cout << "The sum of " << x << " and " << y << " is " << sum() << endl;		
}

int testClass::sum()
{
	return (x + y);	
}

// Main

int main()
{
	int a=0;
	int b=0;

	testClass t;
	testClass();
	t.print();
	cout << endl << "Input 2 integers: ";
	cin >> a >> b;

	testClass(a,b);

	cout << endl << t.sum() << endl;

	return 0;
}


Any help will be appreciated.

Thanks.
Here you go. The code has been fixed for you. The only thing I did was remove the const from the void testClass::print() const as well as from the declaration in your Class header.

~maingeek

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class testClass
{
public:
	int sum();
		//Postcondition: Returns the sum of the
		//				 private data numbers
	void print();
		//Prints the values of the private data
		//members
	testClass();
		//defualt constructor
		//Postcondition: x =0; y =0
	testClass(int a, int b);
		//constructor with parameters
		//Initializes the private data members to the
		//values specified by the parameters.
		//Postcondition: x = a; y= b
private:
	int x;
	int y;
};

#include <iostream>

using namespace std;

testClass::testClass()
{
	x = 0;
	y = 0;
}

testClass::testClass(int a, int b)
{
	x = a;
	y = b;
}

void testClass::print()
{
	cout << "The sum of " << x << " and " << y << " is " << sum() << endl;		
}

int testClass::sum()
{
	return (x + y);	
}

// Main

int main()
{
	int a=0;
	int b=0;

	testClass t;
	testClass();
	t.print();
	cout << endl << "Input 2 integers: ";
	cin >> a >> b;

	testClass(a,b);

	cout << endl << t.sum() << endl;

	return 0;
}


Thanks, I got it working.
We just learned about the 'const' keyword today in class with objects, so this code may work better for you, as it will allow your client program (the main) to define constants as testClass.

The only thing that I changed from your original code was added the const qualifier to the sum(), both in the declaration and implementation.

~maingeek.

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class testClass
{
public:
	int sum();
		//Postcondition: Returns the sum of the
		//				 private data numbers
	void print() const;
		//Prints the values of the private data
		//members
	testClass();
		//defualt constructor
		//Postcondition: x =0; y =0
	testClass(int a, int b);
		//constructor with parameters
		//Initializes the private data members to the
		//values specified by the parameters.
		//Postcondition: x = a; y= b
private:
	int x;
	int y;
};

#include <iostream>

using namespace std;

testClass::testClass()
{
	x = 0;
	y = 0;
}

testClass::testClass(int a, int b)
{
	x = a;
	y = b;
}

void testClass::print() const
{
	cout << "The sum of " << x << " and " << y << " is " << sum() << endl;		
}

int testClass::sum()
{
	return (x + y);	
}

// Main

int main()
{
	int a=0;
	int b=0;

	testClass t;
	testClass();
	t.print();
	cout << endl << "Input 2 integers: ";
	cin >> a >> b;

	testClass(a,b);

	cout << endl << t.sum() << endl;

	return 0;
}
Last edited on
Topic archived. No new replies allowed.