program does not return the value entered

#include <iostream>

using namespace std;

class testClass
{
public:
    int sum();
    void print() const;
    testClass();
    testClass(int a, int b);
private:
    int x;
    int y;
};

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

void testClass::print() const
{
    cout << "x = " << x << "y = " << y << endl;
}

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

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

int main()
{
    int a;
    int b;

testClass mytestClass;

cout << "enter a value for x: ";
cin >> a;
cout << "enter a value for y: ";
cin >> b;

mytestClass.print();

    return 0;
};

Edited 3 Years Ago by pyTony: fixed formatting

You should use CODE tags so that it is indented correctly and things like ::print isn't replaced with a smiley face and "rint".

Also, you are inputting into the automatic variables a and b in main. These are completely unrelated to the ones in your class.

Edited 3 Years Ago by happygeek: fixed formatting

so how do i input into the variables specified in the class

If it's printing zero for both values, it's because you called the empty constructor, not the one where you pass the values.Instead of:

int main()
{
int a;
int b;
 
testClass mytestClass;
 
cout << "enter a value for x: ";
cin >> a;
cout << "enter a value for y: ";
cin >> b;
 
mytestClass.print();

Do:

int main()
{
int a;
int b;
 
 
cout << "enter a value for x: ";
cin >> a;
cout << "enter a value for y: ";
cin >> b;
testClass mytestClass(a,b);
 
mytestClass.print();
This article has been dead for over six months. Start a new discussion instead.