Declarations/Definitions
I noticed that the class is separated into two files. What goes where?
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.
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!
And that's true in Java as well.
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.
Hay! I noticed that
cow.hpp
is included in bothmain.cpp
andcow.cpp
. What's that all about?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!
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;
}
We need to run clang++
three times:
- Compile:
clang++ -c main.cpp
to createmain.o
. - Compile:
clang++ -c cow.cpp
to createcow.o
. - Link:
clang++ -o main main.o cow.o
to linkmain.o
andcow.o
into the executablemain
.
(When logged in, completion status appears here.)