Thread: Learning C++ by Example

  1. #121
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    I didn't find out how to.
    My idea is create a track object and then gettracks. then iterate, but doesn't allow me, because tracks doesn't have the getTracks()
    Code:
        auto tracks = MyCds.GetTracks;
    //    for (unsigned i = 0; i < tracks.size(); ++i) {
    //        std::cout << tracks.at(i).GetTrackName() << std::endl;
    //    }
    Update Code:
    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    
    
    
    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;}
    
    
        void AddTrack(XTrackInfo NewTrack){
    
            m_TrackInfo.emplace_back(std::move(NewTrack));
    
        }
    
        int GetLenght() const{
    
            int sum=0;
    
            auto && MyTracks = this->GetTracks();
    
            for (auto && elem : MyTracks)
            {
                sum+= elem.GetTrackLength();
    
            }
    
        return sum;
        }
    
    
    
    
    };
    
    class XCollection
    {
        private:
    
    
        std::vector <XMusicCd> m_cdCollection;
    
        public:
        XCollection(){}
        XCollection(std::vector <XMusicCd> CdCollection):
        m_cdCollection(std::move(CdCollection))
        {}
    
    
        const std::vector <XMusicCd>& GetCds() const {return m_cdCollection;}
    
        void addCD(XMusicCd NewCD){
    
            m_cdCollection.emplace_back(std::move(NewCD));
    
       }
    
    
    
    
    };
    
    //void CalcTotalLen(const XMusicCd& Cd){
    //    int sum = 0;
    //    auto && MyTracks = Cd.GetTracks();
    
    //    for (auto && elem : MyTracks)
    //    {
    //        sum+= elem.GetTrackLength();
    
    //    }
    //    std::cout << "total len: "<< sum << std::endl;
    
    //}
    
    void PrintCollectionContents(const XCollection& Collection){
        //std::cout << "Cd number : " << "1" << "\n";
        std::cout << "Cd info : " << std::endl;
    
        auto MyCds = Collection.GetCds();
    
        for(auto && elem: MyCds){
            std::cout << elem.GetAuthor() << std::endl;
    
            //How can I acess the tracks of the particular cd ?
        }
        auto tracks = MyCds.GetTracks;
    //    for (unsigned i = 0; i < tracks.size(); ++i) {
    //        std::cout << tracks.at(i).GetTrackName() << std::endl;
    //    }
    
    
    }
    
    void PrintCdContents(const XMusicCd& Cd)
    {
        std::cout << "Author : " << Cd.GetAuthor() << "\n";
        std::cout << "\n" << std::endl;
        std::cout << "Track Info" << std::endl;
    
    
        //std::vector<XTrackInfo> tracks = Cd.GetTracks();
         auto && MyTracks = Cd.GetTracks();
    
    //    for (std::vector<XTrackInfo>::const_iterator it = tracks.begin(); it != tracks.end(); ++it) {
    //        std::cout << it->GetTrackName() << std::endl;
    //        std::cout << it-> GetTrackLength() << std::endl;
    //    }
    
    //     std::vector<XTrackInfo> tracks = Cd.GetTracks();
    //     for (unsigned i = 0; i < tracks.size(); ++i) {
    //         std::cout << tracks.at(i).GetTrackName() << std::endl;
    //     }
         for (auto && elem : MyTracks)
         {
                 std::cout << elem.GetTrackName() << std::endl;
                 std::cout << elem.GetTrackLength() << std::endl;
         }
    
    
    }
    
    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);
        //CalcTotalLen(MyCd);
        std::cout << "total cd lenght: " << MyCd.GetLenght()<< std::endl;
    
        XMusicCd MyCd2;
        MyCd2.SetAuthor("Hello World 2");
        MyCd2.AddTrack(XTrackInfo("This is a test", 400));
        MyCd2.AddTrack(XTrackInfo("This is a test 2", 500));
        PrintCdContents(MyCd2);
        //CalcTotalLen(MyCd);
        std::cout << "total cd lenght: " << MyCd2.GetLenght()<< std::endl;
    
        XCollection MyCollection;
        MyCollection.addCD(MyCd);
        MyCollection.addCD(MyCd2);
        //MyCollection.size();
        PrintCollectionContents(MyCollection);
    
    }

  2. #122
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by marcoesteves View Post
    I didn't find out how to.
    My idea is create a track object and then gettracks. then iterate, but doesn't allow me, because tracks doesn't have the getTracks()
    Code:
        auto tracks = MyCds.GetTracks;
    //    for (unsigned i = 0; i < tracks.size(); ++i) {
    //        std::cout << tracks.at(i).GetTrackName() << std::endl;
    //    }
    Each collection contains a CD.
    Each CD contains tracks.
    So:

    Code:
    for (auto && Cd : Cds.GetCds()) // ...for every cd...
    for (auto && Track : Cd.GetTracks()) // ... for every track...
    // your code
    We shall also talk more about auto && later, but for now, you can keep using it.
    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. #123
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    What's with all the "You may not change this ..." comments?

  4. #124
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by antred View Post
    What's with all the "You may not change this ..." comments?
    Elysa's initial instructions.

  5. #125
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Quote Originally Posted by marcoesteves View Post
    Elysa's initial instructions.
    Ah, ok. Sorry, didn't read through the entire thread (it's rather long).

  6. #126
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by antred View Post
    Ah, ok. Sorry, didn't read through the entire thread (it's rather long).

    no Problem

  7. #127
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Ok, it is fine, I think that I'm too attached at i 's and j's .

    Now, I'm thinking: "Ok print them all it's fine, but I want to acess a particular index? My class types doesn't have "at" implemented. It is possible using iterators?

    It's time to go to the deeper example, or just test more functionalities at this one?


    Updated code:
    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    
    
    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:
    
        std::string m_Author;
        std::vector<XTrackInfo> m_TrackInfo;
    
    public:
    
        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;}
    
    
        void AddTrack(XTrackInfo NewTrack){
    
            m_TrackInfo.emplace_back(std::move(NewTrack));
    
        }
    
        int GetLenght() const{
    
            int sum=0;
    
            auto && MyTracks = this->GetTracks();
    
            for (auto && elem : MyTracks)
            {
                sum+= elem.GetTrackLength();
    
            }
    
        return sum;
        }
    
    };
    
    class XCollection
    {
        private:
    
    
        std::vector <XMusicCd> m_cdCollection;
    
        public:
        XCollection(){}
        XCollection(std::vector <XMusicCd> CdCollection):
        m_cdCollection(std::move(CdCollection))
        {}
    
    
        const std::vector <XMusicCd>& GetCds() const {return m_cdCollection;}
    
        void addCD(XMusicCd NewCD){
    
            m_cdCollection.emplace_back(std::move(NewCD));
    
       }
    
    };
    
    
    void PrintCollectionContents(const XCollection& Collection){
    
    
        std::cout << "Cd info : " << std::endl;
    
        for (auto && Cd : Collection.GetCds()){ // ...for every cd...
            std::cout << Cd.GetAuthor() << std::endl;
    
            for (auto && Track : Cd.GetTracks()) // ... for every track...
                std::cout << Track.GetTrackName() << std::endl;
        }
    
    }
    
    void PrintCdContents(const XMusicCd& Cd)
    {
        std::cout << "Author : " << Cd.GetAuthor() << "\n";
        std::cout << "\n" << std::endl;
        std::cout << "Track Info" << std::endl;
    
         auto && MyTracks = Cd.GetTracks();
    
         for (auto && elem : MyTracks)
         {
                 std::cout << elem.GetTrackName() << std::endl;
                 std::cout << elem.GetTrackLength() << std::endl;
         }
    
    
    }
    
    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);
        //CalcTotalLen(MyCd);
        std::cout << "total cd lenght: " << MyCd.GetLenght()<< std::endl;
    
        XMusicCd MyCd2;
        MyCd2.SetAuthor("Hello World 2");
        MyCd2.AddTrack(XTrackInfo("This is a test  3", 400));
        MyCd2.AddTrack(XTrackInfo("This is a test  4", 500));
        PrintCdContents(MyCd2);
    
        std::cout << "total cd lenght: " << MyCd2.GetLenght()<< std::endl;
    
        XCollection MyCollection;
        MyCollection.addCD(MyCd);
        MyCollection.addCD(MyCd2);
        PrintCollectionContents(MyCollection);
    
    }

  8. #128
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by marcoesteves View Post
    Now, I'm thinking: "Ok print them all it's fine, but I want to acess a particular index? My class types doesn't have "at" implemented. It is possible using iterators?
    Code:
    std::vector<int> v;
    // Now create code that accesses the n'th element of v.
    
    struct X { std::vector<int> v; };
    X x;
    // Now create code that accesses the "n'th element" of x.
    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. #129
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by Elysia View Post
    Code:
    std::vector<int> v;
    // Now create code that accesses the n'th element of v.
    
    struct X { std::vector<int> v; };
    X x;
    // Now create code that accesses the "n'th element" of x.
    I don't know if I'm doing something wrong when I create the project.

    I think the 1st part is done, the second one not, something is missing.

    Code:
    #include <iostream>
    #include <vector>
    
    std::vector<int> v;
    // Now create code that accesses the n'th element of v.
    
    
    struct X { std::vector<int> v; };
    
    // Now create code that accesses the "n'th element" of x.
    
    int main()
    
    {
        X x;
    
        for (int i=1; i<10; i++) v.push_back(i);   // 1 2 3 4 5 6 7 8 9
    
    
         std::cout << "myvector contains:";
         for (std::vector<int>::iterator it=v.begin(); it!=v.end(); ++it)
           std::cout << ' ' << *it;
         std::cout << '\n';
    
         std::cout << "myvector contains:";
    
         for (std::vector<int>::iterator it=x.v.begin(); it!=x.v.end(); ++it)
           std::cout << ' ' << *it;
    
         std::cout << '\n';
    
        return 0;
    }

  10. #130
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Here it is another update. I hope that is what you ask for Elysia:

    Code:
    #include <iostream>
    #include <vector>
    
    std::vector<int> v;
    // Now create code that accesses the n'th element of v.
    
    
    struct X { std::vector<int> v; };
    
    X x;
    
    // Now create code that accesses the "n'th element" of x.
    
    int main()
    
    {
         for (int i=1; i<10; i++) v.push_back(i);   // 1 2 3 4 5 6 7 8 9
    
    
         std::cout << "myvector contains:";
         for (std::vector<int>::iterator it=v.begin(); it!=v.end(); ++it)
           std::cout << ' ' << *it;
         std::cout << '\n';
    
         //Struct
    
         x.v.resize(2);
         x.v[0] =1;
         x.v[1] = 2;
    
    
         std::cout << "myvector in struct contains:";
         for (std::vector<int>::iterator it=x.v.begin(); it!=x.v.end(); ++it)
           std::cout << ' ' << *it;
         std::cout << '\n';
    
        return 0;
    }

  11. #131
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Does this exercise answer your question?
    "Now, I'm thinking: "Ok print them all it's fine, but I want to acess a particular index? My class types doesn't have "at" implemented. It is possible using iterators?"
    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.

  12. #132
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by Elysia View Post
    Does this exercise answer your question?
    "Now, I'm thinking: "Ok print them all it's fine, but I want to acess a particular index? My class types doesn't have "at" implemented. It is possible using iterators?"
    Iterators come with magic xD

    Should I apply to the more complex example all of this Stuff?

  13. #133
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    I've updated a git : https://github.com/CreativeSoftware/LearnCppByExample

    How can I can I make a book price change according to the label color?

    I was thiking about user interface. Probably is a good practice the user interface be independent of all code.
    In other words, to the current classes it must doesn't matter if it is a window or a prompt from Console. Should I create another class naming Interface?

  14. #134
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by marcoesteves View Post
    How can I can I make a book price change according to the label color?
    Well, first off, decide who is responsible for setting the pricing (i.e. what class/component in your system).
    If that component is not public (i.e. it is hidden because you do not want the user to be able to change the price directly; this makes sense in certain situations where price is determined by certain factors and only specific components are allowed to change that), then decide the path the user must go to change that price.
    Then decide what information that component must get in order to change its price. If the component is not public, do this step recursively going backwards from the component that can change the price to the public interface the user can call to change the price.

    I was thinking about user interface. Probably is a good practice the user interface be independent of all code.
    In other words, to the current classes it must doesn't matter if it is a window or a prompt from Console. Should I create another class naming Interface?
    Yes. This is always a good idea because it creates a well-defined component in your system that handles specific things related to that component. That way you centralize and abstract your code so that you reduce complexity in other parts and protect them from changes.
    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. #135
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Book's price it is a function of labelcolor.
    So, it seems to me correct price be a private attribute without setfunction (to the user), however with a mutator function to the program.

    When a user sets a label color changes the price.

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