Definitions
Inline Member Function
An inline member function is a function in a class definition that is within the class declaration.
Scope Resolution Operator ::
The scope resolution operator distinguishes entities by the location of their definition (library, namespace, class, etc.).
Method Declaration
A method’s declaration is the function’s signature. It “declares” to the compiler that the method exists, allowing the code to be compiled. But a definition is required to use a method.
It is not an error to include multiple copies of a method’s declaration may be included in a C++ project.
| |
Method Definition
A method’s definition is the implementation of a function. The definition can be attached to a declaration, or not. Only one definition of a method may exist.
Including multiple definitions of a method is an error.
| |
| |
Separate Files for Header & Implementation
For a class, we can separate the header file from the implementation. Conventionally, each class will have one header file and one implementation file. Separating the header from the implementation is conventional for C++ code.
Class.h— header file contains method declarations.Class.cpp— implementation file contains method definitions.
Exercise: Working with Another Programmer
Let’s simulate a team context. You are a member of a programming team. You have received a header file from the project’s software architect. The software must adhere to this design.
Given the following header file, write a main.cpp which will output the area of a Triangle object. You will need to implement the class methods in a RightTriangle.cpp file.
RightTriangle.h
| |
main.cpp
| |
RightTriangle.cpp
| |
Example: Separating a Class into a header and Implementation Files
Let’s separate the following program into 3 files:
main.cppPokemon.hPokemon.cpp
Entire Program
| |
Separated Into Files
This file will include the Pokemon.h header file and use the Pokemon class. Here we create a Pokemon object, set its properties, and output it to cout.
main.cpp
| |
Pokemon.h
This file will have the class’s data members and only declarations of the class methods. Here we simply have some getters, setters, and a format method.
| |
Pokemon.cpp
This file will contain the class’s method definitions, or implementations. Here we define the getters, setters, and the format method.
| |
Exercise: Using a Constructor to Set Private Fields
If a programmer wants the users of a class to manipulate the value of a class field, she can simply make that field public.
On the other hand, if a programmer does not want the users of a class to manipulate the value of a class field, she would make that field private.
However, a programmer may wish for a user to set the value of a class field, but not make arbitrary changes. In this situation, neither public nor private is sufficient.
To allow the desired behavior, a programmer may provide a constructor which sets an object’s private fields without exposing the field as public
Let’s set the fields of a Triangle class with a constructor.
| |