Constructors for Composed Objects

This is a discussion on Constructors for Composed Objects within the C++ Programming forums, part of the General Programming Boards category; Hi there This is my first cpp program, so please be patient with me... I am trying to practice applying ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    24

    Constructors for Composed Objects

    Hi there

    This is my first cpp program, so please be patient with me...


    I am trying to practice applying constructors on a composed class, and have learned that in case of a composed class, the outer class ctor is responsible for initialaizing the inner class, and this can be achieved by using an initialization list.

    So here's my code:


    class Point
    {

    private:
    int x;
    int y;
    };



    class Rectangle
    {
    public:
    Rectangle(int px, int py, int width, int height);
    height);
    private:

    Point x;
    Point y;
    int m_width;
    int m_height;
    };


    and heres my ctor :

    Rectangle::Rectangle(int px,int py,int width,int height)
    :x(px),y(py),m_width(width),m_height(height)
    {}



    Still, I get a compiler error C2664: '__thiscall Point::Point(const class Point &)' : cannot convert parameter 1 from 'int' to 'const class Point &'
    Reason: cannot convert from 'int' to 'const class Point'
    No constructor could take the source type, or constructor overload resolution was ambiguous

    I understand the meaning of this error, but isnt this what init. list are for?


    Please help!


    Thanks

    Gozlan
    And if you get no joy from music hall
    Remember there is always alcohol
    And If you get no joy from Gin
    Here is the abyss jump in

  2. #2
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    In your Point class definition you have to also add constructor(s) and a destructor. Right now there isn't any.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    24
    I did now.

    I added
    Point(int px,int py);
    ~Point();

    to my Point class.

    Still , I get the same error
    And if you get no joy from music hall
    Remember there is always alcohol
    And If you get no joy from Gin
    Here is the abyss jump in

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Post the code where you are attempting to instantiate the objects.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    24
    What I've specified below is the only code I've written.


    Still, it should compile without object instantiating.
    And if you get no joy from music hall
    Remember there is always alcohol
    And If you get no joy from Gin
    Here is the abyss jump in

  6. #6
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Since there is no inheritance involved there seems to be some sort of conflict regarding access, so you might have to use an accessor method or else make Point a friend of Rectangle. I'm not sure, I'm just learning C++ myself.

    point.h file:
    Code:
    #ifndef _point_h
    #define _point_h
    
    class Point
    {
     public:
    
      Point() { _x = 0; _y = 0; }
      ~Point () { }
      void Set(int x, int y) { _x = x; _y = y; }
    
        private:
      int _x;
      int _y;
    };
    
    class Rectangle
    {
     public:
      Rectangle (int, int, int, int);
      ~Rectangle() { }
     private:
      Point x;
      int _width;
      int _height;
    };
    
    
    Rectangle::Rectangle (int p1, int p2, int width, int height)
      { 
        _width = width; 
        _height = height;
        x.Set(p1,p2);
      }
    
    #endif
    point.cpp file
    Code:
    #include"point.h"
    #include<iostream>
    
    int main()
    {
      Rectangle rec(2,4,30,50);
      
      return 0;
    }

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    24
    T
    And if you get no joy from music hall
    Remember there is always alcohol
    And If you get no joy from Gin
    Here is the abyss jump in

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    24
    Thanks Troll King


    I would like to get to the bottom of this initialization list , which is supposed to enable an efficient initialization of composed objects.


    Unless of course I got the whole idea wrong , which I am counting on the members of this forum to let me know.
    And if you get no joy from music hall
    Remember there is always alcohol
    And If you get no joy from Gin
    Here is the abyss jump in

  9. #9
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    I wouldn't mind that answer myself. I think it might have to do with the fact that Point and Rectangle do not inherit from each other, and the data members are private, rather than protected, but I can't be sure. Hopefully some master class programmer will provide us with the answer.

  10. #10
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    It's a shame that we don't have any C++ers here.

  11. #11
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    You use initializer list when the private data members are
    constant, have non-default constructors or are referances.

  12. #12
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Can the Rectangle class call the Point classes constructor and pass it arguments, or do you have to use an assessor function like I did above. The classes do not inherit from each other. The assesor function worked, but I was getting errors when I tried to call the Point constructor. The original poster was getting errors as well. This is what we are trying to understand.

  13. #13
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    You can pass arguments to the constructor. I think the reason for the error is that he uses
    Code:
     Rectangle::Rectangle(int px,int py,int width,int height)
    :x(px),y(py),m_width(width),m_height(height)
    {}
    when he wants
    Code:
    Rectangle::Rectangle(int px, int py, int width, int height)
     : x(px, py), y(px, py), m_width(width), m_height(height)
    {
    
    }
    I'm not sure why the need for two points x and y.

  14. #14
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    You're right. Great. I had the syntax wrong when I tested it:

    point.cpp file
    Code:
    #include"point.h"
    #include<iostream>
    
    int main()
    {
      Rectangle rec(2,4,30,50);
      Rectangle rec2();
    
      return 0;
    }
    point.h file:
    Code:
    #ifndef _point_h
    #define _point_h
    
    class Point
    {
     public:
      Point () { _x = 0; _y = 0; }
      Point(int x, int y) { _x = x; _y = y; }
      ~Point () { }
    
        private:
      int _x;
      int _y;
    };
    
    class Rectangle
    {
     public:
      Rectangle() { _width = 0; _height = 0; }
      Rectangle (int, int, int, int);
      ~Rectangle() { }
    
     private:
      Point x;
      int _width;
      int _height;
    };
    
    
    Rectangle::Rectangle (int p1, int p2, int width, int height)
      :  _width(width), _height(height), x(p1,p2)
    {
    }
    
    #endif

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Initializing Objects with constructors
    By freddyvorhees in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2008, 08:11 AM
  2. Passing Objects, Constructors, Pointers, References, Values ???
    By BlackSlash12 in forum C++ Programming
    Replies: 24
    Last Post: 12-14-2007, 06:26 PM
  3. Global objects and exceptions
    By drrngrvy in forum C++ Programming
    Replies: 1
    Last Post: 09-29-2006, 08:37 AM
  4. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  5. chain of objects within pop framework help needed
    By Davey in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2004, 11:01 AM

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