#include guards Practical session #2 Software Engineering 094219.

Slides:



Advertisements
Similar presentations
1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
Advertisements

C Language.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Kernighan/Ritchie: Kelley/Pohl:
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
C++ Functions CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
1 Abstract Data Type (ADT) a data type whose properties (domain and operations) are specified (what) independently of any particular implementation (how)
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Review of C++ Programming Part II Sheng-Fang Huang.
Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.
Copyright 2001 Oxford Consulting, Ltd1 January Storage Classes, Scope and Linkage Overview Focus is on the structure of a C++ program with –Multiple.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
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.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
VARIABLES AND DATA TYPES Chapter2:part1 1. Objectives: By the end of this section you should: Understand what the variables are and why they are used.
An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
System Programming Practical Session 7 C++ Memory Handling.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
Chapter 2. Variable and Data type
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
CSIS 123A Lecture 7 Static variables, destructors, & namespaces.
Advanced BioPSE NCRR Programming Conventions and Standards J. Davison de St. Germain Chief Software Engineer SCI Institute December 2003 J.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance.
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
1 Another Example: Complex Class #ifndef _Complex_H #define _Complex_H class Complex { float re, im; // by default private public: Complex(float x = 0,
C++ Functions A bit of review (things we’ve covered so far)
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 2 Functions September.
Java Programming Language Lecture27- An Introduction.
Object Access m1.write(44); m2.write(m2.read() +1); std::cout
Pointer to an Object Can define a pointer to an object:
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
IS Program Design and Software Tools Introduction to C++ Programming
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Introduction to C++ Systems Programming.
6 Chapter Functions.
Seoul National University
Classes.
C++ Compilation Model C++ is a compiled language
Arrays Arrays A few types Structures of related data items
What Is? function predefined, programmer-defined
Seoul National University
C Programming Lecture-17 Storage Classes
Presentation transcript:

#include guards Practical session #2 Software Engineering

Modular programming

Header file = interface  Declarations of functions and classes.  Definitions of classes, constants and types.  Inline functions. CPP file = implementation  Definitions of functions (class members or regular functions ).  Initialization of static class members.  In order to use the identifiers from header file – it must be included.

#include  Pre-compiler command that inserts the included file in place of #include.  #include - standard library  #include “filename.h” - your own header file

#include - rule 1 typedef std::vector int_vector; //Needs #include but has no //such include File1.h #include ….. File2.h #include “File2.h” #include “File1.h” Usage1.cpp OK #include “File1.h” #include “File2.h” Usage2.cpp Compilation error! Every header file must have all #includes it requires !!! Otherwise the order of includes will matter and the files are not independent!

#include - rule 1 typedef my_int* int_vector; //Needs my_int but has no //such include File1.h typedef int my_int; ….. File2.h #include “File2.h” #include “File1.h” Usage1.cpp OK Compilation error! #include “File1.h” #include “File2.h” Usage2.cpp Every header file must have all #includes it requires !!! Otherwise the order of includes will matter and the files are not independent!

#include - a problem #include “File1.h” #include “File3.h” //Needs code C and B Usage1.cpp Compilation error! Redefinition of class A “File2.h” is included twice ! => class A is defined twice ! class A { }; //More code File2.h #include “File2.h” //Code C File1.h #include “File2.h” //Code B File3.h

#include - a solution #include “File1.h” #include “File3.h” //Needs code C and B Usage1.cpp OK #ifndef FILE2_H #define FILE2_H class A { }; //More code #endif File2.h #include “File2.h” //Code C File1.h #include “File2.h” //Code B File3.h Include guards Every header file should have include guards ! To avoid double or cyclic inclusion! These files should have include guards too !

C++ variables and types

Agenda  Variables and types  Reference 10

Variable  Variable is a place (a piece of memory) for data storage, in C++: variable=object.  Variable has a name = identifier.  Variable has a type, that determines:  The size in memory.  The values that can be stored in it.  The operations that can be applied to it.  Variables should be initialized (good practice ). Initialization syntax:  type identifier (initial_value) ; //example: int k(5);  type identifier = initial_value; //example: int k = 5; 11 The same Example: double num ( 0.0 );

Types in C Primitive types.  Examples:  bool, int, double, char… 2. Compound (or composite ) types:  Any type that is not primitive.  Any type that made up of primitive types or other compound types.  Examples:  Arrays.,  Pointers, references,  Classes,  And there are more… C++ is a statically typed language each variable has to have a type before usage and this type cannot be changed!

Primitive built-in types Minimum sizeMeaningType NABooleanbool 8 bitsCharacterchar 16 bitsWide characterwchar_t 16 bitsShort integershort 16 bitsIntegerint 32 bitsLong integerlong 32 bits (6 p)Single-precision floating pointfloat 64 bits (10 p)Double-precision floating pointdouble 128 bits (10 p)Extended-precision floating pointlong double 13 We can find out the actual size of a variable by using sizeof operator

Scope and local variable  The scope (or simply the “{ }” braces) of an identifier, is the part of the program over which the identifier can be seen and used.  Local variable is a variable which defined inside a function. Local variable can be accessed only from the scope where it was defined and cleared automatically at the end of the scope.  Example (with compiler errors) : 1. void average() 2. { { 3. for ( int i =0 ; i < 5 ; i++ ) { 4. int x, sum = 0 ; 5. std::cout << “Enter "<< i+1 <<"'th integer:“ << std::endl; 6. std::cin >> x ; 7. sum += x ; } 8. } 9. std::cout<<" The average: “<< sum / i <<std::endl; 10. } What is the error and how can we fix it? 14

 Global variable defined in the main scope – outside the scope of all functions/classes.  Examples : 1. #include 2. //Global scope 3. int x = 20; 4. void incX(){//Local scope 5. // int x = 10 ; 6. x ++; 7. } 8. int main(){ 9. std::cout<<" Global x = "<< x <<std::endl; 10. incX(); 11. std::cout<<" Global x + 1 = "<< x <<std::endl; 12. system("pause"); 13. return 0 ; 14. } Global variable What will be the result of adding this code line (5)? 15

Constant ( const) variable Constant ( const ) variable must be initialized at its definition point and cannot be changed after its definition. Example : 1. void main(){ 2. const int cInt = 5 ; 3. const double cDoub ; //ERROR 4. cInt ++ ; //ERROR 5. cDoub +=.3 ; // ERROR 6. std::cout << cInt << std::endl ; 7. system("pause"); 8. } 16

Agenda  Variables and types  Reference 17

Reference variable  Reference type can be thought as alternative name for a variable.  Reference must be initialized at definition time.  Example : 1. long index = 10 ; 2. long &refIndex = index ; 3. std::cout<< ++refIndex << endl; 4. std::cout<< index << endl; 5. long &refIndex2 ; //ERROR 6. long &refIndex3 = 10 ; //ERROR  Reference can’t be rebind after definition. refIndex is a reference to index forever. Whenever we use refIndex (after we created it) we access the object referenced. 18

const reference Reference can be const: you cannot change the object via the const reference. long longNum ; const long& longRef = longNum; //correct longRef = 5; //ERROR  The opposite is not correct: const object can have only const references or pointers to it const long longNum (5); long& longRef = longNum; // ERROR. //Because we could change const object //via non const reference! 19