CS 70

Exploring a C++ Class

Let's explore how we define and use classes in C++, and how we can use an object-based approach to structure our programs.

  • Duck speaking

    I've made classes before in Java and Python! So it's basically the same, right?

  • LHS Cow speaking

    Some things are quite similar, but there are lots of differences, some big and obvious and some much more subtle. Your experience in other languages can sometimes be useful but other times it can be quite misleading!

Take a look at the example code below, and

  • Identify the things that look familiar
  • Identify things that don't look familiar or you don't understand

Please spend a significant amount of time looking at the code! If you are able to, maybe you could even discuss it with someone else before you move on.

To help you explore, you can also open and run the code on-line using the link below, which will allow you to play around and make changes.

main.cpp:

/*
 * \file main.cpp
 * \author CS 70 Provided Code
 * \brief Creates and interacts with some Cows.
 */

#include <iostream>
#include "cow.hpp"

using namespace std;

int main() {
    Cow bessie{3, 12};
    const Cow mabel{1, 2};

    // This line wouldn't work!
    // Cow duke;

    bessie.moo(1);
    mabel.moo(2);
    bessie.setAge(4);

    // This line wouldn't work!
    // mabel.setAge(2);

    return 0;
}

cow.hpp:

/*
 * \file cow.hpp
 * \author CS 70 Provided Code
 * \brief Interface definition for the Cow class.
 */

#ifndef COW_HPP_INCLUDED
#define COW_HPP_INCLUDED

#include <iostream>
#include <fstream>

// A very very long scary comment tat apparently can't use s wc is weird, at least I tink.

/*
 * \class Cow
 * \brief Knows how many spots it has and how old it is. Can moo.
 */
class Cow {
 public:
    // We can only have a Cow if we know
    // how many spots it has and how old it is
    Cow(int numSpots, int age);
    Cow() = delete;

    // Moo the right number of times.
    void moo(int numMoos) const;

    // Accessor member functions
    int getNumSpots() const;
    int getAge() const;

    // Mutator member functions
    void setNumSpots(int numSpots);
    void setAge(int age);

 private:
    // Per-Cow data
    int numSpots_;
    int age_;
};

#endif  // ifndef COW_HPP_INCLUDED

cow.cpp:

/*
 * \file cow.cpp
 * \author CS 70 Provided Code
 * \brief Implements the Cow class
 */

#include <cstddef>
#include <iostream>
#include "cow.hpp"

using namespace std;

Cow::Cow(int numSpots, int age)
    : numSpots_{numSpots},
      age_{age}
{
    cout << "Made a cow with " << numSpots_ << " spots!" << endl;
}

void Cow::moo(int numMoos) const {
    for (int i = 0; i < numMoos; ++i) {
        cout << "Moo! ";
    }
    cout << endl;
}

int Cow::getNumSpots() const {
    return numSpots_;
}

int Cow::getAge() const {
    return age_;
}

void Cow::setNumSpots(int numSpots) {
    numSpots_ = numSpots;
}

void Cow::setAge(int age) {
    age_ = age;
}

Just Checking…

Did you spend a reasonable amount of time looking over this code to find both familiar and unfamiliar aspects of it?

(When logged in, completion status appears here.)