Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    DaZ
    DaZ is offline
    New Coder
    Join Date
    Dec 2002
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can anyone shed some light please...

    just starting out basically in C++

    ive got a class declared like the following;

    -----------------------------------------------------------------
    class rectangle
    {
    public:
    rectangle();
    ~rectangle();
    void SetLength(int length){itslength = length;}
    int GetLength() const { return itslength;}

    void SetWidth(int width){itswidth = width;}
    int GetWidth() const {return itswidth;}

    private:
    int itslength;
    int itswidth;
    };
    -----------------------------------------------------------------

    the part of the code i dont understand is when declaring the constructor function underneath that section of code, the book i have states that it should be like the following
    -----------------------------------------------------------------
    // rectangle constructor declaration
    rectangle::rectangle():
    itswidth(5),
    itslength(10)
    {}
    -----------------------------------------------------------------

    now, this compiles and works correctly. and sets the member variables to 5 and 10 for itswidth and itslength.

    however the only way ive learned to do this before is
    // rectangle constructor declaration
    rectangle::rectangle()
    {
    itswidth =5;
    itslength=10;
    }

    this code also works correctly and compiles fine.

    is the code ive put in above, that came with the book some kind of shortcut?? or does C++ allow for slight typos.

  • #2
    Regular Coder
    Join Date
    Nov 2002
    Location
    India
    Posts
    159
    Thanks
    0
    Thanked 0 Times in 0 Posts
    rectangle::rectangle():
    itswidth(5),
    itslength(10)
    {}
    It's been sometime since I used C++. The above declaration works for user-defined data types. For Example,
    I have two classes A and B with some contructors.

    And, I have another class C which has two objects one each of the above mentioned classes. You may pass the parameters to the constructors of the objects of classes A and B as above.
    Code:
    class A {
    public:
    	A() {
    		cout << "No-arg constructor";
    	}
    
    	A(int i) {
    		cout << i;
    	}
    }
    
    class B {
    public:
    	B() {
    		cout << "No-arg constructor";
    	}
    
    	B(int i, float f) {
    		cout << i << f;
    	}
    }
    
    class C {
    private:
    	A oa;
    	B ob;
    
    public:
    	C() {
    		cout << "No-arg constructor";
    	}
    	C(float f, int i):oa(i), ob(i, f) {}
    }
    Calling the constructor of class C with two args would in turn call the one-arg constructor of oa and the two-arg constructor of ob.

  • #3
    Regular Coder
    Join Date
    Nov 2002
    Location
    India
    Posts
    159
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I dont know if it works for primitive data types too.

  • #4
    DaZ
    DaZ is offline
    New Coder
    Join Date
    Dec 2002
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    nice one thanks.

    its just that the book ive got goes along quite happily, and then it throws something like that in and doesnt explain it..

    thanks again for clearing it up for me.
    DaZ


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •