#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Smile C++ constructors


    just a bit confused about constructors concept....


    see my example code here :

    Code:
    #include<iostream>
    #include<stdio.h>
    #include<conio.h>
    
    using namespace std;
    
    
    class integer
    {
    	private:
    		int i;
    	
    	public:
    		
    		void getdata()
    		{
    			cout<<endl<<"Enter an integer";
    			cin>>i;
    		}
    		
    		void setdata(int j)
    		{
    		i=j;
    		}
    		
    		integer(){ //zero argument constructor
    		}
    		
    		integer(int j) //one argument constructor
    			{
    				i=j;
    			}
    			
    		void displaydata()
    			{
    				cout<<endl<<"value of i is : "<<i<<endl;
    			}
    		
    }
    
    void main()
    	 {
    	 	integer i1(100),i2,i3;
    	 	i1.displaydata();
    	 	i2.setdata(200);
    	 	i2.displaydata();
    	 	i3.getdata();
    	 	i3.displaydata();
    	 getch();
    	 }
    Here i1(100) is one argument constructor.... if what i say is correct .. then i2 and i3 are zero argument constructors...

    Does this imply that..
    All class objects are by default zero arguments constructors?
    If all this is true then every object of class is always calling a constructor or it is a constructor.....

    please explain...
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0
    1 more question.... i get error that main must return int in this program... why ??
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,220
    Rep Power
    2222
    Originally Posted by swapy
    1 more question.... i get error that main must return int in this program... why ??
    Because main must return int: zero for successful completion, non-zero for failure.

    C lets you get away with being lazy and doing things wrong; C++ doesn't.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,220
    Rep Power
    2222
    Objects are not constructors. Rather, objects have constructors. They just have the name of the class. And the syntax of instantiating an object through declaration is to attach the arguments to the object name. How many and what datatypes of the arguments determine which constructor is used (as per the rules of function overloading). The default constructor is the one with no arguments.

    Comments on this post

    • swapy agrees
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,116
    Rep Power
    1803
    When an object is created or comes into scope its constructor is automatically called. Which constructor is called is governed by the rules of function overloading - the constructor whose argument count and types match is called.

    A constructor with no arguments is known as a default constructor - the one that is called when an object is instantiated with no arguments.

    Comments on this post

    • swapy agrees
  10. #6
  11. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,116
    Rep Power
    1803
    Originally Posted by remunance
    Why important constructor in program
    The ability to automatically (and correctly) initialise an object reduces the scope for errors - the designer of the class can ensure that it is performed to their design rather than relying on the user of the class to have to know how to do that, and more importantly to avoid having to give the user the means to do that (correctly or otherwise).

    That is to say it minimises the ways in which an object can be use incorrectly.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    constructor


    class example
    {
    int p, q;
    public:
    example(int a, int b); //parameterized constructor
    };
    example :: example(int a, int b)
    {
    p = a;
    q = b;
    }

    example e = example(0, 50); //explicit call

    example e(0, 50); //implicit call

    Comments on this post

    • Scorpions4ever disagrees : Nice try ripping off stuff from Wikipedia. Strongly doubt you know anything about C++, judging by the fact that you'd made a previous post asking what a constructor was.

IMN logo majestic logo threadwatch logo seochat tools logo