Problems implementing Polymorphism

Hello guys.
I'm trying understand and learn C++.
I've started to read a book, then I read about OOP and Design.
I want to implement a conceptual model of a Library store.
This is the Class Diagram that I want to implement.

https://dl.dropboxusercontent.com/u/710615/class%20diagram.png

My current problem is: How can I can redefine the price at musicCDs ?

THis is my source code:
https://dl.dropboxusercontent.com/u/710615/newDiamanteProject.zip
Thank you
The data members in Product should be protected instead of private, as in:

1
2
3
4
5
6
7
8
class Product
{
protected:

    string _barCode, _title, _author, _publisher;
    int _price;

    // ... 


The difference is that protected allows derived classes to see the members.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Base
{
private:

    int i;

protected:

    int j;
};

class Derived: public Base
{
    void func()
    {
        i = 10; // doesn't work
        j = 10; // works
    }
};


Edit: also you shouldn't use names starting with underscore, such as _author.

In C++, identifiers starting with _ and __ are reserved for C++ Standard Library programmers (those who code iostream, string, etc), so in theory there could be name conflicts. Use author_ instead.
Last edited on
I took all your advices, but probably i'm missing something.

I've got this error:
error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘<unresolved overloaded function type>’)
cout << "price: " << price << "\n";

Updated code
https://dl.dropboxusercontent.com/u/710615/newDiamanteProject_v2.zip

Updated class diagram
https://dl.dropboxusercontent.com/u/710615/class%20diagram.png
Last edited on
Looks like a typo: do you mean price_ instead?

cout << "price: " << price_ << "\n";
> The data members in Product should be protected instead of private
Not really. The class already provides an interface to access the members, use the interface.
We may discuss if that interface is appropriate, but given those members are part of the `Product' class they should be managed there (by instance, to ensure LSP)

"Scott Meyers Effective C++ 3rd Ed. Item 22 Declare members private" also provides a rationale of why protected members shouldn't be used (it compares them with public)


¿what is a `Diamond' and why you say that `Product' is a `Diamond'?
Last edited on
Diamond is the name that I gave to the "store". The store has Products. I think this kind of class is called "abstract", I'm not sure.

With the changes that Catfish suggested and changed price to price_ into musicCD . The output is 2, which is incorrect. The price object has 15 as value.

ne555 I read something similiar what your are mentioning. So, as I beginner I thought the correct is: "keep everything as possible private and then provide methods to access then".
But I'm in troubles :) I don't get it how to put every OOP concepts working together.

I hope that you could help me :)
> The output is 2, which is incorrect.
1
2
3
4
5
6
    cdBox(string const &barCode, string const &title,
            string const &author, string const &publisher, int price, int noCds );

musicCD::musicCD(string const &barCode, string const&title,
            string const &author, string const &publisher, int noCds, int nsongs, int price)
:cdBox(barCode, title, author, publisher, noCds, price),
the order of the parameters is incorrect



> and then provide methods to access them
No, methods are to perform actions, no to simply retrieve data.
http://www.javaworld.com/article/2073723/core-java/why-getter-and-setter-methods-are-evil.html


> Diamond is the name that I gave to the "store". The store has Products.
¿so why is Product deriving from Store?


> I think this kind of class is called "abstract"
abstract class have at least one pure virtual member function, and cannot be instantiated.
Ok, I've correct the order, I didn't know this makes difference.

So, I shouldn't have a parent class like Store, to "hold" my products?

From the text that you are mentioning, are a bad programming practice create "accessor methods" ?
What I should do? Use public and protected keywords?
Change architecture ?

Thanks, it is very usefull your help to understand this begginner OO concepts.

> So, I shouldn't have a parent class like Store, to "hold" my products?
¿do you treat a `Product' as you would an `Store'? ¿why do you think it is a good idea to derive `Product' from `Store'? ¿what advantages do that give you?

it seems that you are trying to implement http://en.wikipedia.org/wiki/Composite_pattern


> From the text that you are mentioning, are a bad programming practice create "accessor methods" ?
¿have you read the article?
http://pragprog.com/articles/tell-dont-ask
Topic archived. No new replies allowed.