class composition constructor question...

This is a discussion on class composition constructor question... within the C++ Programming forums, part of the General Programming Boards category; Hi and thanks for reading. I purchased a small interpolation class from www.codecogs.com (really good site, by the way) and ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Location
    US
    Posts
    14

    class composition constructor question...

    Hi and thanks for reading.
    I purchased a small interpolation class from www.codecogs.com (really good site, by the way) and wanted to use the interpolation class in my little project.
    I am running into problems and have been struggling a bit.
    I'm simplifying things, but here goes:
    I have a base class called "force" a derived class called "myForce" and the math class from codecogs called "Cubic".
    Base class:
    Code:
    class Force
    {
    protected:
        Maths::Interpolation::Cubic m_cubic;
    public:
        Force();
    };
    Force::Force()
    {
    }
    Code:
    class myForce: public Force
    {
    public:
        SpringForce();
    };
    Code:
    class Cubic
    {
    public:
         Cubic(int n, double *x, double *y);
    private:
         int m_n;
         double *m_x, *m_y, *m_b, *m_c, *m_d;
    }
    With this code the compiler tells me that there are no appropriate default constructor available for the Cubic class. Fair enough. Can someone explain to me how does constructor of the aggregate class object (Cubic, in this case) is called?
    I'm having a hard time understanding the relationship between base class constructor (that I omitted), and the constructor for the member data m_cubic that is part of the Force class.
    If anyone has a simple example I'd be much grateful!!
    Andre.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    Use the initializer list. Here's a couple options:
    Code:
    Force::Force() : m_cubic(0, 0, 0)
    {
    }
    Code:
    Force::Force(int n, double *x, double *y) : m_cubic(n, x, y)
    {
    }
    
    SpringForce::SpringForce(int n, double *x, double *y) : Force(n, x, y)
    {
    }
    The first initializes the values to zero in the base class (probably not what you want or the Cubic class would provide a default constructor). The second lets the person constructing the Force set the values. If the user uses the SpringForce, then it should call the base class constructor with appropriate values as shown.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    24
    I thought that if you did not write a constructor one would be created that does not have any parameters ??

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    That's true. What about my post makes you doubt that?

    In the Cubic class, you do have a constructor. That means the compiler won't create a default constructor (with no parameters) for you. That is why you have to call the existing constructor from your Force class, because there is no default constructor for it to call to initialize the m_cubic member.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

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