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>
usingnamespace 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;
}
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.
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>
usingnamespace 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;
}
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.
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>
usingnamespace 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;
}