Reminder: Defining a Class
Defining the Cow
and Barn
Classes
Remember that you define the data members and member functions of a class in the class definition.
The class definition goes in a header file.
Here are the definitions for a
Barn
class and aCow
class!
class Barn {
public:
Barn();
Barn(size_t numCows);
void feed();
// Disable copying and assignment
Barn(const Barn& other) = delete;
Barn& operator=(const Barn& rhs) = delete;
private:
size_t cowCapacity_;
Cow residentCow_;
};
class Cow {
public:
Cow();
Cow(string name, size_t numSpots);
void feed();
// Synthesize copying and assignment automatically
Cow(const Cow& other) = default;
Cow& operator=(const Cow& rhs) = default;
private:
string name_;
size_t numSpots_;
bool hasBeenFed_;
};
Checking In
We're going to make a bunch of changes to the
Barn
class today, and we'll see how those changes affect object lifetime.First, let's make sure we understand the classes we're starting with.
Hay! Why did we disable the copy constructor and assignment operator for the
Barn
class, but let the compiler synthesize them for theCow
class?For one thing, we need to copy and assign
Cow
s, but these operations are less necessary for the ways we'll useBarn
s.Our first task will be to get to a usable
Barn
class without these operations, and then once we've done that, we'll think about what we need to do to provide them.
(When logged in, completion status appears here.)