Top Banner
Separating Definition & Implementation Headers and Comments
22

Separating Definition & Implementation Headers and Comments.

Jan 04, 2016

Download

Documents

Tabitha Sharp
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Separating Definition & Implementation Headers and Comments.

Separating Definition & Implementation

Headers and Comments

Page 2: Separating Definition & Implementation Headers and Comments.

Goal

• Modular abstractions we can use to build complex systems

Page 3: Separating Definition & Implementation Headers and Comments.

Not Modular or Abstract

Page 4: Separating Definition & Implementation Headers and Comments.

Function Declaration

• Function Declarationreturn type name(parameter list);

Page 5: Separating Definition & Implementation Headers and Comments.

Function Declaration

• Parameter names optional– Strongly encouraged – order matters!

Page 6: Separating Definition & Implementation Headers and Comments.

Implementation

• Functions need to be part of class to work with properties

• Naked function is global – not part of any class

Page 7: Separating Definition & Implementation Headers and Comments.

Scope Resolution

• :: Scope resolution operator

This::That"That is a part of This"

Page 8: Separating Definition & Implementation Headers and Comments.

Scope Resolution

• :: Scope resolution operator

double Circle::getArea()…"getArea is a part of the Circle class"

Page 9: Separating Definition & Implementation Headers and Comments.

Modules

• Copy & Paste is not modular or scalable

• Need mechanism to import existing classes/code

Page 10: Separating Definition & Implementation Headers and Comments.

The Process

• Build:– Preprocess– Compile– Link

Run:– Load

Page 11: Separating Definition & Implementation Headers and Comments.

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

Page 12: Separating Definition & Implementation Headers and Comments.

Linking

• Linking combines separate object files into one executable

• Everything better exist– Better only have one copy

Page 13: Separating Definition & Implementation Headers and Comments.

Finding headers

• Include with < > looks in compiler defined directories

• Include with " " looks in your project, then regular directories

Page 14: Separating Definition & Implementation Headers and Comments.

Guards

• Should not have multiple declarations of same class/function/etc…

• Easy to do by accident with headersInclude A and B, B also includes A

Page 15: Separating Definition & Implementation Headers and Comments.

Guards

• Preprocessor guard:– #ifndef : if this symbol not defined, read until

#endif– #define : define a symbol– CIRCLE_H made up

should be unique

Page 16: Separating Definition & Implementation Headers and Comments.

.h File

• Class declaration goes in .h file– Clients include .h

Page 17: Separating Definition & Implementation Headers and Comments.

.cpp File

• Function implementation must happen once– Dedicated .cpp file• Include .h file and

other headers• NO MAIN!!!

Page 18: Separating Definition & Implementation Headers and Comments.

Header Rules

.h 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

Page 19: Separating Definition & Implementation Headers and Comments.

Naming Conventions

• C++ doesn't have one standardized convention• I encourage– Class names : CapitalizeEachWordclass Person, class BankAccount

– File names : .h and .cpp match and match class namePerson.h, Person.cpp

– Variable names : camelCasePerson bob;BankAccount accountOne;

Page 20: Separating Definition & Implementation Headers and Comments.

Naming Members

• State (member variables) are (usually) nouns– int hour– string address– int numberOfAuthors

• Behavior (member functions) are (usually) verbs– void printTime()– int getHour()

Page 21: Separating Definition & Implementation Headers and Comments.

Shadowing

• Watch out for shadowed names

Page 22: Separating Definition & Implementation Headers and Comments.

Shadowing

• Watch out for shadowed names– Goofy parameter names:

– m_ to preceded names of member variables: