Default Constructors

This is a discussion on Default Constructors within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; class Circle { private: float radius; public: void setRadius(float); float getArea(); }; //Class function ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    10

    Default Constructors

    Code:
    	#include <iostream>
    	using namespace std;
    
    
    	class Circle
    	{
    	private:
    	float radius;
    
    	public:
    	void setRadius(float);
    	float getArea();
    
    	};
    
    //Class function definitions
    
    void Circle::setRadius(float r)
    { radius = r ;
    }
    
    float Circle::getArea()
    {	return 3.14159 * radius * radius;
    }
    
    int main()
    {
    
    	Circle Box;
    
    	float CirRadius;
    	cout << "Enter the radius of the circle " ;
    	cout << endl;
    	cin >> CirRadius;
    	Box.setRadius(CirRadius);
    	cout << "Your area is " << Box.getArea();
    	cout << endl;
    
    getchar();
    getchar();
    return 0;
    }
    This is my code, obviously, and this is what I came up with for a default constructor, I am only getting one error, a syntax error when I run it, any ideas why?

    Code:
    	class Circle
    	{
    	private:
    	float radius;
    	float area;
    
    	public:
    	Circle::Circle(Raidus= 0);
    	};
    
    	Circle::Circle()
    	{
    		cout << "Welcome to the constructor!\n ";
    		cout << endl;
    		cout << "The Radius is set to zero\n ";

  2. #2
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    Circle::Circle doesn't need any parameters since you are defaulting it to zero, and you need to initilize radius in either an initializer list or inside the body of the function.

    - SirCrono6
    Last edited by SirCrono6; 04-10-2005 at 08:36 PM. Reason: Stuff :)
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    ...also
    Code:
    private:
    	float radius;
    
    Circle::Circle(Raidus= 0);
    Where is your member variable named Raidus? In addition, inside a class, you don't need to qualify variables or functions with the class name.

    Code:
    Circle::Circle()
    	{
    		cout << "Welcome to the constructor!\n ";
    		cout << endl;
    		cout << "The Radius is set to zero\n ";
    Function definitions end with a closing brace.
    Last edited by 7stud; 04-10-2005 at 08:38 PM.

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    10
    That's a typo, my fault.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I think by definition a default constructor has NO parameters, so it will always look like this:
    Code:
    ClassName()
    {
         //do something
    }
    The only question is whether you have to define one or the compiler is going to supply one automatically. If you define ANY constructor, then the compiler won't supply a default constructor, and you need a default constructor to do this:

    ClassName aThing;
    Last edited by 7stud; 04-10-2005 at 08:59 PM.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    in this line
    Code:
    Circle::Circle(Raidus= 0);
    1. you need to specify the type of the paramter - more than likely, in this case, it should eb float
    2. I assume Raidus is meant to be radius. you can't use the same name for parameter variables. you would need Circle::Circle(float r){ radius = r; }
    3. You need to define AND protoype default constructors which take no parameters.
    Circle(void) != Circle(float = 0); These are not the same functions.


    Code:
    class Circle
    {
    	private:
    	float radius;
    	float area;
    
    	public:
    	Circle(void);
             //no need for variable name in prototype
    	Circle(float = 0);
    };
    
    Circle::Circle(void)
    {
    	cout << "Welcome to the REAL default constructor!\n ";
    	cout << endl;
    	cout << "The Radius is set to zero\n ";
             radius = 0;
    }
    
    //don't specify the default value here, only in the class prototype
    Circle::Circle(float Radius) //i can use Radius because of case-sensitivity
    {
    	cout << "Welcome to the FAKE default constructor!\n ";
    	cout << endl;
    	cout << "The Radius is set to zero\n ";
             radius = Radius
    }

    This should compile as is, but once try to initialize an object, it will complain about this
    Code:
    Circle a();
    but not this
    Code:
    float x = 3.145f;
    Circle b(x);
    however, i am unsure of the behaviour of this - i imagine this will complain to
    Code:
    Circle c;

    can you guess why?
    Last edited by misplaced; 04-10-2005 at 09:17 PM.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    >>This should compile as is, but once try to initialize an object, it will complain about this

    Although the compiler usually generates a default constructor/destructor as well as a copy constructor and assignment operator if you don't define one. See if this compiles:
    Code:
    class Test
    {
    
    };
    
    int main()
    {
    Test A;
    Test B(A);
    Test C;
    A=C;
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    that's not really what i was saying....with the code i posted above, you should get an ambiguous functions overload error.

    seeing as Circle(void) and Circle(float = 0) can both be called the same way - Circle()
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Circle(void)
    Come on? You really enjoy typing 'void' that much? Not to mention 'void' is somewhat scary for beginners. Let's go with the following and call it a day:
    Code:
    Circle::Circle()
    {
    
    }

  10. #10
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,803
    Here's my idea.
    Code:
    #include <iostream>
    using namespace std;
    
    
    class Circle
    {        
        private:
            float radius;
    
        public:
            inline Circle() : radius(0.0){}//How about this?                
            void setRadius(float);
            float getRadius()
            {
                return radius;
            }
            float getArea();
    };
    
    //Class function definitions
    
    void Circle::setRadius(float r)
    {
        radius = r ;
    }
    
    float Circle::getArea()
    {
        return 3.14159 * radius * radius;
    }
    
    int main()
    {
    
        Circle Box;
        cout<<"Default Radius: ";
        cout<<Box.getRadius()<<endl;
        float CirRadius;
        cout << "Enter the radius of the circle " ;
        cout << endl;
        cin >> CirRadius;
        Box.setRadius(CirRadius);
        cout << "Your area is " << Box.getArea();
        cout << endl;
    
        getchar();
        getchar();
        return 0;
    }
    Last edited by prog-bman; 04-11-2005 at 03:08 PM.
    Woop?

  11. #11
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    This would create a constructor plus a default constructor in one single step:
    Code:
    Circle::Circle(float Radius = 0.0f)
    {
      this->Radius = Radius;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  12. #12
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by Magos
    This would create a constructor plus a default constructor in one single step:
    Code:
    Circle::Circle(float Radius = 0.0f)
    {
      this->Radius = Radius;
    }

    by definition, a default constructor takes no parameters. that one does.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  13. #13
    Registered User
    Join Date
    Feb 2005
    Posts
    10
    Next question, assuming everything is defined properly, would this added code work as an overload constructor?

    Keep in mind, i'm very very wet behind the ears when it comes to C++, and programming in general, so if something I post is completely assinine, go easy on me.

    Code:
    #include <iostream>
    #include "Circle.h"
    
    using namespace std;
    
    int main()
    {
    
    	Circle size(3.0);		//starting radius size of 3.0 added
    	float Radius;
    
    	cout << "Enter the radius of the circle " ;
    	cin >> Radius;
    	size.Radius(Radius);
    	cout << "The total area, including extra radius is ";
    	cout << size.getArea() << endl;
    
    	getchar();
    	getchar();
    	return0;
    
    }

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by Magos
    Code:
    Circle::Circle(float Radius = 0.0f)
    {
      this->Radius = Radius;
    }
    Quote Originally Posted by misplaced
    by definition, a default constructor takes no parameters. that one does.
    ...nevertheless it serves as a default constructor, which you can test yourself:
    Code:
    #include <iostream>
    using namespace std;
    
    class Circle
    {
    public:
    	float radius;
    
    	Circle(float r = 0.0)
    	{
    		radius = r;
    		cout<<"constructor called"<<endl;
    	}
    
    	
    };
    
    int main()
    {
    	Circle A(3.0);
    	Circle B;  //what happens here??
    
    	cout<<B.radius<<endl;
    
    	return 0;
    }
    In fact, if you try to add the following default constructor to Circle:
    Code:
    Circle()
    {
    	radius = 0;
    }
    you will get the following error:

    warning C4520: 'Circle' : multiple default constructors specified
    error C2668: 'Circle::Circle' : ambiguous call to overloaded function
    Last edited by 7stud; 04-11-2005 at 10:53 PM.

  15. #15
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by SquirrelRJ
    Next question, assuming everything is defined properly, would this added code work as an overload constructor?
    It's not clear what your class looks like now, so it's a bit confusing, but this won't work:
    Code:
    Circle size(3.0);		//starting radius size of 3.0 added
    float Radius;
    
    size.Radius(Radius);
    First of all, you have to use some imagination and not name every variable in your program Radius. The code fragment:

    size.Radius

    indicates that Radius is a member of the Circle class. But, then you also have a variable in main declared here:

    float Radius;

    So, my first piece of advice is: do NOT EVER name variables with identical names. Because of those identical variable names, it is not clear what you think you are doing with this statement:

    size.Radius(Radius);

    However, I'll assume you think you are trying to assign the value of a variable you called Radius to the Radius member of the size Circle. Another tip: capitalize your object names. However, there are a couple of things wrong with that statement:

    1) Radius is a private member, so you cannot access it. Private members of a class can only be accessed by member functions of the class.

    2)If the Radius were public, you still couldn't assign it a value doing this:

    size.Radius(Radius)

    The way you would assign the value is like this:

    size.Radius = Radius;

    You can do this:
    Code:
    float num(3.0);
    cout<<num<<endl; //3
    That's called "functional notation" for declaring initial values--but you must initialize the variable when it is declared. You cannot do this:
    Code:
    float num;
    num(3.0);
    The variable num has already been declared, and to the compiler, that looks like you are trying to call a function named num() and sending the argument 3.0 to the function. I suggest you don't use functional notation to initialize variables of simple types--I hardly ever see that done, and you will avoid these exact types of problems by not using it.
    Last edited by 7stud; 04-11-2005 at 11:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array members copied by the default copy constructors?
    By cyberfish in forum C++ Programming
    Replies: 4
    Last Post: 02-17-2008, 11:46 PM
  2. Default Constructors
    By pritin in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2007, 06:38 AM
  3. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  4. Default Constructors
    By MethodMan in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2002, 06:30 PM
  5. Default Constructors w/Classes as Parameters
    By GrNxxDaY in forum C++ Programming
    Replies: 22
    Last Post: 07-31-2002, 07:50 AM

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