Re: Help with class definition for a beginner
"Vladimir Grigoriev" wrote:
Some remarks upon your final version. First of all you should take into
account that you have created an abstract data type, so you cannot create
objects of the type Beverage.
Yes, I'm aware of this. This is intentional. This an abstract base class I
use to learn a pattern like this:
int main()
{
Beverage* pBeverage = new Decaf();
cout << pBeverage->displaySize() << pBeverage->getDescription() << " $" <<
pBeverage->cost() << endl;
delete pBeverage; pBeverage = NULL;
cout << endl;
}
Second, destructors have not parameters, so it is more correctly to write
virtual ~Beverage();
You mean without the void as a parameter. Yes, an oversight, thanks.
Third, if you write interface function that only returns a class member
value it is a good idea to declare it with const specifier
Size getSize() const;
Interesting. I'll have to study this. Thanks.
Forth IMO such simple functions as
int Beverage::getSize() { return this->size; }
it is better do define inline i..e. inside class
class Beverage
{
public:
...
Size getSize() const { return size; }
...
};
Yes, very definately. Just easier for me to have all the code in one spot
in the .cpp file while I learn.
Fifth there is no need to specify pointer this before class members in C++.
The reason I am using the pointers is that this enables me to select from
intellisense what I am looking for in the class, and see what intellisense
sees. This helps me in several ways as I learn - except that I have had to
also learn that intellisense in VS has some bugs :)
Thanks for your tips.