Need some help.

This is a discussion on Need some help. within the C++ Programming forums, part of the General Programming Boards category; I need some help on a simple program that I can't get to work. Here is the code that I ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    I can't get to work
    This bug description does not help too much.
    What is your problem?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21