Separating Definition & Implementation Headers and Comments.

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.
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.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 15 – Digital Clock Application: Building Your.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Abstract Data Types Using Classes Lecture-5. Abstract Data Types Using Classes Representing abstract data types using C++ We need the following C++ keywords.
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.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
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.
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.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
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.
Program in Multiple Files. l all C++ statements are divided into executable and non-executable l executable - some corresponding machine code is generated.
Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object Access m1.write(44); m2.write(m2.read() +1); std::cout
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
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
Functions Separate Compilation
Introduction to Classes
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.
Introduction to Classes
Chapter 9 Objects and Classes
C Preprocessor(CPP).
Separating Definition & Implementation
Code Organization Classes
Introduction to Classes and Objects
Comments, Prototypes, Headers & Multiple Source Files
Structures putting data together.
Separating Definition & Implementation
Code Organization CSCE 121 J. Michael Moore.
Classes.
C++ Compilation Model C++ is a compiled language
C Programming Language
What Is? function predefined, programmer-defined
Templates Generic Programming.
Chapter 11 Class Inheritance
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Code Organization Classes
Lecture 8 Object Oriented Programming (OOP)
Presentation transcript:

Separating Definition & Implementation Headers and Comments

Goal Modular abstractions we can use to build complex systems

Not Modular or Abstract

Function Declaration return type name(parameter list);

Function Declaration Parameter names optional – Strongly encouraged – order matters!

Implementation Functions need to be part of class to work with properties Naked function is global – not part of any class

Scope Resolution :: Scope resolution operator This::That "That is a part of This"

Scope Resolution :: Scope resolution operator double Circle::getArea()… "getArea is a part of the Circle class"

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 – 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 – Goofy parameter names: – m_ to preceded names of member variables: