Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.

Slides:



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

C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
 2006 Pearson Education, Inc. All rights reserved Functions.
 2006 Pearson Education, Inc. All rights reserved Functions.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
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 Machine Languages, Assembly Languages, and High-level Languages Three types of computer languages 1.Machine.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
 2006 Pearson Education, Inc. All rights reserved Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
Introduction to C++CS-2303, C-Term Introduction to C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
C++ Functions CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
 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.
Review of C++ Programming Part II Sheng-Fang Huang.
Introduction to C++ Systems Programming.
You gotta be cool. C++ ? C++ Standard Library Header Files Inline Functions References and Reference Parameters Default Arguments and Empty Parameter.
Chapter 01: Introduction to Computer Programming
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
 2008 Pearson Education, Inc. All rights reserved Function Call Stack and Activation Records Data structure: collection of related data items.
 2006 Pearson Education, Inc. All rights reserved Functions and an Introduction to Recursion.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
 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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
 2006 Pearson Education, Inc. All rights reserved Functions and an Introduction to Recursion.
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.
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.
 2003 Prentice Hall, Inc. All rights reserved Storage Classes Variables have attributes –Have seen name, type, size, value –Storage class How long.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
Chapter 18 Introduction to Custom Templates C++ How to Program, 9/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor Note:
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Object-Oriented Programming (OOP) and C++
 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 Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
 2006 Pearson Education, Inc. All rights reserved Functions and an Introduction to Recursion.
Chapter 15 - C++ As A "Better C"
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Functions.
Chapter 1.2 Introduction to C++ Programming
Chapter 1: Introduction to computers and C++ Programming
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.11 Function Call Stack and Activation Records
6 Functions.
Functions and an Introduction to Recursion
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
6 Functions.
Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Miscellaneous C++ Topics
Introduction to C++ Programming
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Variables have attributes
Chapter 15 - C++ As A "Better C"
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Presentation transcript:

Introduction to C++ Systems Programming

Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between C and C++  A Simple C++ Example –C++ Input/Output  C++ Libraries –C++ Header Files  Another Simple C++ Example –Inline Functions  Call by Reference in C++  References and Reference Parameters

Systems Programming: Introduction to C++ 3 Systems Programming: 3 Introduction to C++   Default Arguments   Unary Scope Resolution Operator

Systems Programming: Introduction to C++ 4 Systems Programming: 4 Introduction to C++   C++ was developed by Bjarne Stroustrup at Bell Laboratories – –Originally called “C with classes” – –The name C++ includes C’s increment operator (++) Indicate that C++ is an enhanced version of C   C++ programs –classesfunctions. –Built from pieces called classes and functions.   C++ Standard Library – –Rich collections of existing classes and functions

Systems Programming: Introduction to C++ 5 Why use C++   Many claim it is a better C because it is all of C with additions:   Objects {and object-oriented philisophy}   Inheritance   Polymorphism   Exception handling   Templates

Systems Programming: Introduction to C++ 6 Simple C++ Example // C++ simple example #include //for C++ Input and Output int main () { int number3; std::cout << "Enter a number:"; std::cin >> number3; int number2, sum; std::cout << "Enter another number:"; std::cin >> number2; sum = number2 + number3; std::cout << "Sum is: " << sum <<std::endl; return 0; } standard output stream object stream insertion operator stream extraction operator standard input stream object stream manipulator Concatenating insertion operators

Systems Programming: Introduction to C++ 7 A Simple C++ Program   C++ file names can have one of several extensions –.cpp –Such as:.cpp,.cxx or.C (uppercase)   Commenting –// –A // comment is a maximum of one line long. – –A /*…*/ C-style comments can be more than one line long.  iostream – –Must be included for any program that outputs data to the screen or inputs data from the keyboard using C++ style stream input/output.  void  C++ requires you to specify the return type, possibly void, for all functions. – void –Specifying a parameter list with empty parentheses is equivalent to specifying a void parameter list in C.

Systems Programming: Introduction to C++ 8  std::endl  Stream manipulator std::endl – –Outputs a newline. – –Flushes the output buffer.  std::cout name ( cout ) “namespace” ( std ).  The notation std::cout specifies that we are using a name ( cout ) that belongs to a “namespace” ( std ). A Simple C++ Program

Systems Programming: Introduction to C Header Files   C++ Standard Library header files – –Each contains a portion of the Standard Library. Function prototypes for the related functions Definitions of various class types and functions Constants needed by those functions – –“Instruct” the compiler on how to interface with library and user-written components. –.h –Header file names ending in.h Are “old-style” header files Superseded by the C++ Standard Library header files –#include –Use #include directive to include class in a program.

Systems Programming: Introduction to C++ 10 Fig C++ Standard Library header files

Systems Programming: Introduction to C Inline Functions   Inline functions – –Reduce function call overhead—especially for small functions. –inline –Qualifier inline before a function’s return type in the function definition “Advises” the compiler to generate a copy of the function’s code in place (when appropriate) to avoid a function call. – –Trade-off of inline functions Multiple copies of the function code are inserted in the program (often making the program larger). –inline –The compiler can ignore the inline qualifier and typically does so for all but the smallest functions.

Systems Programming: Introduction to C++ 12 inline qualifier Complete function definition so the compiler knows how to expand a cube function call into its inlined code. Another Simple C++ Program

Systems Programming: Introduction to C++ 13 cube function call that could be inlined Another Simple C++ Program

Systems Programming: Introduction to C++ 14 Fig Fig C++ keywords

Systems Programming: Introduction to C++ 15 Fig Fig C++ keywords

Systems Programming: Introduction to C Inline Functions (Cont.)  using  using statements help eliminate the need to repeat the namespace prefix –std:: –Ex: std::   for statement’s condition evaluates to either 0 (false) or nonzero (true) –bool –Type bool represents boolean (true/false) values. The two possible values of a bool are the keywords true and false. –When true and false are converted to integers, they become the values 1 and 0, respectively. bool null –When non-boolean values are converted to type bool, non- zero values become true, and zero or null pointer values become false.

Systems Programming: Introduction to C References and Reference Parameters   Reference Parameter – –An alias for its corresponding argument in a function call. – –& placed after the parameter type in the function prototype and function header – –Example int &countint &count in a function header –Pronounced as “ count is a reference to an int ” – –Parameter name in the called function body actually refers to the original variable in the calling function.

Systems Programming: Introduction to C++ 18 Function illustrating pass-by-value Function illustrating pass-by-reference Variable is simply mentioned by name in both function calls Call by Reference and Call by Value in C++

Systems Programming: Introduction to C++ 19 Receives copy of argument in main Receives reference to argument in main Modifies variable in main Call by Reference and Call by Value in C++

Systems Programming: Introduction to C++ 20   References – –are used as aliases for other variables within a function. All operations supposedly performed on the alias (i.e., the reference) are actually performed on the original variable. An alias is simply another name for the original variable. Must be initialized in their declarations. –Cannot be reassigned afterward. – –Example int count = 1; int &cRef = count; cRef++;int count = 1; int &cRef = count; cRef++; cRef –Increments count through alias cRef 18.7 References and Reference Parameters

Systems Programming: Introduction to C++ 21 Creating a reference as an alias to another variable in the function Assign 7 to x through alias y References and Reference Parameters

Systems Programming: Introduction to C++ 22 Uninitialized reference References and Reference Parameters

Systems Programming: Introduction to C++ 23 References // Three ways in C++ #include #include int main () { int y = 8; int y = 8; int &yref = y; int &yref = y; int *yptr = &y; int *yptr = &y; printf(" y = %d\n using ref y = %d\n using pointer y = %d\n", y, yref, *yptr); y, yref, *yptr); return 0; return 0;} $ g++ -o ref ref.cpp $./ref y = 8 using ref y = 8 using pointer y = 8 4 y yref yptr

Systems Programming: Introduction to C++ 24 References and Reference Parameters   Returning a reference from a function – –Functions can return references to variables. Should only be used when the variable is static. – –A Dangling reference Returning a reference to an automatic variable –That variable no longer exists after the function ends.

Systems Programming: Introduction to C Default Arguments   Default argument – –A default value to be passed to a parameter. Used when the function call does not specify an argument for that parameter – –Must be the rightmost argument(s) in a function’s parameter list. – –Should be specified with the first occurrence of the function name. Typically the function prototype

Systems Programming: Introduction to C++ 26 Default arguments Calling function with no arguments Calling function with one argument Calling function with two arguments Calling function with three arguments Default Arguments

Systems Programming: Introduction to C++ 27 Note that default arguments were specified in the function prototype, so they are not specified in the function header Default Arguments

Systems Programming: Introduction to C Unary Scope Resolution Operator  ::  Unary scope resolution operator ( :: ) – –Used to access a global variable when a local variable of the same name is in scope. – –Cannot be used to access a local variable of the same name in an outer block.

Systems Programming: Introduction to C++ 29 Unary scope resolution operator used to access global variable number Unary Scope Resolution Operator

Systems Programming: Introduction to C Function Overloading   Overloaded functions – –Overloaded functions have The same name But different sets of parameters – –Compiler selects proper function to execute based on number, types and order of arguments in the function call. – –Commonly used to create several functions of the same name that perform similar tasks, but on different data types.

Systems Programming: Introduction to C++ 31 Function Overloading Defining a square function for int s Defining a square function for double s Output confirms that the proper function was called in each case

Systems Programming: Introduction to C++ 32 Constructor overload class Listnode { Listnode () { link = NULL; } Listnode( string word) { link = NULL; lword = word; } … Private: Listnode* link; string lword; };

Systems Programming: Introduction to C Function Templates   A more compact and convenient form of overloading. – –Identical program logic and operations for each data type.   Function template definition – –Written by programmer once. – –Essentially defines a whole family of overloaded functions. – template –Begins with the template keyword. – <> –Contains a template parameter list of formal type and the parameters for the function template are enclosed in angle brackets ( <> ). – –Formal type parameters typenameclass.Preceded by keyword typename or keyword class. Placeholders for fundamental types or user-defined types.

Systems Programming: Introduction to C Function Templates   Function-template specializations – –Generated automatically by the compiler to handle each type of call to the function template. – max T int –Example for function template max with type parameter T called with int arguments maxCompiler detects a max invocation in the program code. intTint is substituted for T throughout the template definition. max.This produces function-template specialization max.

Systems Programming: Introduction to C++ 35 Function Template Example Using formal type parameter T in place of data type

Systems Programming: Introduction to C++ 36 Common Programming Error  class typename  Not placing keyword class or keyword typename before every formal type parameter of a function template (e.g., writing instead of ) is a syntax error.

Systems Programming: Introduction to C++ 37 Invoking maximum with int arguments Function Template Example

Systems Programming: Introduction to C++ 38 Invoking maximum with double arguments Invoking maximum with char arguments Function Template Example