Thread: Need some help.

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    42

    Need some help.

    I need some help on a simple program that I can't get to work. Here is the code that I have so far:

    Code:
    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 greatly appreciated.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I can't get to work
    This bug description does not help too much.
    What is your problem?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    42
    My problem is that I can not get the constructor with parameters to initialize the values of x and y to the values of a and b.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    testClass t; - declares var and calls default constructor

    testClass(); - calls default constructor without creating a var

    testClass(a,b); - calls non-default constructor without creating a var

    testClass t(a,b); - creates a var and calls non default constructor.

    Also in your constructor - better to use initialization list instead of assignments
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    42
    Maybe I am tired, but I'm kinda confused on how to fix it. I tried messing around with what you put in your reply, but I still can't get the non-default constructor to work. The values for x and y still remain to be 0.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    42
    n/m i got it working...thanks

Popular pages Recent additions subscribe to a feed