Thread: Constructors and Conditionals

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

    Constructors and Conditionals

    Hello guys, I've started learning C++ a few days ago.
    I read about the language and the OOD. I'm trying implement this "conceptual exercise":
    This is my classDiagram:
    https://dl.dropboxusercontent.com/u/...0diagramV3.png

    Currently I have a problem. I don't know how can I use constructors with conditionals. And, on the other hand how to create the methods (set /get) the variables at this case.

    I want the price of a book has different values according to the string "marker". For instance, if the "marker" has the value "blue", the book price is 20.

    My code can be downloaded from

    github:
    https://github.com/CreativeSoftware/DiamondQt

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What do you mean by "use constructors with conditionals"?

    Quote Originally Posted by marcoesteves
    I want the price of a book has different values according to the string "marker". For instance, if the "marker" has the value "blue", the book price is 20.
    This sounds like the kind of thing that would be stored in a database of some sort. For example, perhaps you would have a file for which each line corresponds to an entry, and each entry has a name (e.g., "blue") and a value (e.g., "20"). You would then read the file, parse its contents into say, a std::map or std::unordered_map, and then when a book object is created with "blue", its price will be set to 20 by consulting the map.
    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

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Yes, You're right, this is "bad" approach.
    I'm just trying to figure out different implementations applyng to an example.

    What I want know, is if it is possible create different class constructors using conditionals.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by marcoesteves
    What I want know, is if it is possible create different class constructors using conditionals.
    No, it isn't. Constructors are defined at compile-time. You can of course create different objects conditionally.
    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

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    So, on my "example".
    I must initialize the price object, then verify the "marker", and then update its price?

    Or in other hand, create 2 different constructors?

    (I've tried both, but I got compiling errors, actually the code which is on github has 2 compiling errors)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Incrementation in conditionals
    By Niels_M in forum C Programming
    Replies: 2
    Last Post: 07-20-2010, 11:19 AM
  2. conditionals
    By s_siouris in forum C Programming
    Replies: 3
    Last Post: 03-11-2008, 07:29 AM
  3. How to eliminate irrelevant conditionals?
    By cdave in forum C Programming
    Replies: 9
    Last Post: 12-10-2005, 04:39 PM
  4. circle and conditionals
    By a1pro in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2005, 02:05 AM
  5. Conditionals
    By uniqueniq in forum C Programming
    Replies: 6
    Last Post: 02-13-2003, 06:20 PM

Tags for this Thread