CS 70

Declarations/Definitions

  • Cat speaking

    I noticed that the class is separated into two files. What goes where?

  • LHS Cow speaking

    Good eye! This is different than in other languages you've studied.

The Class Header File

The header file (.hpp) contains the class definition. The class definition specifies the interface of the class: how other pieces of code should interact with this class. So it gives the name of the class and also declarations for all of the member variables and the member functions of the class.

  • Python speaking

    Actually, this is quite different from a class definition in Python! In Python, a class definition contains the definitions of all of the member functions, too!

  • Jo speaking

    And that's true in Java as well.

  • Bjarne speaking

    But in C++, we separate the interface from the implementation. The header file contains the interface (as a class definition), and the source file contains the implementation (the definitions of the member functions).

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>

/*
 * \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

Code Using the Class

A file that uses the class needs to #include the header file for the class. That way the compiler knows that the class exists and also knows the names and types associated with of all of the member variables and member functions.

main.cpp:

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;
}

The Class Source File

The source file (.cpp) for the class contains the member function definitions. This file is separately compiled into an object file (.o) and then linked into the final executable.

  • Horse speaking

    Hay! I noticed that cow.hpp is included in both main.cpp and cow.cpp. What's that all about?

  • LHS Cow speaking

    Oh, yeah! Well, member functions need to know about the class interface, too, right? The compiler needs to match the class name to know it is correct and also needs to know about member variables and member functions in case a function refers to them!

  • RHS Cow speaking

    So having #include "cow.hpp" incow.cpp ensures that the class is fully defined before we try to define all of its member functions.

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;
}

How many times will we need to run clang++ in order to compile this program into an executable file called main? Don't forget that we use clang++ for both compiling and linking!

We need to run clang++ three times:

  1. Compile: clang++ -c main.cpp to create main.o.
  2. Compile: clang++ -c cow.cpp to create cow.o.
  3. Link: clang++ -o main main.o cow.o to link main.o and cow.o into the executable main.

(When logged in, completion status appears here.)