Separating Definition & Implementation

Slides:



Advertisements
Similar presentations
Introduction to C++ An object-oriented language Unit - 01.
Advertisements

Chapter 11 Separate Compilation and Namespaces. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Separate Compilation.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Chapter 12 Separate Compilation and Namespaces. Abstract Data Type (ADT) ADT: A data type consisting of data and their behavior. The abstraction is that.
CSE 303 Lecture 16 Multi-file (larger) programs
1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the.
Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12A Separate Compilation and Namespaces For classes this time.
Specification and Implementation Separating the specification from implementation makes it easier to modify programs. Changes in the class’s implementation.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
1 Abstract Data Type (ADT) a data type whose properties (domain and operations) are specified (what) independently of any particular implementation (how)
Java Class Syntax CSIS 3701: Advanced Object Oriented Programming.
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
Multiple Files. Monolithic vs Modular  one file before  system includes  main driver function  prototypes  function.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Separating Definition & Implementation Headers and Comments.
CS 213 Fall 1998 Namespaces Inline functions Preprocessing Compiling Name mangling Linking.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
ECE122 Feb. 22, Any question on Vehicle sample code?
Compilation & Linking Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens The Preprocessor When a C compiler is invoked, the.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
MODULAR ORGANIZATION Prepared by MMD, Edited by MSY1.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
15. WRITING LARGE PROGRAMS. Source Files A program may be divided into any number of source files. Source files have the extension.c by convention. Source.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Separate Compilation Bryce Boe 2013/10/09 CS24, Fall 2013.
Chapter 9 Separate Compilation and Namespaces. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Separate Compilation (9.1)
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation and Namespaces.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Separate Compilation and Namespaces.
C code organization CSE 2451 Rong Shi. Topics C code organization Linking Header files Makefiles.
Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Object Access m1.write(44); m2.write(m2.read() +1); std::cout
Programs – Preprocessing, Compilation and Linking
TK1924 Program Design & Problem Solving Session 2011/2012
What Is? function predefined, programmer-defined
Chapter 9 More on Objects and Classes
Compilation and Debugging
Compilation and Debugging
Separate Compilation and Namespaces
A First C++ Class – a Circle
Chapter 13 - The Preprocessor
Functions Separate Compilation
Chapter 5 Function Basics
The Preprocessor Based on Chapter 1 in C++ for Java Programmers by Weiss When a C compiler is invoked, the first thing that happens is that the code is.
Pre-processor Directives
Separate Compilation and Namespaces
Structures putting data together.
Chapter 9 Objects and Classes
C Preprocessor(CPP).
Code Organization Classes
Introduction to Classes and Objects
Comments, Prototypes, Headers & Multiple Source Files
Structures putting data together.
Code Organization CSCE 121 J. Michael Moore.
Classes.
C++ Compilation Model C++ is a compiled language
C Preprocessor Seema Chandak.
C Programming Language
What Is? function predefined, programmer-defined
Chapter 11 Class Inheritance
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Executable program (p1)
Code Organization Classes
Lecture 8 Object Oriented Programming (OOP)
SPL – PS3 C++ Classes.
Presentation transcript:

Separating Definition & Implementation .h and .cpp explained

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

The Process Build: Preprocess Compile Link Run: Load

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

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

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

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

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

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

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

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

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;

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

Shadowing Watch out for shadowed names

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