Presentation is loading. Please wait.

Presentation is loading. Please wait.

Separating Definition & Implementation

Similar presentations


Presentation on theme: "Separating Definition & Implementation"— Presentation transcript:

1 Separating Definition & Implementation
.h and .cpp explained

2 Modules Copy & Paste is not modular or scalable
Need mechanism to import existing classes/code

3 The Process Build: Preprocess Compile Link Run: Load

4 Separate Compilation Each .cpp has to compile on its own
Needs everything declared Hey, this thing is going to exist… Does not need actual code

5 Linking Linking combines separate object files into one executable
Everything better exist Better only have one copy

6 Finding headers Include with < > looks in compiler defined directories Include with " " looks in your project, then regular directories

7 Guards Should not have multiple declarations of same class/function/etc… Easy to do by accident with headers Include A and B, B also includes A

8 Guards Preprocessor guard:
#ifndef : if this symbol not defined, read until #endif #define : define a symbol CIRCLE_H made up should be unique

9 .h File Class declaration goes in .h file Clients include .h

10 .cpp File Function implementation must happen once Dedicated .cpp file
Include .h file and other headers NO MAIN!!!

11 Header Rules .h file .cpp file Header – declaration of data type
Only include other .h files if necessary .cpp file Code – implementation of functions Include necessary headers for declarations Never include .cpp files in other files

12 Naming Conventions C++ doesn't have one standardized convention
I encourage Class names : CapitalizeEachWord class Person, class BankAccount File names : .h and .cpp match and match class name Person.h, Person.cpp Variable names : camelCase Person bob; BankAccount accountOne;

13 Naming Members State (member variables) are (usually) nouns
int hour string address int numberOfAuthors Behavior (member functions) are (usually) verbs void printTime() int getHour()

14 Shadowing Watch out for shadowed names

15 Shadowing Watch out for shadowed names Use goofy parameter names:
Or m_ to preceded names of member variables:


Download ppt "Separating Definition & Implementation"

Similar presentations


Ads by Google