overloading constructors

This is a discussion on overloading constructors within the C++ Programming forums, part of the General Programming Boards category; this is my program---------- Code: #include <iostream> #include <cstdlib> class rectangle { public: rectangle(); rectangle(int length, int width); ~rectangle() {} ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    31

    overloading constructors

    this is my program----------

    Code:
    #include <iostream>
    #include <cstdlib>
    
    class rectangle
    {
     public:
     rectangle();
     rectangle(int length, int width);
     ~rectangle() {}
     int getlength() const {return itslength;}
     int getwidth() const {return itswidth;}
    
     private:
     int itslength;
     int itswidth;
    };
    
    rectangle::rectangle()
    {
     itswidth=10;
     itslength=5;
    }
    
    rectangle::rectangle(int length, int width)
    {
     int itslength=length;
     int itswidth=width;
    }
    
    int main()
    {
     rectangle rect1;
     cout<<"rect1's length is "<<rect1.getlength()<<endl;
     cout<<"rect1's width is "<<rect1.getwidth()<<endl;
     int alength;
     int awidth;
     cout<<"\n\nNow enter a value for a new rectangle's length:";
     cin>>alength;
     cout<<"\nAnd now a value for its width:";
     cin>>awidth;
     rectangle rect2(alength, awidth);
     cout<<"The new rectangle's values are:\n";
     cout<<"length = "<<rect2.getlength()<<endl;
     cout<<"width = "<<rect2.getwidth()<<endl;
     system("PAUSE")
     return 0;
    }
    the code compiles and all but whenever i display the values for the new rectangle, it doesnt work. like ill enter 5 and 5 for rect2, but itll display like 37365978 and 792654973. can anyone tell me why this/how to fix it?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,775
    Code:
    rectangle::rectangle(int length, int width)
    {
     int itslength=length;
     int itswidth=width;
    }
    You should not be declaring local variables itslength and itswidth. They end up hiding the member variables.

    EDIT:
    Actually, in this case it may be better to combine to a single constructor, and declare it inline. You dont need to explicitly declare a destructor.
    Code:
    class rectangle
    {
    public:
    	rectangle(int length = 10, int width = 5) : itslength(length), itswidth(width) {}
    	int getlength() const {return itslength;}
    	int getwidth() const {return itswidth;}
    private:
    	int itslength;
    	int itswidth;
    };
    Last edited by laserlight; 05-29-2006 at 09:41 AM.
    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

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    25
    [QUOTE=chasingxsuns]this is my program----------

    Code:
    rectangle::rectangle(int length, int width)
    {
     int itslength=length; //you declared a new local variable
     int itswidth=width; //the same here
    }
    The value it displayed seemed weird because that was garbage. You declared two new local variables whose names are the same as member variables of the class. So when the constructor is automatically called, the values will be passed to the local variables, not member variable as you expected.
    You can modify your code by simply removing the "int" before itswidth and itslength, that tells the compiler that you're mutating the class member variables :

    Code:
    rectangle::rectangle(int length, int width)
    {
     itslength=length;
     itswidth=width;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading operators
    By ugmusicbiz in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2009, 12:41 PM
  2. classes and overloading constructors
    By barneygumble742 in forum C++ Programming
    Replies: 2
    Last Post: 09-14-2005, 05:32 AM
  3. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  4. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 09:16 AM
  5. Overloading constructors?
    By Brown Drake in forum C++ Programming
    Replies: 6
    Last Post: 09-20-2001, 10:37 AM

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