CS3505 Lecture4
CS3505 Lecture4
Lecture 4:
Headers, Makefiles, and Libraries
Assignment Questions?
Back to the Point class
Destructors
• Destructors are called when an object leaves
scope or delete is called on an object.
• Looks like
~Point() { stuff }
• Destructors are needed when the class has
acquired resources that need to be cleaned
up
– memory
– open files/network connections/etc.
• Does Point need a destructor?
Using Multiple Files
• Most C++ projects are broken into multiple
files
– Often move each class into its own file(s)
• An implementation in a .cpp file
• An interface in a header file with a .h extension
– The header file is #include’d in .cpp files
• This is a very simple model
• During compilation, the .h text is (virtually) inserted
into the .cpp file at the location of the include
• Break point.cpp into multiple files.
#include
• #include is an example of a preprocessor directive – it
tells the compiler to do something
• Two main ways of including
– #include <systemheader>
• searches in known locations for systemheader
– #include “myLocal.h”
• looks in local directory and project specified locations
• Why not systemheader.h?
– C-style headers are <stdio.h>
– C++-style lack the .h to be different
• there is a file called “systemheader” on our system
Design Rules for .h files
• Definition in Ex.h
class Ex {
int x; // data member
public:
Ex(int x); // constructor
};
No implementation, just forward declaration
• Implementation of constructor in Ex.cpp
Ex::Ex(int x) : x(x) {
}
class Ex {
int x_;
public:
Ex(int x);
Ex(std::ostream);
};
This means we will give full name
Design Rules for .h files
• Use minimal includes in the header
#include <iostream>
class Ex {
int x_;
public:
Ex(int x);
Ex(std::ostream);
};
Design Rules for .h files
• The implementation .cpp should not
assume the .h will continue to have
includes such as <iostream>
#include <iostream> #include “Ex.h”
class Ex { #include <iostream>
int x_;
public: Ex::Ex(int x) : x_(x) {
Ex(int x); }
};
g++ Compilation
• g++ really does two things.
– Compile the source
– Link the files together
• If Point(x,y) is defined in point.cpp, then when
main.cpp is compiled it
– Sees the prototype of Point(x,y) in the .h file
– Enters that in some internal table
– When it is used in main.cpp it knows it should exist
– During linking, it makes the connection between the
prototype table and the actual compiled implementation
Object Files
• You can compile without linking
g++ -c point.cpp
• Make
– A system for compiling projects
– Uses a Makefile to define compilation rules
– Speeds compilation by only redoing ‘touched’
files.
Makefiles
• Basic Makefile setup
“Thing to make”: “what it depends on”
“How to make it”
• A simple Makefile for point is
– Note that the .h is in the dependencies but not
in the compilation line – the .cpp code
includes it
point: main.cpp point.h point.cpp
g++ -o point main.cpp point.cpp
• Type ‘make’ or ‘make point’ to compile
More Advanced
• Define compilation properties with
variables
Design Patterns
Toolkits (libraries)
Languages
Gang of Four (GoF)
• Design Patterns book
launched this concept
for software
– borrowed from the
“patterns” idea in
architecture
– 4 authors
Façade Design Pattern
• façade
– an outward appearance that is maintained to
conceal a less pleasant or creditable reality.
• Problem: external code may be difficult to
use and end up tightly-coupled to yours.
• Solution: wrap a subset of the external
library in an easier-to-use interface
Façade Example: Initial Interface
class Light {
public:
void on() { std::cout << "Lights on" << std::endl; }
void off() { std::cout << "Lights off" << std::endl; }
};
class TV {
public:
void on() { std::cout << "TV on" << std::endl; }
void off() { std::cout << "TV off" << std::endl; }
};
The Façade
class houseControlFacade {
public:
Light hall,kitchen;
TV basement, main, porch, bathroom;