Thread: Learning C++ by Example

  1. #91
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by Elysia View Post
    I am going to agree with jimblumberg here. This project is just way too advanced for you at the moment.
    I strongly suggest you do basic exercises found in beginners' books for now. Feel free to chime in (in a new thread) if you have problems with THOSE.
    Learn how to work with most standard library types. How to iterate them, insert things, delete things, etc.
    THEN come back to this project.
    Yes, it is advanced, but I want to reach it

  2. #92
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by jimblumberg View Post
    Also can you provide the Author, Publisher, and date of Publication of the book you are referring to?

    Jim
    I still have the original code and project at git.
    I stuck on the simple example provided my Elysia

    I need to learn concepts. Probably I need a mentor too.
    Last edited by marcoesteves; 09-03-2014 at 04:52 AM.

  3. #93
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    I read documentation, and I don't know what is missing.

    When I try to print the array, the problem is always the same.
    I'm not able to convert Xmusic CD type into a char, and I know that isn't suppose too.

    For which I read, I thought the following instructions were able to do what I've want, however the compiler doesn't agree with me.

    Code:
    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.
       //Cd.
    
        std::cout << "Author : " << Cd.GetAuthor() << "\n";
        std::cout << "\n" << std::endl;
        std::cout << "Track Info" << std::endl;
    
        std::copy(Cd.GetTracks().begin(), Cd.GetTracks().end(), std::ostreambuf_iterator<char>(std::cout));
        std::copy(Cd.GetTracks().begin(), Cd.GetTracks().end(), std::ostream_iterator<char>(std::cout, " "));
        for (auto c : Cd.GetTracks()){
    
           std::cout << c << ' ';
       }
    
        //Cd.GetTracks();
    }
    The complete code is here:
    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    
    // You may not modify XTrackInfo
    class XTrackInfo
    {
        std::string m_TrackName;
        int m_Length;
    
    public:
        XTrackInfo() {}
    
        XTrackInfo(std::string TrackName, int Length):
            m_TrackName(std::move(TrackName)),
            m_Length(Length)
        {}
    
        void SetTrackName(std::string TrackName) { m_TrackName = std::move(TrackName); }
        void SetTrackLength(int Length) { m_Length = Length; }
        const std::string& GetTrackName() const { return m_TrackName; }
        int GetTrackLength() const { return m_Length; }
    };
    
    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, std::vector<XTrackInfo> Tracks):
            m_Author(std::move(Author)),
            m_TrackInfo(std::move(Tracks))
        {}
    
        void SetAuthor(std::string Author) { m_Author = std::move(Author); }
    
        const std::string& GetAuthor() const { return m_Author; }
        const std::vector<XTrackInfo> GetTracks() const { return m_TrackInfo;}
    
        int GetLength() const; // Left incomplete on purpose; you will implement it later
    
    
        void AddTrack(XTrackInfo NewTrack){
            //m_TrackInfo.emplace_back(NewTrack.GetTrackName(),NewTrack.GetTrackLength());
    
            m_TrackInfo.emplace_back(std::move(NewTrack));
    
        }
    
    
        // 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.
       //Cd.
    
        std::cout << "Author : " << Cd.GetAuthor() << "\n";
        std::cout << "\n" << std::endl;
        std::cout << "Track Info" << std::endl;
    
        std::copy(Cd.GetTracks().begin(), Cd.GetTracks().end(), std::ostreambuf_iterator<char>(std::cout));
     //   std::copy(Cd.GetTracks().begin(), Cd.GetTracks().end(), std::ostream_iterator<char>(std::cout, " "));
    //    for (auto c : Cd.GetTracks()){
    
    //        std::cout << c << ' ';
    //    }
    
        //Cd.GetTracks();
    
    
    }
    
    int main()
    {
        // You may not change this function
        XMusicCd MyCd;
        MyCd.SetAuthor("Hello World");
        MyCd.AddTrack(XTrackInfo("This is a test", 100));
        MyCd.AddTrack(XTrackInfo("This is a test 2", 200));
        PrintCdContents(MyCd);
    }

  4. #94
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And what type is *Cd.GetTracks().begin()?

    Hint: To easily find a type, you can do:

    int x = *Cd.GetTracks().begin();
    Or

    template<typename T> void Debug(); // Don't implement
    Debug<decltype(*Cd.GetTracks().begin())>();

    The compiler (or linker) will yield error messages which will contain the exact types.
    These are beginner mistakes! You shouldn't be having trouble with these things if you're ready to implement this.
    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. #95
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by marcoesteves
    I appreciate your advice, however I'm too stubborn to give up at least now.
    I'm trying do both things in parallel. Learn and Implemmenting.
    It is clear that I'm missing something, probably I didn't find any appeal books.
    Quote Originally Posted by marcoesteves
    Yes, it is advanced, but I want to reach it
    There is this so-called Telescope Rule: 'Thomson's Rule for First-Time Telescope Makers: "It is faster to make a four-inch mirror then a six-inch mirror than to make a six-inch mirror."'

    I have no idea if the rule is genuine, but the spirit of the rule applies here: it is faster/easier to learn by doing simpler projects and then do a more complex project than it is to do the more complex project.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #96
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Yes, it is advanced, but I want to reach it
    That's fine but you need to take small steps, not giant leaps. Learn the basics then things won't seem as complicated.

    IMO your latest attempt is not what you should be trying. You should be iterating through the vector with for(;;) loops not using iterators. Iterators are not the answer to every programming problem and this problem can be solved easier without their use. And as I said in my last post you need to slow down, backup and learn the basics. Start with a simpler "project" like the one I introduced in my last post. Once you understand how to access a class from within a class switching to using the vector is very very simple. But until you really understand how classes interact you'll probably never really understand what is happening and your chances for success will be doubtful.

    Again in this instance I don't recommend using iterators until you actually understand how to access a single instance of your Track class.


    Jim

  7. #97
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by jimblumberg View Post
    You should be iterating through the vector with for(;;) loops not using iterators. Iterators are not the answer to every programming problem and this problem can be solved easier without their use.
    I disagree. Iterators are the common way of iterating through every standard container. Therefore it should follow that, unless you have need of indices, it should be the standard way of doing it and the first thing to learn and try. It's not harder. It's simpler because it also reduces the syntax and clutter with range-based for loops.
    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.

  8. #98
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I disagree. Iterators are the common way of iterating through every standard container.
    IMO the problem here is not trying to iterate through the vector but accessing elements of the "embedded" class. Until the OP knows how to access a single instance of the "Track" class from the "CD" class he'll never be able to access the elements of the vector, using any method.

    And IMO it's better to first learn to access individual elements of the vector using loops without the iterators, once this concept is learned iterators become much simpler.


    Jim

  9. #99
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by laserlight View Post
    There is this so-called Telescope Rule: 'Thomson's Rule for First-Time Telescope Makers: "It is faster to make a four-inch mirror then a six-inch mirror than to make a six-inch mirror."'

    I have no idea if the rule is genuine, but the spirit of the rule applies here: it is faster/easier to learn by doing simpler projects and then do a more complex project than it is to do the more complex project.
    I agree with you, I'll slow down. I think the root of the problem is that I've studied OOD before OOP with C++. So, since I know that is conceptuably possible I want do, however I don't know basic concepts and "tricks", which come with practice on implementation.

  10. #100
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by jimblumberg View Post
    IMO the problem here is not trying to iterate through the vector but accessing elements of the "embedded" class. Until the OP knows how to access a single instance of the "Track" class from the "CD" class he'll never be able to access the elements of the vector, using any method.

    And IMO it's better to first learn to access individual elements of the vector using loops without the iterators, once this concept is learned iterators become much simpler.


    Jim
    Sure, but you have to learn how to iterate containers anyway, even to access a single element, be that by iterators or indices, so that point is rather moot IMO. You can access individual elements of a vector with iterators, too.
    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. #101
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by jimblumberg View Post
    IMO the problem here is not trying to iterate through the vector but accessing elements of the "embedded" class. Until the OP knows how to access a single instance of the "Track" class from the "CD" class he'll never be able to access the elements of the vector, using any method.
    This is what I felt.

    I've started learning c++ a couple months. And first books and examples that I look into didn't mentioned STL, however with Elysia help and digging on another books I found out how powerfull it is.

    And my current problem it is : I don't know how to acess the content of the TrackInfo inside of CD class. This is a composition problem

  12. #102
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by Elysia View Post
    Sure, but you have to learn how to iterate containers anyway, even to access a single element, be that by iterators or indices, so that point is rather moot IMO. You can access individual elements of a vector with iterators, too.
    I Agree, but I have a prior problem: Don't know how to access the TrackInfo Vector inside CD class.
    I'm digging

  13. #103
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, like we've said, you need to take a step back and do some proper exercises in beginner books. A good list of recommended books can be found here.
    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.

  14. #104
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by Elysia View Post
    Well, like we've said, you need to take a step back and do some proper exercises in beginner books. A good list of recommended books can be found here.
    Thanks, but most of them are not using c++11 standard.

    I think this is another problem. The first type that I use c++ was in 2004. I did 2 or 3 simple projects(simple inheritance, simple user interface).
    Since 2004, I have never seen C++ again, and lots of things have changed. I need to learn from "beggining", however some begginer concepts are too beginner, and some intermediate concepts are too hard for me. It's hard find a book which fits my needs :\
    Last edited by marcoesteves; 09-03-2014 at 09:14 AM.

  15. #105
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The only book that, at least somewhat, learns and teaches C++11 that I know of is this one.
    There is also this.
    I have read neither of these books, so I don't know how well they cover the language, though.
    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.

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