Need help with Composition

This is a discussion on Need help with Composition within the C++ Programming forums, part of the General Programming Boards category; Could anybody point me in the direction of a good tutorial/example of composition being used. I am having some trouble ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    7

    Need help with Composition

    Could anybody point me in the direction of a good tutorial/example of composition being used. I am having some trouble implementing it in a project. Specifically, where it occurs within a class, the header and .cpp file. Thanks

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Here is an example of composition. The class ComposedOfInt is composed of an int. If you're talking about what I think you are talking about, then composition is just the use of a type as a member of a new class. Often, composition is preferred to inheritance when extending the interface of an existing class. If this is not what you are talking about, maybe give a little more information.
    Code:
    class ComposedOfInt
    {
    public:
        explicit ComposedOfInt(int data = 0) : data_(data) { }
        int getData() { return data_; }
        void setData(int data) { data_ = data; }
    private:
        int data_;
    };
    
    #include <iostream>
    
    int main()
    {
        ComposedOfInt coi(3);
        std::cout << coi.getData() << std::endl;
        coi.setData(coi.getData() + 2);
        std::cout << coi.getData() << std::endl;
    }
    Last edited by jlou; 10-25-2004 at 10:55 PM. Reason: Thanks JaWiB! :oops:

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Hmm I think you meant to put:
    Code:
    class ComposedOfInt
    {
    public:
        explicit ComposedOfInt(int data = 0) : data_(data) { }
        int getInt() { return data_; }
        void setInt(int data) { data_ = data; }
    private:
        int data_;
    };
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The only reference I have heard to composition in C++ is defining a class within another. But that only once.

    Explain more what you are trying to do and what trouble you are having.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-10-2007, 03:51 PM
  2. Composition & Aggregration
    By aldajlo in forum C++ Programming
    Replies: 1
    Last Post: 10-26-2004, 04:39 PM
  3. difference between aggregation and composition
    By anil_beloved in forum C Programming
    Replies: 1
    Last Post: 10-18-2004, 01:17 PM
  4. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 04:41 AM
  5. composition, i think
    By blight2c in forum C++ Programming
    Replies: 3
    Last Post: 05-01-2002, 09:49 PM

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