CS 70

Reminder: Defining a Class

Defining the Cow and Barn Classes

  • LHS Cow speaking

    Remember that you define the data members and member functions of a class in the class definition.

  • RHS Cow speaking

    The class definition goes in a header file.

  • LHS Cow speaking

    Here are the definitions for a Barn class and a Cow 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

  • RHS Cow speaking

    We're going to make a bunch of changes to the Barn class today, and we'll see how those changes affect object lifetime.

  • LHS Cow speaking

    First, let's make sure we understand the classes we're starting with.

How much space will a Barn take up on the stack?

Which of the following statements are true? (select all that apply)

  • Horse speaking

    Hay! Why did we disable the copy constructor and assignment operator for the Barn class, but let the compiler synthesize them for the Cow class?

  • LHS Cow speaking

    For one thing, we need to copy and assign Cows, but these operations are less necessary for the ways we'll use Barns.

  • RHS Cow speaking

    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.)