SlideShare a Scribd company logo
1 of 25
The Structure of a C++
Program
Outline
1. Separate Compilation
2. The # Preprocessor
3. Declarations and Definitions
4. Organizing Decls & Defs into Files
5. Project Directories & Libraries
1. Separate Compilation
 C++ programs can range from a handful
of statements to hundreds of thousands
 May be written by one person or by a
team
Single File Programs
 OK for small programs (CS150)
 But with large programs:
– compilation would take
minutes, hours, maybe days
 might break compiler
– Team members would
interfere with one another’s
work.
 “Are you still editing that file?
You’ve had it all afternoon.”
 “What do you mean you’re
saving changes to the file?
I’ve been editing it for the
last 45 minutes!”
Multiple File C++ Programs
 Team members can work in parallel on
separate files
 Files are compiled separately
– each individual compilation is fast
 Separately compiled code is linked to
produce the executable
– linking is much faster than compilation
Separate Compilation
 Each file of source code
– programming language text
 is compiled to produce a
file of object code
– binary code, almost
executable
– exact addresses of
variables and functions not
known, represented by
symbols
 All object code files linked
to produce the executable
– Linking mainly consists of
replacing symbols by real
addresses.
A.cpp
variables: a
functions: fa
calls: fb(a)
B.cpp
variables:
functions: fb
calls: fb(1)
C.cpp
variables: b
functions: main
calls: fa(a,b), fb(a), fb(b)
A.o
a: x1000
fa: x12A0
calls: fb(x1000)
B.o
fb: x2000
uses: x2000(1)
C.o
b: x3000
main: x3400
calls: fa(a,x3000), fb(a),
fb(x3000)
foo.exe
x2000(x1000)
x2000(1)
x12a0(X1000,X3000),
X2000(X1000), X2000(X3000)
COMPILING
LINKING
Source
Code
Object
Code
Executable
A.cpp
variables: a
functions: fa
calls: fb(a)
B.cpp
variables:
functions: fb
calls: fb(1)
C.cpp
variables: b
functions: main
calls: fa(a,b), fb(a), fb(b)
A.o
a: x1000
fa: x12A0
calls: fb(x1000)
B.o
fb: x2000
uses: x2000(1)
C.o
b: x3000
main: x3400
calls: fa(a,x3000), fb(a),
fb(x3000)
foo.exe
x2000(x1000)
x2000(1)
x12a0(X1000,X3000),
X2000(X1000), X2000(X3000)
COMPILING
LINKING
Source
Code
Object
Code
Executable
Large Projects
On large projects with hundreds to
thousands of files
 Typically only a few files are changed on
any one day
 Often only the changed files need to be
recompiled
 Then link the changed and unchanged
object code
2. The # Preprocessor
 The preprocessor runs before the compiler
proper
– modifies the source code
– processes preprocessor instructions
 lines beginning with #
– strips out comments
Processor Instructions
 #include
– insert a file
 #define
– define a macro
 #ifdef, #ifndef, #endif
– check to see if a macro has been defined
#include
 Inserts a file or header into the current
source code
 Two versions
– #insert <headerName>
 inserts a system header file from a location defined
when the compiler was installed
– #insert “fileName”
 inserts a file from the current directory
#include Example
 Suppose we have three files: A.h, B.h, C.cpp
 We ask the compiler to only run the
preprocessor and save the result:
g++ -E C.cpp > C.i
 The result is file C.i
– Note the presence of content from all three files
 includes markers telling where the content came from
#include Example 2
 In real programs, most of the code
actually seen by the compiler may come
from #include’s
– helloWorld.cpp
– helloWorld.i
#define
 Used to define macros (symbols that the
preprocessor will later substitute for)
– Sometimes used to supply constants
#define VersionNumber "1.0Beta1"
int main() {
cout << "Running version “
<< VersionNumber
<< endl;
 Much more elaborate macros are possible,
including ones with parameters
#ifdef, #ifndef, #endif
 Used to select code based upon whether a
macro has been defined:
#ifdef __GNUG__
/* Compiler is gcc/g++ */
#endif
#ifdef _MSC_VER
/* Compiler is Microsoft Visual C++
*/
#endif
#if, #define, and #include
 All of these macros are used to reduce the
amount of code seen by the actual compiler
 Suppose we have three files: A2.h, B2.h, C2.cpp
 We ask the compiler to only run the
preprocessor and save the result:
g++ -E C2.cpp > C2.i
 The result is file C2.i
– Note that the code from A2.h is included only once
– Imagine now, if that were iostream instead of A2.h
3. Declarations and Definitions
 Some of the most common error
messages are
– … is undeclared
– … is undefined
– … is defined multiple times
 Fixing these requires that you understand
the difference between declarations and
definitions
– and how they relate to the program structure
Declarations
 A declaration in C++
– introduces a name for something
– tells what “kind” of thing it is
– gives programmers enough information to use
it
Definitions
 A definition in C++
– introduces or repeats a name for something
– tells what “kind” of thing it is
– tells what value it has and/or how it works
– gives the compiler enough information to
generate this and assign it an address
General Rules for Decls & Defs
 All definitions are also declarations.
– But not vice-versa
 A name must be declared before you can write
any code that uses it.
 A name can be declared any number of times,
as long as the declarations are identical.
 A name must be defined exactly once,
somewhere within all the separately compiled
files making up a program.
D&D: Variables
 These are definitions of variables:
int x;
string s = “abc”;
MyFavoriteDataType mfdt (0);
 These are declarations:
extern int x;
extern string s;
extern MyFavoriteDataType mfdt;
D&D: Functions
 Declaration:
int myFunction (int x, int y);
 Definition
int myFunction (int x, int y)
{
return x + y;
}
 The declaration provides only the header.
the definition adds the body.
D&D: Data Types
 Data types in C++ are declared, but never
defined.
 These are declarations:
typedef float Weight;
typedef string* StringPointer;
enum Colors {red, blue, green};
 Later we will look at these type declarations
struct S { … };
class C { … };
4. Organizing Decls & Defs into
Files
5. Project Directories & Libraries

More Related Content

Similar to cppProgramStructure.ppt

CS8251_QB_answers.pdf
CS8251_QB_answers.pdfCS8251_QB_answers.pdf
CS8251_QB_answers.pdfvino108206
 
Preprocessor directives in c language
Preprocessor directives in c languagePreprocessor directives in c language
Preprocessor directives in c languagetanmaymodi4
 
Preprocessor directives in c laguage
Preprocessor directives in c laguagePreprocessor directives in c laguage
Preprocessor directives in c laguageTanmay Modi
 
C++ unit-1-part-6
C++ unit-1-part-6C++ unit-1-part-6
C++ unit-1-part-6Jadavsejal
 
LOSS_C11- Programming Linux 20221006.pdf
LOSS_C11- Programming Linux 20221006.pdfLOSS_C11- Programming Linux 20221006.pdf
LOSS_C11- Programming Linux 20221006.pdfThninh2
 
Introduction to cpp language and all the required information relating to it
Introduction to cpp language and all the required information relating to itIntroduction to cpp language and all the required information relating to it
Introduction to cpp language and all the required information relating to itPushkarNiroula1
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programmingTejaswiB4
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programmingTejaswiB4
 
Basics of C Lecture 2[16097].pptx
Basics of C Lecture 2[16097].pptxBasics of C Lecture 2[16097].pptx
Basics of C Lecture 2[16097].pptxCoolGamer16
 
Introduction to C Language (By: Shujaat Abbas)
Introduction to C Language (By: Shujaat Abbas)Introduction to C Language (By: Shujaat Abbas)
Introduction to C Language (By: Shujaat Abbas)Shujaat Abbas
 

Similar to cppProgramStructure.ppt (20)

Prog1-L1.pdf
Prog1-L1.pdfProg1-L1.pdf
Prog1-L1.pdf
 
Cp week _2.
Cp week _2.Cp week _2.
Cp week _2.
 
INTRODUCTION TO C LANGUAGE.pptx
INTRODUCTION TO C LANGUAGE.pptxINTRODUCTION TO C LANGUAGE.pptx
INTRODUCTION TO C LANGUAGE.pptx
 
CS8251_QB_answers.pdf
CS8251_QB_answers.pdfCS8251_QB_answers.pdf
CS8251_QB_answers.pdf
 
Preprocessor directives in c language
Preprocessor directives in c languagePreprocessor directives in c language
Preprocessor directives in c language
 
Preprocessor directives in c laguage
Preprocessor directives in c laguagePreprocessor directives in c laguage
Preprocessor directives in c laguage
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
C++ unit-1-part-6
C++ unit-1-part-6C++ unit-1-part-6
C++ unit-1-part-6
 
C PROGRAMMING
C PROGRAMMINGC PROGRAMMING
C PROGRAMMING
 
LOSS_C11- Programming Linux 20221006.pdf
LOSS_C11- Programming Linux 20221006.pdfLOSS_C11- Programming Linux 20221006.pdf
LOSS_C11- Programming Linux 20221006.pdf
 
Introduction to cpp language and all the required information relating to it
Introduction to cpp language and all the required information relating to itIntroduction to cpp language and all the required information relating to it
Introduction to cpp language and all the required information relating to it
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
 
C intro
C introC intro
C intro
 
Presentation 2.ppt
Presentation 2.pptPresentation 2.ppt
Presentation 2.ppt
 
Basics of C Lecture 2[16097].pptx
Basics of C Lecture 2[16097].pptxBasics of C Lecture 2[16097].pptx
Basics of C Lecture 2[16097].pptx
 
Introduction to C Language (By: Shujaat Abbas)
Introduction to C Language (By: Shujaat Abbas)Introduction to C Language (By: Shujaat Abbas)
Introduction to C Language (By: Shujaat Abbas)
 
Programming in C++
Programming in C++Programming in C++
Programming in C++
 
C++ basics
C++ basicsC++ basics
C++ basics
 
A05
A05A05
A05
 

Recently uploaded

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 

Recently uploaded (20)

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 

cppProgramStructure.ppt

  • 1. The Structure of a C++ Program
  • 2. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into Files 5. Project Directories & Libraries
  • 3. 1. Separate Compilation  C++ programs can range from a handful of statements to hundreds of thousands  May be written by one person or by a team
  • 4. Single File Programs  OK for small programs (CS150)  But with large programs: – compilation would take minutes, hours, maybe days  might break compiler – Team members would interfere with one another’s work.  “Are you still editing that file? You’ve had it all afternoon.”  “What do you mean you’re saving changes to the file? I’ve been editing it for the last 45 minutes!”
  • 5. Multiple File C++ Programs  Team members can work in parallel on separate files  Files are compiled separately – each individual compilation is fast  Separately compiled code is linked to produce the executable – linking is much faster than compilation
  • 6. Separate Compilation  Each file of source code – programming language text  is compiled to produce a file of object code – binary code, almost executable – exact addresses of variables and functions not known, represented by symbols  All object code files linked to produce the executable – Linking mainly consists of replacing symbols by real addresses. A.cpp variables: a functions: fa calls: fb(a) B.cpp variables: functions: fb calls: fb(1) C.cpp variables: b functions: main calls: fa(a,b), fb(a), fb(b) A.o a: x1000 fa: x12A0 calls: fb(x1000) B.o fb: x2000 uses: x2000(1) C.o b: x3000 main: x3400 calls: fa(a,x3000), fb(a), fb(x3000) foo.exe x2000(x1000) x2000(1) x12a0(X1000,X3000), X2000(X1000), X2000(X3000) COMPILING LINKING Source Code Object Code Executable
  • 7. A.cpp variables: a functions: fa calls: fb(a) B.cpp variables: functions: fb calls: fb(1) C.cpp variables: b functions: main calls: fa(a,b), fb(a), fb(b) A.o a: x1000 fa: x12A0 calls: fb(x1000) B.o fb: x2000 uses: x2000(1) C.o b: x3000 main: x3400 calls: fa(a,x3000), fb(a), fb(x3000) foo.exe x2000(x1000) x2000(1) x12a0(X1000,X3000), X2000(X1000), X2000(X3000) COMPILING LINKING Source Code Object Code Executable
  • 8. Large Projects On large projects with hundreds to thousands of files  Typically only a few files are changed on any one day  Often only the changed files need to be recompiled  Then link the changed and unchanged object code
  • 9. 2. The # Preprocessor  The preprocessor runs before the compiler proper – modifies the source code – processes preprocessor instructions  lines beginning with # – strips out comments
  • 10. Processor Instructions  #include – insert a file  #define – define a macro  #ifdef, #ifndef, #endif – check to see if a macro has been defined
  • 11. #include  Inserts a file or header into the current source code  Two versions – #insert <headerName>  inserts a system header file from a location defined when the compiler was installed – #insert “fileName”  inserts a file from the current directory
  • 12. #include Example  Suppose we have three files: A.h, B.h, C.cpp  We ask the compiler to only run the preprocessor and save the result: g++ -E C.cpp > C.i  The result is file C.i – Note the presence of content from all three files  includes markers telling where the content came from
  • 13. #include Example 2  In real programs, most of the code actually seen by the compiler may come from #include’s – helloWorld.cpp – helloWorld.i
  • 14. #define  Used to define macros (symbols that the preprocessor will later substitute for) – Sometimes used to supply constants #define VersionNumber "1.0Beta1" int main() { cout << "Running version “ << VersionNumber << endl;  Much more elaborate macros are possible, including ones with parameters
  • 15. #ifdef, #ifndef, #endif  Used to select code based upon whether a macro has been defined: #ifdef __GNUG__ /* Compiler is gcc/g++ */ #endif #ifdef _MSC_VER /* Compiler is Microsoft Visual C++ */ #endif
  • 16. #if, #define, and #include  All of these macros are used to reduce the amount of code seen by the actual compiler  Suppose we have three files: A2.h, B2.h, C2.cpp  We ask the compiler to only run the preprocessor and save the result: g++ -E C2.cpp > C2.i  The result is file C2.i – Note that the code from A2.h is included only once – Imagine now, if that were iostream instead of A2.h
  • 17. 3. Declarations and Definitions  Some of the most common error messages are – … is undeclared – … is undefined – … is defined multiple times  Fixing these requires that you understand the difference between declarations and definitions – and how they relate to the program structure
  • 18. Declarations  A declaration in C++ – introduces a name for something – tells what “kind” of thing it is – gives programmers enough information to use it
  • 19. Definitions  A definition in C++ – introduces or repeats a name for something – tells what “kind” of thing it is – tells what value it has and/or how it works – gives the compiler enough information to generate this and assign it an address
  • 20. General Rules for Decls & Defs  All definitions are also declarations. – But not vice-versa  A name must be declared before you can write any code that uses it.  A name can be declared any number of times, as long as the declarations are identical.  A name must be defined exactly once, somewhere within all the separately compiled files making up a program.
  • 21. D&D: Variables  These are definitions of variables: int x; string s = “abc”; MyFavoriteDataType mfdt (0);  These are declarations: extern int x; extern string s; extern MyFavoriteDataType mfdt;
  • 22. D&D: Functions  Declaration: int myFunction (int x, int y);  Definition int myFunction (int x, int y) { return x + y; }  The declaration provides only the header. the definition adds the body.
  • 23. D&D: Data Types  Data types in C++ are declared, but never defined.  These are declarations: typedef float Weight; typedef string* StringPointer; enum Colors {red, blue, green};  Later we will look at these type declarations struct S { … }; class C { … };
  • 24. 4. Organizing Decls & Defs into Files
  • 25. 5. Project Directories & Libraries