Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Part 1:

Slides:



Advertisements
Similar presentations
Chapter 11 Separate Compilation and Namespaces. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Separate Compilation.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Quality of a Class Abstraction: Coupling & Cohesion Michael L. Collard, Ph.D. Department of Computer Science Kent State University.
CSE 303 Lecture 16 Multi-file (larger) programs
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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 © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
1 Lab Session-XIV CSIT121 Spring 2002 b Namespaces b First Class Travel b Lab Exercise 14 (Demo) b Lab Exercise b Practice Problem.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Guide To UNIX Using Linux Third Edition
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
1 Friends and Namespace COSC 1567 C++ Programming Lecture 6.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMING PRACTICES API documentation.
Chapter 9: Coupling & Cohesion Omar Meqdadi SE 273 Lecture 9 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Chapter 13. Binary Files In binary files we do not need to format data File streams include two member functions specifically designed to input and output.
chap13 Chapter 13 Programming in the Large.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Separate Compilation. A key concept in programming  Two kinds of languages, compilation (C, Pascal, …) and interpretation (Lisp, …, Matlab, Phython,
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Concordia University Department of Computer Science and Software Engineering COMP345 Advanced Program Design with C++ Lecture 1: C++ basics 1.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Templates.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
CPS120: Introduction to Computer Science Functions.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
CPS120: Introduction to Computer Science Lecture 14 Functions.
1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Part 9:
Chapter 11 Separate Compilation and Namespaces. Learning Objectives Separate Compilation –Encapsulation reviewed –Header and implementation files Namespaces.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Static arrays.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Software Engineering and Object-Oriented Design Topics: Solutions Modules Key Programming Issues Development Methods Object-Oriented Principles.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-1 Concordia University Department of Computer Science and Software Engineering COMP345.
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.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.
Chapter 3 Part II. 3.8 Placing a Class in a Separate File for Reusability.cpp file is known as a source-code file. Header files ◦ Separate files in which.
Chapter 9: Coupling & Cohesion Omar Meqdadi SE 273 Lecture 9 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Appendix 1 - Packages Jim Fawcett copyright (c)
Chapter 12 Classes and Abstraction
Introduction to C++ Systems Programming.
Advanced Program Design with C++
Separate Compilation and Namespaces
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Separate Compilation and Namespaces
Introduction to Classes and Objects
Classes, Constructors, etc., in C++
C++ Compilation Model C++ is a compiled language
What Is? function predefined, programmer-defined
SPL – PS1 Introduction to C++.
Presentation transcript:

Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Part 1: Program Structure Joey Paquet, COMP Advanced Program Design with C++

Concordia University Department of Computer Science and Software Engineering Click to edit Master title style PROGRAM STRUCTURE free functions and main function compilation unit header file, implementation file namespaces Joey Paquet, COMP Advanced Program Design with C++

Concordia University Department of Computer Science and Software Engineering C++ is a superset of C Main function is the program driver Free functions Data structures A C program is a valid C++ program Classes encapsulate other functions: Object-oriented The main function is a free function, not a class member Structure of C++ programs Joey Paquet, COMP Advanced Program Design with C++

Concordia University Department of Computer Science and Software Engineering Simple C++ program Joey Paquet, COMP Advanced Program Design with C++

Concordia University Department of Computer Science and Software Engineering Only simplistic programs are self-contained into one file C++ is a language that specifies programming structures and includes only basic data structures and operators Most programs require the use of libraries Complier will compile the library’s code and the program’s code, then the linker will link them Program structure: multiple files programs Joey Paquet, COMP Advanced Program Design with C++

Concordia University Department of Computer Science and Software Engineering Aside from having to use libraries, large programs need to be split into different files for various reasons: Speed up compilation: Upon changes to the code, the compiler will recompile only the files that had a change. Increase organization, decrease browsing time: Physically splitting your code along logical lines will make it easier to browse through the code to locate classes, functions, etc. Facilitate code reuse: Modular physical design allows for grouping related entities and separating them from less related ones. Each group can then be logically designed to be reusable across different projects. Reused code can be fixed, fixing all projects that use it. Split coding responsibilities among programmers: For really large projects, several programmers are involved. The larger are the program files, the more likely it is that several programmers are changing the same file simultaneously. Program structure: multiple files programs Joey Paquet, COMP Advanced Program Design with C++

Concordia University Department of Computer Science and Software Engineering In C++, a compilation unit is a file A file may contain several functions, data structures, or classes (unlike Java) Each compilation unit is compiled individually into an object file The linker then attempts to resolve cross-references between the object files to form the unique executable file Program structure: compilation unit Joey Paquet, COMP Advanced Program Design with C++

Concordia University Department of Computer Science and Software Engineering Program Parts, or components Kept in separate files Compiled separately and linked together before the program runs Much more freedom to define what is a “part”, as it can be composed of a group of functions, data structures and classes In C++, there is a separation between the interface and the implementation of a component Each component is a group of highly cohesive and highly coupled elements Program structure: compilation units as program parts Joey Paquet, COMP Advanced Program Design with C++

Concordia University Department of Computer Science and Software Engineering Cohesion: The degree to which the elements of a module belong together in the achievement of a common goal. Cohesion is increased if: The functionalities embedded in a module have much in common. The composing elements carry out a small number of related activities, by avoiding coarsely grained and/or unrelated sets of data. Advantages of high cohesion: Increased understandability of modules (simpler, fewer operations). Increased maintainability, because changes in one module require fewer changes in other modules. Increased reusability, because application developers will find the component they need more easily among the cohesive set of operations provided by the module. Program structure: cohesion Joey Paquet, COMP Advanced Program Design with C++

Concordia University Department of Computer Science and Software Engineering Coupling: the degree to which an element relies on other elements for its operation Coupling is not a desired quality, but is a side-effect of defining different elements to carry a common task Much related to cohesion: good modules exhibit high cohesion and high coupling Coupling between modules is what really needs to be avoided Disadvantages of high coupling: A change in one module forces a ripple effect of changes in other modules Assembly of modules requires more effort due to the increased inter- module dependency A module might be harder to reuse and/or test because dependent modules must be included Program structure: coupling Joey Paquet, COMP Advanced Program Design with C++

Concordia University Department of Computer Science and Software Engineering Programs may have header files (.h ) and program files (.cpp ) No one-to-one relationship between file and classes as in Java Can have one file that contains many classes Not necessary, can have the whole program in a single file Additional “physical design” consideration Important aspect of C++ program design Program structure: header files and implementation files Joey Paquet, COMP Advanced Program Design with C++

Concordia University Department of Computer Science and Software Engineering Interface/header File (.h file) Contains class declaration with free functions and operators declarations Useful to programmers, as it is an abstract view of a module Separate compilation unit Implementation File (.cpp file) Contains free/member function definitions Separate compilation unit Program structure: header files and implementation files Joey Paquet, COMP Advanced Program Design with C++

Concordia University Department of Computer Science and Software Engineering Header files are intended for providing forward declarations to the compiler Typically, for each x.cpp file, there is a corresponding x.h file Any program file using entities defined in x.cpp will #include “x.h” This way, this program file does not use unresolved identifiers The linker will later make the proper connections on the object files In Java, this is solved by having files named after the single class they contain The Java model is much easier to use, though less flexible than the C++ model Program structure: header files Joey Paquet, COMP Advanced Program Design with C++

Concordia University Department of Computer Science and Software Engineering A module’s interface should always be in its header file Programs that use this module will "include" it. There is different syntax to use if you are including a user-defined module, or an existing library module: #include “mymodule.h” Quotes indicate a user-defined module The compiler will find it in your working directory #include indicate predefined library header file The compiler will find it in the library directories Using different search paths Program structure: header files Joey Paquet, COMP Advanced Program Design with C++

Concordia University Department of Computer Science and Software Engineering A module’s implementation code should be in.cpp file Typically give interface file and implementation file the same name mymodule.h and mymodule.cpp Not enforced, but failure to do so is confusing A module is composed of classes and free functions All the modules’ classes’ member function and free functions are defined here Implementation file must #include the module’s header file, as it contains the module’s classes and data structures declarations.cpp files contain the executable code Function definitions, including main(), free functions, member functions. Program structure: implementation files Joey Paquet, COMP Advanced Program Design with C++

Concordia University Department of Computer Science and Software Engineering Header file: example Joey Paquet, Advanced Program Design with C++ //This is the header file dtime.h. This is the interface for the class DigitalTime. //Values of this type are times of day. The values are input and output in //24 hour notation as in 9:30 for 9:30 AM and 14:45 for 2:45 PM. #ifndef DTIME_H #define DTIME_H #include using namespace std; class DigitalTime { public: DigitalTime(int theHour, int theMinute); DigitalTime( ); getHour( ) const; getMinute( ) const; void advance(int minutesAdded); void advance(int hoursAdded, int minutesAdded); friend bool operator ==(const DigitalTime& time1, const DigitalTime& time2); friend istream& operator >>(istream& ins, DigitalTime& theObject); friend ostream& operator <<(ostream& outs, const DigitalTime& theObject); private: int hour; int minute; static void readHour(int& theHour); static void readMinute(int& theMinute); static int digitToInt(char c); }; #endif //DTIME_H

Concordia University Department of Computer Science and Software Engineering Implementation file: example Joey Paquet, Advanced Program Design with C++ //This is the implementation file: dtime.cpp of the class DigitalTime. //The interface for the class DigitalTime is in the header file dtime.h. #include using namespace std; #include "dtime.h" //Uses iostream and cstdlib: DigitalTime::DigitalTime(int theHour, int theMinute) { if (theHour 24 || theMinute 59){ cout << "Illegal argument to DigitalTime constructor."; exit(1); } else{ hour = theHour; minute = theMinute; } if (hour == 24) hour = 0; } DigitalTime::DigitalTime( ){ hour = 0; minute = 0; } int DigitalTime::getHour( ) const{ return hour; } int DigitalTime::getMinute( ) const{ return minute; } // All other member functions and operators definitions declared in dtime.h should be defined here }

Concordia University Department of Computer Science and Software Engineering Header files are typically included multiple times e.g., class interface included by class implementation and program file Must only be compiled once else, multiply defined names No guarantee which " #include " in which file the compiler might see first Use preprocessor directives Tell compiler to include header file only once Program structure: redundant includes Joey Paquet, COMP Advanced Program Design with C++

Concordia University Department of Computer Science and Software Engineering Header file fname.h structure: #ifndef FNAME_H #define FNAME_H … //Content of header file … #endif FNAME typically name of file for consistency, readability This syntax avoids multiple definitions from compiling the same header file more than once May also use: #pragma once … //Content of header file … Program structure: redundant includes Joey Paquet, COMP Advanced Program Design with C++

Concordia University Department of Computer Science and Software Engineering In fact, this is a specialized use of the conditional compilation preprocessor directive. Conditional compilation: #ifdef x //or ifndef... #else... #endif Can be used to switch between portions of code by switching on/off x, e.g. machine-dependent code. Program structure: redundant includes Joey Paquet, COMP Advanced Program Design with C++

Concordia University Department of Computer Science and Software Engineering Namespace: Collection of name definitions inside of a program, potentially across different files For example, namespace “std” is common in libraries Has all standard library definitions we need #include using namespace std; Includes entire standard library of name definitions #include using std::cin; using std::cout; Can specify just the objects we want Can be more efficient, as it avoids including things we don’t use Program structure: namespaces Joey Paquet, COMP Advanced Program Design with C++

Concordia University Department of Computer Science and Software Engineering Used as a solution to resolve potential name clashes Large programs use many classes and functions As a program re-uses many other files, it increases the possibility of encountering entities that have the same name Namespaces are meant to deal with this A namespace can be turned "on" with the using namespace directive If names might conflict, switch it off But how to “switch it off” after it has been activated? You cannot, but conveniently, the using directive is effective only in the code block in which it is used (see next slide). Use different namespaces in separated code blocks. Though this solution has its limitations. This is one reason why the use of using NS::name directive is advocated over using namespace NS Program structure: namespaces Joey Paquet, COMP Advanced Program Design with C++

Concordia University Department of Computer Science and Software Engineering Given namespaces NS1 and NS2 Both have void function myFunction() defined differently If we want to use either definitions at different places in our program, we may do the following: { using namespace NS1; myFunction(); } { using namespace NS2; myFunction(); } Program structure: namespaces Joey Paquet, COMP Advanced Program Design with C++

Concordia University Department of Computer Science and Software Engineering All code goes in some namespace Unless specified, code belongs to the global namespace No need for using directive Global namespace always available Implied “automatic” using directive But there is no way to “turn it off” Program structure: global namespace Joey Paquet, COMP Advanced Program Design with C++

Concordia University Department of Computer Science and Software Engineering To create a namespace: namespace Space1 { Some_Code } Places names defined in Some_Code into namespace Space1 Can then be made available by : using namespace Name_Space_Name And any of the entities defined (e.g. NSentity) in it can be made available by: using Name_Space_Name::NSentity Program structure: creating a namespace Joey Paquet, COMP Advanced Program Design with C++

Concordia University Department of Computer Science and Software Engineering As seen earlier, header files and implementation files hold different parts of the definition/declaration of the entities in a module. Thus, the same namespace needs to be declared in both In the header file (declarations): namespace Space1 { void greeting(); } In the implementation file (definitions): namespace Space1 { void greeting() { cout << "Hello from namespace Space1.\n"; } } Program structure: creating a namespace across header and implementation files Joey Paquet, COMP Advanced Program Design with C++

Concordia University Department of Computer Science and Software Engineering Can specify where name comes from Use "qualifier" and scope-resolution operator Used if only intend one use (or few) If overused, leads to less readable code NS1::fun1(); Specifies that fun1() comes from namespace NS1 Especially useful for parameters: int getInput(std::istream inputStream); Parameter found in istream ’s std namespace Eliminates need for using directive or declaration Program structure: inline namespace qualification Joey Paquet, COMP Advanced Program Design with C++

Concordia University Department of Computer Science and Software Engineering Program structure: namespaces example Joey Paquet, COMP Advanced Program Design with C++

Concordia University Department of Computer Science and Software Engineering Program structure: namespaces example Joey Paquet, COMP Advanced Program Design with C++

Concordia University Department of Computer Science and Software Engineering Compilation unit defined: A file, along with all files #include d in file Every compilation unit has unnamed namespace Written same way, but with no name All names are then local to compilation unit Use unnamed namespace to keep things "local" Scope of unnamed namespace is compilation unit Not same as global namespace Global namespace: No namespace grouping at all Global scope Unnamed namespace: Has namespace grouping, just no name Local scope Program structure: unnamed namespace Joey Paquet, COMP Advanced Program Design with C++

Concordia University Department of Computer Science and Software Engineering #include using namespace std; namespace { const int i = 4; // these are local to the int variable;// compilation unit } const int x = 1;// this is global int main() { cout << i << x << endl; variable = 100; return 0; } Program structure: unnamed namespace example Joey Paquet, COMP Advanced Program Design with C++

Concordia University Department of Computer Science and Software Engineering #include using namespace std; namespace { const int i = 4; // this is local } int i = 2; // this is global int main() { cout << i << endl; // ERROR, i is ambiguous return 0; } Program structure: global vs. unnamed namespace example Joey Paquet, COMP Advanced Program Design with C++

Concordia University Department of Computer Science and Software Engineering Walter Savitch, Absolute C++ (Chapter 1, 11), Addison-Wesley, Bjarne Stroustrup, The C++ Programming Language (Chapters 2,6,14,15), Addison-Wesley, Y. Daniel Liang, Introduction to Programming with C++ (Chapter 2, 13). References Joey Paquet, COMP Advanced Program Design with C++