EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.

Slides:



Advertisements
Similar presentations
Chapter 7: User-Defined Functions II
Advertisements

CSCI 171 Presentation 11 Pointers. Pointer Basics.
Kernighan/Ritchie: Kelley/Pohl:
Chapter 14: Overloading and Templates
CPSC230 Computers & Programming I Lecture Notes 20 Function 5 Dr. Ming Zhang.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Review of C++ Programming Part II Sheng-Fang Huang.
Introduction to C++ Systems Programming.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
1 Introduction to C++ Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Learners Support Publications Classes and Objects.
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.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction 15.2C A Simple Program: Adding Two Integers.
Fundamentals of C and C++ Programming Simple data structures Pointers.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
STRUCTURE OF A C++ PROGRAM. It is a common practice to organize a program into three separate files. The class declarations are placed in a header file.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
1 Introduction to C++ Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
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.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
EC-111 Algorithms & Computing Lecture #6 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
 2003 Prentice Hall, Inc. All rights reserved Storage Classes Variables have attributes –Have seen name, type, size, value –Storage class How long.
Functions. Predefined Functions C++ comes with libraries of code that can be reused in your programs. The code comes in the form of predefined functions.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Programming Fundamentals Enumerations and Functions.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 2 Functions September.
C++ Programming Michael Griffiths Corporate Information and Computing Services The University of Sheffield
Chapter 15 - C++ As A "Better C"
Procedural and Object-Oriented Programming
Chapter 7: User-Defined Functions II
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.
FUNCTIONS In C++.
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.
Pointers and References
Pointers, Dynamic Data, and Reference Types
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
Variables have attributes
Chapter 15 - C++ As A "Better C"
Pointers and References
Pointers, Dynamic Data, and Reference Types
Functions Chapter No. 5.
INTRODUCTION TO C.
Presentation transcript:

EEL 3801 C++ as an Enhancement of C

EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates the comment.  Cannot be used for more than one line.  The C comment indicator ( /*... */ ) can be used also if comment is to be for longer than one line.

EEL 3801 – Lotzi Bölöni Stream Input  C++ uses the standard input stream cin and the stream extraction operator >> to serve the same purpose of scanf() in C.  >> also called the get from operator.  Does not require format strings and conversion specifiers.

EEL 3801 – Lotzi Bölöni Stream Input  C++ knows what is the type of the data being read because the variable has been declared already.  The variable name to which the value read will be assigned does not need to be preceded by the & operator.  Must include the include file.

EEL 3801 – Lotzi Bölöni Stream Output  C++ uses the standard output stream cout and the stream insertion operator << to serve the same purpose of printf() in C.  << also called the put to operator.  Other than that, it has the same basic features as the cin stream and the >> operator.

EEL 3801 – Lotzi Bölöni Example #include main() { cout << “Enter your age:” ; int my_age; cin >> my_age; if (my_age < 18) cout << “you are a minor”; else cout << “You are getting old!”; }

EEL 3801 – Lotzi Bölöni Another Example #include main() { cout << “Enter two integers:” ; int x,y; cin >> x >> y; cout << “The sum of “ << x << “and “ << y << “ is “ << x+y << ‘\n’; }

EEL 3801 – Lotzi Bölöni Declarations  In C, all variable declarations must appear before any executable statements.  In C++, that is not the case.  Variables can be declared anywhere in the program as long as they precede the statement where the declared variable is first used.  See previous example.

EEL 3801 – Lotzi Bölöni Declarations  The variables can even be declared inside a loop specification parenthesis. for (int i = 0; i <= 5; i++) { cout << i << ‘\n’; }

EEL 3801 – Lotzi Bölöni Creating New Data Types  Like in C, enum, struct or union data structures are defined as models.  Unlike C, when an instance is declared, a data type is being automatically created.  The keywords struct, enum or union are not required.  typedef also not necessary - it is implicit.

EEL 3801 – Lotzi Bölöni Example struct Name { char first[10]; char last[10]; }; Name x; No need to use typedef or to say: struct Name x;

EEL 3801 – Lotzi Bölöni Function Prototypes  Unlike in C, functions prototypes are required.  When the function is defined prior to its use - the header serves as the prototype  C++ uses the prototype for type checking.  If nothing is placed within the parenthesis of the function prototype, C++ interprets that as void. C interprets as turning off checks.

EEL 3801 – Lotzi Bölöni Inline Functions  Similar in nature to preprocessor macros, but with some significant differences: –type checking is done in inline functions –no unexpected side effects as in macros –can be debugged with a debugger.  Only advises the compiler to replace the code segment in the text itself.  Only done by compiler for small functions.

EEL 3801 – Lotzi Bölöni Inline Functions  Otherwise, used as a regular C++ function.  If inline function is changed, all files in the program that use it must be recompiled. #include inline float cube(const float s) { return s*s*s;} main() { cout << “The answer is” << cube(4); }

EEL 3801 – Lotzi Bölöni Reference Parameters  Permit call by reference without using pointers.  A reference parameter is an alias for its corresponding argument.  Uses the & operator just as * is for pointers.  All operations on the reference parameter are actually performed on the original variable itself.

EEL 3801 – Lotzi Bölöni Reference Parameters  Do not have to be used only in function calls. int count = 1; //declare integer variable int &c = count;// c is alias for count c++; // increments count using its alias  For efficiency if argument is large, non- modifiable parameters can be passed to functions as references to constant variables

EEL 3801 – Lotzi Bölöni Reference Parameters #include void square_by_reference(int &); main() {int z = 4; cout << “z = “ << z; square_by_reference(z); cout << Z is now = “ << z; } void square_by_reference(int &cref) { cref = cref * cref; }

EEL 3801 – Lotzi Bölöni Reference Parameters - Caveats  Reference variables must be initialized in their declarations.  Cannot be reassigned as aliases to other variables.  Cannot be de-referenced using *  Cannot be used to perform pointer arithmetic.

EEL 3801 – Lotzi Bölöni Reference Parameters - Caveats  Can point to references, but you are actually pointing at the variable for which the reference is an alias.  Cannot compare references for the same reason.  Cannot get the address of a reference (same)  When returning reference (or a pointer) to a variable defined in function, make it static.

EEL 3801 – Lotzi Bölöni Constant Variables  The const keyword can be used to declare constant variables in a function.  Better than using the #define pre- processor directive.  Such variables cannot be changed. const float PI = ;  Constant variable must be initialized at declaration.

EEL 3801 – Lotzi Bölöni Constant Variables  Placing const in array declaration is illegal in C, but legal in C++.  Visible to debugger, while #define directives are not.  Pointers can also be declared as constant with the const operator.

EEL 3801 – Lotzi Bölöni Dynamic Memory Allocation  C++ Uses new and delete in lieu of malloc() and free.  Already saw that.

EEL 3801 – Lotzi Bölöni Default Arguments  Values can be specified for the parameters so that if an argument is not supplied, the default value is used by the function.  Must be the leftmost arguments in the parameter list.  Can be done with an inline function also.

EEL 3801 – Lotzi Bölöni Default Arguments int area(int length = 1, int width = 1) { return length * width; } Can be called in the following ways: area() ==> same as area(1,1) area(5) ==> same as area(5,1) area(5,5) ==> same as area(5,5)

EEL 3801 – Lotzi Bölöni Scope Resolution Operator  In C and C++, variables of the same name can be declared when one is global and the other local.  References to a variable while the local is in scope, refer to the local variable.  In such cases, the global variable is “invisible” and cannot be referenced.

EEL 3801 – Lotzi Bölöni Scope Resolution Operator  Scope resolution operator :: permits such a global variable to become visible where the local variable of the same name is in scope.  Cannot be used to refer to another variable of the same name in another function - just global variables.  In general, using two variables of the same name is not good programming practice.

EEL 3801 – Lotzi Bölöni Scope Resolution Operator #include float value = ; main() { int value = 7; cout << “Local value = “ << value << “Global value = “ << ::value ‘\n’; }

EEL 3801 – Lotzi Bölöni Function Overloading  In C, it is not legal to have two functions of the same name in the same program.  That is not the case in C++ as long as the functions have different sets of parameters.  Even the same parameters but of a different type is sufficient to distinguish them.  This is called function overloading.

EEL 3801 – Lotzi Bölöni Function Overloading  When overloaded function called, the C++ compiler selects the proper function by inspecting the order, types and number of arguments.  Typically used to write common functions that do the same things to different data types.

EEL 3801 – Lotzi Bölöni Function Overloading #include int square(int x) { return x * x; } float square(float y) { return y * y; } main() { cout << square(7) << ‘\n’; cout << square(7.5) << ‘\n’; }