Thread: Learning C++ by Example

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    122

    Learning C++ by Example

    Hello !

    I've started a thread Vector of Objects and Constructors and Elysia helped me a lot.

    I figured out that I'm committed lots of mistakes. So, I decided pursuing learn c++ reading books and doing examples. I step back, and I hope learn more consistently and at same help others learning C++.

    I think that is important apply C++ concepts to an aggregate example in order to apply all of the basic concepts, from design to the implementation. I create a case study text: https://dl.dropboxusercontent.com/u/...%20C%2B%2B.pdf

    Currently this is my Class Diagram, do you think is it correct?
    https://dl.dropboxusercontent.com/u/...%20diagram.svg

    After completing the Class Diagram I'll jump to the implementation and I'll provide all code.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    DiamondStore:
    SaveToFile is probably okay (so you can read the data back into a store at a later date, i.e. serialization), but not PrintData is not for reasons I explained before.
    GetProduct should take an argument that identifies what product you want.

    Product:
    Year is missing.
    All getters should take no argument and return the type of the variable they're returning (you've made this mistake in several places, not just in Product).
    All member variables should be protected.

    AudioTrack:
    Missing "number" from Each track has a title, number and length.
    Missing getters.

    MusicCD:
    It makes no sense to accept a parameter to set the price since the specification says they must cost 15 euros. It does not make sense to be able to change the price, either, so remove the setter.
    Missing getter for enumerating the audio tracks (or better yet, an iterator interface).
    GetPrice is inherited from Product, so you don't need to override it here. Remove it.
    SetPrice should be overloaded and made private or protected.
    The class shall not redefine members that it inherits from Product (i.e. price).

    Album:
    Specification doesn't say there must be an album class. It only says that the amount of tracks in a music CD may be variable. Not sure if an album class if necessary.
    If it is, then you're going about it wrong since the specification says A music CD can be a double or triple album (i.e. you're doing it the other way around).

    GameCD:
    Again, do not redeclare price which is inherited, and do not redeclare the setter and getter for price. Also overloaded SetPrice and make it protected/private to ensure the price cannot be changed.
    Missing constructors.

    Book:
    Again, do not redeclare price which is inherited, and do not redeclare the setter and getter for price. Also overloaded SetPrice and make it protected/private to ensure the price cannot be changed.
    Missing constructors.
    You don't seem to distinguish between blue and green books. Since the only thing that changes between them is price, you should probably get away with using an enum class to distinguish between the two which must be passed to the Book class.
    Some getters don't have a return type (you've made this mistake in several places, not just in Book).
    All member variables should be protected.

    ComicBook:
    Missing constructors.

    After all this, you should carefully consider for each class: are there are sane defaults? For example, for a book, can I get away with not specifying barcode, title, author, etc? If I cannot get away with it, then it must be a requirement to specify these parameters when creating such a class. If such is the case, consider disallowing a default constructor (this makes it so you can't forget to specify important data).

    This is the first step. After this comes meeting the rest of the requirements.
    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. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by Elysia View Post
    Album:
    Specification doesn't say there must be an album class. It only says that the amount of tracks in a music CD may be variable. Not sure if an album class if necessary.
    If it is, then you're going about it wrong since the specification says A music CD can be a double or triple album (i.e. you're doing it the other way around).
    I've changed the case Study. However, I don't know if it is needed a class, or just add a array of MusicCds at Store. I made it clear that all cds are in boxes. Games only have 1 CD, and musicCds, usually called albums, can be 2 or more cds.
    What do you think?

    Quote Originally Posted by Elysia View Post

    MusicCD:
    It makes no sense to accept a parameter to set the price since the specification says they must cost 15 euros. It does not make sense to be able to change the price, either, so remove the setter.
    Missing getter for enumerating the audio tracks (or better yet, an iterator interface).
    How can I add an iterator interface at UML diagram?

    Quote Originally Posted by Elysia View Post
    Book:
    You don't seem to distinguish between blue and green books. Since the only thing that changes between them is price, you should probably get away with using an enum class to distinguish between the two which must be passed to the Book class.
    After looking to "How to implement an enum class", how can I connected in a UML Diagram? I found the symbol, but I don't how to use it.

    I think this is a very complete Case to implement c++

    This my updated ClassDiagram (xmi and svg)
    https://dl.dropboxusercontent.com/u/...0diagram_2.svg
    https://dl.dropboxusercontent.com/u/...ondStore_3.xmi
    This is my case study:
    https://dl.dropboxusercontent.com/u/...%20C%2B%2B.pdf

    I think it is close to start the implementation phase.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by marcoesteves View Post
    I've changed the case Study. However, I don't know if it is needed a class, or just add a array of MusicCds at Store. I made it clear that all cds are in boxes. Games only have 1 CD, and musicCds, usually called albums, can be 2 or more cds.
    What do you think?
    I only see what was before -- that a Music CD is one or more albums. Having an array of Music CDs at the store isn't a good idea - the idea is that each product shall be a single product as far as the store is concerned. Polymorphism means abstraction, and that means that the base class shall not know of the derived classes. As far as it's concerned, the store carries only products. If each product can be multiple products suddenly, then you're going to increase the complexity of the Store because all products end up there and that means all products must meet the requirements of the store (i.e., take into the fact that they can, in actuality, be multiple products).

    How can I add an iterator interface at UML diagram?
    You should study the concept of iterators. However, as for implementation, I can add that since you're probably going to use a vector, you can use its iterators as your class's iterators for MusicCD for iterating tracks. But to understand that, take a look at what iterators are first and how to use and implement them.

    After looking to "How to implement an enum class", how can I connected in a UML Diagram? I found the symbol, but I don't how to use it.
    The idea is that the enum class should contain the book type, i.e. green or blue. Then, as it's a type, you should composition to bind it to the book class.

    I think this is a very complete Case to implement c++
    It's certainly a good start.
    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. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by Elysia View Post
    You should study the concept of iterators. However, as for implementation, I can add that since you're probably going to use a vector, you can use its iterators as your class's iterators for MusicCD for iterating tracks. But to understand that, take a look at what iterators are first and how to use and implement them.


    The idea is that the enum class should contain the book type, i.e. green or blue. Then, as it's a type, you should composition to bind it to the book class.
    I read about both concepts and I think this is the "way" of write them into UML Class Diagram.
    I'm starting Implementation by creating the base classes, I'll update tomorrow.
    In parallel i'll wait for the Design evaluation.

    Class Diagram (Svg format)
    https://dl.dropboxusercontent.com/u/...0diagram_3.svg

    Class Diagram (Xmi) format)
    https://dl.dropboxusercontent.com/u/...ondStore_4.xmi

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, I won't say it's perfect. It's also hard to see is some things are just mistakes (such as label in Book is really a string or the enum class) or if you haven't properly gone through the feedback you've received.
    But I'll just say go ahead with your initial design. You will probably learn a lot of from it and we can catch mistakes on the way because code does not lie.
    Also going through all the feedback again to properly evaluate if you've taken it into account properly would be a good idea. I have mentioned some things which I don't see fixed.
    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. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Hello

    I've created a new git, with the current project.
    I don't know yet, how to add the classDiagram Content to the ReadMe, but I'll find out

    https://github.com/CreativeSoftware/...pByExample.git


    At this is point I have some doubts. I created the class Product. Should I create the "Add product function" at main ?
    Should I use intermediate variables (asking from user) and then store everything at overload constructor? Should I try do step-by-step using my "set functions" ?

    The "add product function" should receive a product type, or any other derivedclass dataype, such as album ?

    I find myself confused. However, I think this is the "initial confusion" with the evolution, things will be clearer.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by marcoesteves View Post
    At this is point I have some doubts. I created the class Product. Should I create the "Add product function" at main ?
    That seems to me like it should be in the store. You might even argue that it is the store's job to add things such as bar code.

    Should I use intermediate variables (asking from user) and then store everything at overload constructor? Should I try do step-by-step using my "set functions" ?
    I don't know exactly what you mean by this.

    The "add product function" should receive a product type, or any other derivedclass dataype, such as album ?
    No, it should take a product, not any of its derived classes. That's the point of polymorphism: you take a product and all of the derived classes are products.

    I find myself confused. However, I think this is the "initial confusion" with the evolution, things will be clearer.
    Sure, that typically happens when one first tackles a subject, but those confusions go away once you're knowledgeable enough.

    Also, I will stress this: since you are working with polymorphism, you need to properly understand pointers, references and smart pointers. You cannot pass a polymorphic base class by value or you will get type slicing. So add product must not take its argument by value. So the question becomes how do you do it? The easiest solution is to just take a smart pointer to the base class and let the caller allocate the proper product. Otherwise you will need to make a copy of the polymorphic object which presents problems since you don't know its exact type (here is typically where a virtual clone function is used).
    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. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by Elysia View Post
    That seems to me like it should be in the store. You might even argue that it is the store's job to add things such as bar code.


    I don't know exactly what you mean by this.


    No, it should take a product, not any of its derived classes. That's the point of polymorphism: you take a product and all of the derived classes are products.


    Sure, that typically happens when one first tackles a subject, but those confusions go away once you're knowledgeable enough.

    Also, I will stress this: since you are working with polymorphism, you need to properly understand pointers, references and smart pointers. You cannot pass a polymorphic base class by value or you will get type slicing. So add product must not take its argument by value. So the question becomes how do you do it? The easiest solution is to just take a smart pointer to the base class and let the caller allocate the proper product. Otherwise you will need to make a copy of the polymorphic object which presents problems since you don't know its exact type (here is typically where a virtual clone function is used).
    Probably you're right, but I don't know how to implement.
    After starting with c++, I've only do programs with procedures.
    So, I've started to study OO concepts and design. I understand the concepts (pointers and polymorphism) but I'm not able to figure out how can I use it to help me. Firstly how can I write the code, Secondly how can I "connect" everything.

    I think it is a normal "messing". I'm learning !

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by marcoesteves View Post
    Probably you're right, but I don't know how to implement.
    By adding a add product function in your store.

    So, I've started to study OO concepts and design. I understand the concepts (pointers and polymorphism) but I'm not able to figure out how can I use it to help me. Firstly how can I write the code, Secondly how can I "connect" everything.
    You have your UML diagram. Start by implementing those functions as best you can.
    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. #11
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    I'm doing by best.
    I found out that my design was wrong. It makes no sense a musiccd being a product. An Album is a product which can contain 1 or more cds. A cd only contains track objects.

    I implement as best as I can the audiotrack and musicCD . My current problem is: "I don't know how can I test it".
    I have my github updated and my classDiagram.
    https://github.com/CreativeSoftware/...gCppByExample/
    https://dl.dropboxusercontent.com/u/...0diagram_4.png

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why is MusicCD::addNewTracks asking for input and creating new audio tracks which it adds when the parameter MyTracks contains all the tracks to add?
    Also let me ask with respect to which standard are you compiling? C++03 or C++11?
    As for testing, that's pretty easy.

    You know that, for example, for AudioTrack, what you set is what you must get, so:

    AudioTrack MyTrack(1, "Hello", 1, 1);
    MyTrack.GetTrackNumber() == 1 must hold
    MyTrack.GetTrackTitle() == "Hello" must hold
    MyTrack.GetMinutes() == 1 must hold
    MyTrack.GetSeconds() == 1 must hold
    and the same for when using the setters.

    For simple testing, you can wrap all the getters in asserts and run the program. If you get an assertion, you have a bug; otherwise the program is fine.

    Also:
    Product constructor does not initialize year.
    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. #13
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by Elysia View Post
    Why is MusicCD::addNewTracks asking for input and creating new audio tracks which it adds when the parameter MyTracks contains all the tracks to add?
    Also let me ask with respect to which standard are you compiling? C++03 or C++11?
    I'm trying to learn c+11. However, some books and examples are using old-style.
    But my problem was qtcreator. I didn't know how can I setup to C++. And I found out! Everything is working with C+11.

    I don't know how to use setters with cin. I read some examples, but usually they are on main function, and they use local variables to set constructors, like this:
    Code:
            std::cout << "Track Number: ";
            std::cin >> trackno;
            myTrack.setTrackNumber(trackno);
    I want to know how can I use mutator functions to do so.

    Other Problem that I'm facing is using the vector. I'm not sure that I'm thinking properly. My idea is: Collect data from user or an input (and later from file), set the the AudioTrack object with the correspondent attributes and then adding it to the Vector Mytracks.
    A musicCD class is an abbstraction, a musicCD is a vector of tracks.

    I changed my git. I think the name was too general. Probably in Future I'll create more examples, so this particular one, is a bookstore example
    https://github.com/CreativeSoftware/...kStoreExample/

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by marcoesteves View Post
    I don't know how to use setters with cin. I read some examples, but usually they are on main function, and they use local variables to set constructors, like this:
    Code:
            std::cout << "Track Number: ";
            std::cin >> trackno;
            myTrack.setTrackNumber(trackno);
    I want to know how can I use mutator functions to do so.
    This example is not using the constructor, but rather a mutator function. Regardless of using the constructor or a mutator function, the approach is the same.
    Read the necessary data, then construct a new object, passing along the data to the constructor, or, construct a default object and call the mutators functions to set the parameters.

    Other Problem that I'm facing is using the vector. I'm not sure that I'm thinking properly. My idea is: Collect data from user or an input (and later from file), set the the AudioTrack object with the correspondent attributes and then adding it to the Vector Mytracks.
    A musicCD class is an abbstraction, a musicCD is a vector of tracks.
    Sure, that sounds good. What's wrong with it?
    You'd basically create tracks in main, then call a setter on the music cd object to add the track to 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.

  15. #15
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by Elysia View Post

    Sure, that sounds good. What's wrong with it?
    You'd basically create tracks in main, then call a setter on the music cd object to add the track to it.
    I'm glad that I'm thinking correctly. However, I'm doing something wrong.
    Could you help?
    I've updated my git
    https://github.com/CreativeSoftware/...kStoreExample/

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