Re: Help with class definition for a beginner
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.
Second, destructors have not parameters, so it is more correctly to write
virtual ~Beverage();
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;
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; }
...
};
Fifth there is no need to specify pointer this before class members in C++.
And at last as I have said early it is not a good idea to use the same
identifier for class members and parameters.
Instead of
void Beverage::setSize(Size size) { this->size = size; }
it is better to write
void Beverage::setSize( Size theSize ) { size = theSize; }
You should have a system how syntactically specify you variables.
Vladimir Grigoriev
"Stick" <Stick@discussions.microsoft.com> wrote in message
news:7A47AA0D-33FC-467F-9B2A-D2BDFC9D8E32@microsoft.com...
"Arman Sahakyan" wrote:
class Beverage
{
public:
enum Size { TALL, GRANDE, VENTI};
// your code...
};
... and use it with the qualifier from ouside:
obj.setSize(Beverage::TALL);
Or declare the enum globally and use like so:
obj.setSize(TALL);
Note: it would be a good style to declare the input and output types of
Beverage::setSize and Beverage::getSize as enum Size.
void setSize(Size sz);
Size getSize();
Funny thing is that I had to change the return type of getSize() to int to
get it to compile. So, I assumed that on VS I couldn't return and enum
type.
My final working version was this:
class Beverage
{
public:
enum Size {TALL, GRANDE, VENTI};
Beverage();
Beverage(string, Size);
virtual ~Beverage(void);
void setSize(Size);
int getSize();
string displaySize();
virtual void setDescription(string);
virtual string getDescription();
virtual double cost() = 0;
private:
Size size;
string description;
};
// Beverage.cpp
//
#include "Beverage.h"
Beverage::Beverage() { }
Beverage::Beverage(string description, Size size = Beverage::TALL)
{
this->description = description;
this->size = size;
}
Beverage::~Beverage(){}
void Beverage::setSize(Size size) { this->size = size; }
int Beverage::getSize() { return this->size; }
string Beverage::displaySize()
{
switch(Beverage::size)
{
case Beverage::VENTI:
return "Venti ";
break;
case Beverage::GRANDE:
return "Grande ";
break;
default:
return "Tall ";
break;
}
}
string Beverage::getDescription() { return this->description; }
void Beverage::setDescription(string description) { this->description =
description; }
Darned if I could get it to work with and enum return type. It would tell
me I was missing a ';', and that it was not ok to have to default to int
and
that I was redefining the function.
Patrick