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.
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.
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";
> 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'?
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.
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?
> 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