Thread: Learning C++ by Example

  1. #31
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by Elysia View Post
    You are right, but seeing as this is just an example, pay that detail no mind. Just assume that it cannot be calculated.
    Looking only to the example, I think this are the best:

    Code:
    void XMusicCd::SetLength(int Length);
    int XMusicCd::GetLength() const;
    I could be wrong, but I think the lenght is an attribute like the author.
    So, a cd as a array of tracks, and author (and more things) and if the length it is not calculated, it is only another attribute.

  2. #32
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sounds about right.
    So how about if I wanted to print the contents of a music CD's tracks, how would I do it?
    What would the function look like and how would the implementation look like?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #33
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by Elysia View Post
    Sounds about right.
    So how about if I wanted to print the contents of a music CD's tracks, how would I do it?
    What would the function look like and how would the implementation look like?
    Function:
    Code:
    void MusicCD::printMusicTrackContent(std::vector<XTrackInfo> TrackInfo)
    I think something like:
    Code:
    for (unsigned int i=0; i < TrackInfo.size(); i++){
        std::cout << "Track Name: " << TrackInfo[i].trackName() << 
        }
    Assuming there are a method named trackName to acess the XTrackInfo class type

  4. #34
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why are you passing a TrackInfo parameter to the function?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #35
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by Elysia View Post
    Why are you passing a TrackInfo parameter to the function?
    Yes, but it is wrong. I should wrote:

    Code:
    void XmusicCD::printMusicTrackContent(std::vector<XTrackInfo> & TrackInfo)

  6. #36
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What? You are still passing a TrackInfo parameter to the function. Again, my question is why?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #37
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by Elysia View Post
    What? You are still passing a TrackInfo parameter to the function. Again, my question is why?
    What I've thought was assuming that class

    Code:
    class XMusicCd
    {
    private:
        std::string m_Author;
        int m_Length;
        std::vector<XTrackInfo> m_TrackInfo;
     
    public:
        void SetAuthor(std::string Author);
        const std::string& GetAuthor() const;
    };
     
    std::array<XMusicCd, N> MusicCd;
    I should create a void print function which receives as argument the "getter" of m_TrackInfo and then according to its size, printting it.
    It is possible that I'm missunderstading something or just absence of knowlegde.

  8. #38
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>receives as argument the "getter" of m_TrackInfo
    What do you mean by the "getter"?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #39
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by Elysia View Post
    >>receives as argument the "getter" of m_TrackInfo
    What do you mean by the "getter"?
    I was assuming this:
    Code:
    class XMusicCd
    {
    private:
        std::string m_Author;
        int m_Length;
        std::vector<XTrackInfo> m_TrackInfo;
     
    public:
        void SetAuthor(std::string Author);
        const std::string& GetAuthor() const;
    
        void printMusicTrackContent(std::vector<XTrackInfo> & )
    };
     
    std::array<XMusicCd, N> MusicCd;
    and then:
    Code:
    void XmusicCD::printMusicTrackContent(std::vector<XTrackInfo> & TrackInfo)
    {  for (unsigned int i=0; i < TrackInfo.size(); i++){
        std::cout << "Track Name: " << TrackInfo[i].trackName() << 
        }
    }
    This is is my complete thought. It could be wrong

  10. #40
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And assuming you have this then, how would you call this print function from, say, main?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #41
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by Elysia View Post
    And assuming you have this then, how would you call this print function from, say, main?
    Code:
    XMusicCd myCD;
    myCD.printMusicTrackContent()
    ?

  12. #42
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Your code does not compile:
    Code:
    class XTrackInfo {};
    
    class XMusicCd
    {
    private:
        std::string m_Author;
        int m_Length;
        std::vector<XTrackInfo> m_TrackInfo;
      
    public:
        void SetAuthor(std::string Author);
        const std::string& GetAuthor() const;
     
    	void printMusicTrackContent(std::vector<XTrackInfo>&) {}
    };
      
    int main()
    {
    	XMusicCd myCD;
    	myCD.printMusicTrackContent();
    }
    Error 1 error C2660: 'XMusicCd::printMusicTrackContent' : function does not take 0 arguments

    As an exercise, modify the code such that it compiles.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #43
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by Elysia View Post
    Error 1 error C2660: 'XMusicCd::printMusicTrackContent' : function does not take 0 arguments

    As an exercise, modify the code such that it compiles.
    It took some time. It works, but I don't know if it is corrrect!

    https://github.com/CreativeSoftware/XmusicTest

  14. #44
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It is not correct. To make it clearer, let me modify the example:

    Code:
    // You may not modify XTrackInfo
    class XTrackInfo
    {
    	std::string t_trackName;
    	int t_length;
    public:
    	XTrackInfo();
    	XTrackInfo(std::string trackName, int tLength);
    	void setTrackName(std::string trackName);
    	void setTrackLength(int tLength);
    	const std::string& GetTrackName() const;
    	int GetTrackLength() const;
    };
    
    XTrackInfo::XTrackInfo() {}
    XTrackInfo::XTrackInfo(std::string trackName, int tLength)
    	:t_trackName(trackName), t_length(tLength)
    {}
    const std::string &XTrackInfo::GetTrackName() const
    {
    	return t_trackName;
    }
    int XTrackInfo::GetTrackLength() const
    {
    	return t_length;
    }
    void XTrackInfo::setTrackLength(int tLength)
    {
    	t_length = tLength;
    }
    void XTrackInfo::setTrackName(std::string trackName)
    {
    	t_trackName = trackName;
    }
    
    class XMusicCd
    {
    private:
    	// You may not modify these member variables
    	std::string m_Author;
    	std::vector<XTrackInfo> m_TrackInfo;
    
    public:
    	// You may not modify these member functions
    	XMusicCd();
    	XMusicCd(std::string Author, int mLength, std::vector<XTrackInfo> Tracks);
    	
    	void SetAuthor(std::string Author);
    	const std::string& GetAuthor() const;
    
    	int GetLength() const;
    
    	void AddTrack(XTrackInfo NewTrack) {} // Left incomplete on purpose; you will implement it later
    
    	// Add additional functions to this class as necessary to make it compile.
    };
    
    void PrintCdContents(const XMusicCd& Cd)
    {
    	// Implement this function such that it compiles
    	// Also, to be explicit, you may not add any tracks to the CD in here. That is not the purpose of this function. You may also not change the signature of the function.
    	// You shall specifically not modify the CD object by calling any setters.
    }
    
    int main()
    {
    	// You may not change this function
    	XMusicCd MyCD;
    	PrintCdContents(Cd);
    }
    I want this code to compile. You may change the implementation of the class and the PrintCdContents, but not the main function.
    This should force you to think about what functions a class should have, what information these functions need and where it comes from, and especially ownership of data.
    This is a perfectly natural program and a perfectly natural interface that users of a class should come to expect. This is also probably what you would see when someone designs a class.

    So what you should think about:
    - Who owns data? The caller or the class itself? Does the class own the author or does the function the utilizes the class? Which makes more sense?
    - What additional functions, if any, do you need to make the code work?
    - What information does these functions need, if there needs to be added any, and where does it get that information from? What makes most sense?

    Don't feel ashamed to ask for help if you're wondering something or get stuck. This is just an exercise, after all.
    (I removed the SetLength and the total length member seeing as you incorporated the track into your design, so it should not be necessary anymore.)
    Last edited by Elysia; 08-14-2014 at 10:15 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #45
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    I'm stuck. But I'll try to look into code more time.
    However, I must ask.

    you wrote:
    Code:
    XMusicCd MyCD;
    PrintCdContents(Cd);
    Shouldn't be ? :

    Code:
    XMusicCd MyCD;
    PrintCdContents(MyCD);
    Last edited by marcoesteves; 08-16-2014 at 07:24 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Learning C++
    By sLIVER in forum C++ Programming
    Replies: 2
    Last Post: 07-11-2007, 08:18 PM
  2. Learning
    By nojoegohome in forum C++ Programming
    Replies: 6
    Last Post: 07-06-2006, 04:33 AM
  3. Learning Dos and learning Windows
    By blankstare77 in forum C++ Programming
    Replies: 8
    Last Post: 07-31-2005, 03:48 PM
  4. Learning?
    By bob20 in forum A Brief History of Cprogramming.com
    Replies: 41
    Last Post: 12-11-2002, 10:17 AM

Tags for this Thread