Reputation: 836
So I'm really really confused about c++ inheritance. I've been reading around the internet and can't seem to wrap my head around it very well.
I have a package class, the package can be either a Letter, Box, or Crate.
Each package has a tracking number, weight, and price to ship.
There is a cost function that needs to be overwritten from the base class. So I've been writing this out and can't seem to get it working properly.
I'm mainly confused about what to do with Package. Since all packages will have the three attributes, do I make a setter in the package.cpp or would I make setters in each of the subclasses to set the attributes? Each package will have to have their own constructor correct? In my sub classes I just include package.h
because that is the super class? Do I even need anything in the package.cpp
if I'm just using it as an interface essentially?
This is what my Package class looks like:
class Package{
protected:
double price;
int weight;
int trackingNumber;
public:
void setPrice(double price);
void setWeight(int weight);
void setTrack(int trackNum);
virtual double cost(int weight) = 0;
};
Am I doing this correctly or should I be doing most of the work inside my subclasses, such as the constructors and deconstructors?
Upvotes: 1
Views: 158
Reputation: 5249
Since all packages will have the three attributes, do I make a setter in the package.cpp or would I make setters in each of the subclasses to set the attributes? Only in package.cpp if these are common properties for all sub-classes.
Each package will have to have their own constructor correct?
Correct.
In my sub classes I just include package.h because that is the super class? Yes, you need to include that because sub-class definition will refer to the base class.
Do I even need anything in the package.cpp if I'm just using it as an interface essentially?
You should implement all functionality common for all sub-classes here, e.g. it makes perfect sense to implement common setters for price, weight and tracking number by assigning values to instance variables:
double price;
int weight;
int trackingNumber;
You might also want to add getters for the same properties to the base class (package.cpp)
Upvotes: 1
Reputation: 2576
For common attributes places the getters and setters in the super class Package
the subclasses will inherit these from Package
.
Put a base (may be empty) implementation of cost
function in Package
and override it in each of the subclasses.
Also provide a virtual destructor in Package. Any class with pure virtual methods only acts like an interface in C++.
Upvotes: 1
Reputation: 14505
If the values of those attributes are known when derived classes are instantiated, then you don't need to define those setters either in the base class or derived classes. You can pass them to the base in the constructor of derived classes.
E.g.,
class Package
{
public:
Package(double price, int weight, int trackNum)
{}
...
};
class DerivedPackage: public Package
{
public:
// Pass to the contructor of the base class
DerivedPackage(double price, int weight, int trackNum):
Package(price, weight, trackNum)
{}
...
};
If the values can't be determined during object construction (i.e., you need set them at a later time), I suggest you put those setters to the public part of the base class because they are part of your interface and thus can be reused (e.g., if you add more derived classes later).
Upvotes: 1