Constructor help

This is a discussion on Constructor help within the C++ Programming forums, part of the General Programming Boards category; Please run the code "q1_test" and tell me what am I doing wrong with the functions. As you can see, ...

  1. #1
    MMu
    MMu is offline
    Registered User
    Join Date
    Mar 2008
    Posts
    4

    Question Constructor help

    Please run the code "q1_test" and tell me what am I doing wrong with the functions.
    As you can see, the initialized values of the object 'c' does not go into the functions, GetDollars and GetCents.

    To understand a little more I made a change (q1_test2). In here why is the object 'temp' not seeing the constructor (not initializing)?

    Thanks
    Attached Files Attached Files

  2. #2

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    In your constructor, you have this bit of code:

    Code:
    CMoney::CMoney()
    {
    	int dollars=0;
    	int cents=0;
    	bool isNegative=false;
    	char dsign=0;
    	...
    The fact that you're giving each variable a type tells the compiler that you're declaring each variable for the first time, making them local variables that will go out of scope. Don't forget that you already gave the type for these variables in your class declaration! Here's how it should be done:

    Code:
    CMoney::CMoney()
    {
    	dollars = 0;
    	cents = 0;
    	isNegative = false;
    	dsign = 0;
    	printf("Initialize CMoney in constructor: dollars is %d, cents is %d\n\n", dollars, cents);
    }
    Last edited by rudyman; 04-19-2008 at 06:41 AM.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    When you use a type in front of a variable the compiler thinks its a new variable. So in the constructor you were making new variables, not assigning something to the once you created for the class

    Also, when you call a function, you must have () after it. GetDollars does not work, but GetDollars() is fine.

    And when you are using c++ I recommend using std::cout instead of printf()

    Code:
    #include <iostream>
    
    class CMoney
    {		
    	private: 	
    		int dollars;
    		int cents;
    		bool isNegative;
    		char dsign;
    
    	public:
    		int GetCents(void);
    		int GetDollars(void);
    		bool GetIsNegative();
    		char GetSymbol();
    
    		CMoney(int d = 0, int c = 0);		
    };
    
    CMoney::CMoney(int d, int c)
    {
    	dollars=d;
    	cents=c;
    	isNegative=false;
    	dsign=0;
    	
    	std::cout << "Initialize CMoney in constructor: dollars is " << dollars << std::endl;
    	std::cout << "Initialize CMoney in constructor: cents is " << cents << std::endl;
    }
    
    int CMoney::GetDollars()
    {
    	return dollars;
    }
    
    int CMoney::GetCents()
    {
    	return cents;
    }
    
    int main()
    {
    	std::cout <<"In main \n\n";
    	CMoney c;
    	std::cout << "In main: Dollars is " << c.GetDollars() << std::endl;
    	std::cout << "In main: Cent is " << c.GetCents() << std::endl;
    	
    	system("Pause");
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by rudyman View Post
    In your constructor, you have this bit of code:


    Code:
    CMoney::CMoney()
    {
    	dollars = 0;
    	cents = 0;
    	isNegative = false;
    	dsign = 0;
    	printf("Initialize CMoney in constructor: dollars is %d, cents is %d\n\n", dollars, cents);
    }
    Better yet:

    Code:
    CMoney::CMoney() : dollars(0), cents(0), isNegative(false), dsign(0) 
    {
        ...
    }

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Why is using initialization list better? I know it is a bit faster, but I find it makes the code harder to read so I normally dont use it

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Why is using initialization list better?
    1. Because you are initialising the member variables, not assigning to them.
    2. For reference and const member variables, because it is necessary.
    3. As per #1, because it is more efficient, particularly for member variables of class types.

    I know it is a bit faster, but I find it makes the code harder to read so I normally dont use it
    Harder to read is a subjective argument. I personally do not find it harder to read.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    By initializing it, it calls a constructor, not an assignment operator.
    If you would assign in the body, it would then first call the default constructor and then the assignment operator, which is a little unnecessary.
    And some things, like references, must be initialized and not assigned.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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