Learn C++
Design
Scope
Header Guards
Approaches to Programming Design:
1. Procedural Design Approach
Fortran, Assembly C
Top-Down Desing, Bottom-Up Implementation
can solve any problem with this approach
build a program by combining algorithms
algorithms work on data passed between them from function to function
input/output
how to get from input to output
break complex tasks into smaller pieces
design functions to achieve these pieces
issues: scaling
2. Object-Oriented Approach
C++, Java, Python
object: entity using data and behavior, may comprise other objects, has properties, can do things and have things done to it
program consists of interacting objects
design: identify objects and determine how objects need to interact
determine data
determine represention of data as object(s)
define interactions between objects
3. Functional Approach
Haskel, Clojure, F#
Back to Top
Projects
Files
Unit Testing
Headers (.h)
doctest.h
main.h
Source (.cpp)
main.cpp
tester.cpp
Back to Top
Files
Separating Class Definition from Class Implementation
header file (.h) contains the class definition (simply list the data fields, constructor prototypes, function prototypes)
source file (.cpp) contains the class implementation of the constructors and functions
both the .h and .cpp should have the same file name
preprocessor directives
to prevent a header file from being included multiple times:
#indef // @ top
#define // @ top
#endif // @ bottom
Back to Top
Constructors
a special kind of function which initializes an object
no return type
class naming nomenclature
it must initialize all member variables
automatically called when object is declared
default constructor does nothing and has no arguments
overloading a function is providing multiple versions of the function with different Parameters
may overload a constructor
parameters in the function call determine which function is used
// use no-arg constructor
Circle c1;
// use one-arg constructor
Circle c2(5.5);
// use n-arg constructor
Circle c3(5.5, 10); // n = 2
avoid shadowed names, instead i.e. use m_ to proceed names of member variables
example:
private:
double m_radius;
Circle::Circle(double radius) {
m_radius = radius;
}
Back to Top
Syntax
comments
// single line
/* multi-line
/** machine readable
typedef
namespaces
using namespace std;
Namespaces
Typedefs
preprocessor directives
Intro to
Header Files
anything that starts with a #
#include <>iostream
#ifndef // if not defined
#define
#endif // indicates end of header file
Back to Top
Data Structures
Linear
Tree
Graph
Non-Linear
Static
Array
Dynamic
Queue
Stack
Linked List
Back to Top
Loops
while loop
do while loop
for loop
for each loop
Back to Top
Operators
stream insertion operators
cout <<
stream extraction operators
cin >>
Back to Top
Commands
Commands
Windows
Mac/Linux
mkdir create a new directory
cd move into directory (make current)
.. move up one directory
ls list items in directory
compile a main program from the command line:
g++ Circle.h Circle.cpp TestCircleWithHeader.cpp -o Main
Back to Top