C to C++ © Bruce M. Reynolds & Cliff Green, 2002 1 C++ Programming Certificate University of Washington Cliff Green.

Slides:



Advertisements
Similar presentations
1 C++Tutorial Rob Jagnow This tutorial will be best for students who have at least had some exposure to Java or another comparable programming language.
Advertisements

EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
1 String Library and Stream I/O Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.
1 Lecture Note 7- Classes Part II Outline Composition: Objects as Members of Classes Dynamic Memory static Class Members Operator Overloading.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Function 2 (L17) * Function Prototype * Promotion Rules * Data Type * Library Header File * Customer Header File * Case Study * Exercise/Home Work Dr.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Review of C++ Programming Part II Sheng-Fang Huang.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
1 Chapter 8 Destructor & Operator Overloading. 2 Destructor  A destructor is a function that is called when an object is no longer required. A constructor.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Templates © Bruce M. Reynolds & Cliff Green 1 // min function template #include using std::cout; using std::endl; // there’s already one of these in the.
 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.
Copyright 2008 Oxford Consulting, Ltd 1 October C to C++ C++ Comments Can use the symbol // To identify single line comments.
C++ Streams © Bruce M. Reynolds & Cliff Green, C++ Programming Certificate University of Washington Cliff Green.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
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.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
Functions Sujana Jyothi C++ Workshop Day 2. Functions 3 Parameter transmission modes pass by value (default) pass by reference (&) pass by const reference.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
System Programming Practical Session 7 C++ Memory Handling.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
Data Types Storage Size Domain of all possible values Operations 1.
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.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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.
Functions BICSE-6A Mr. Naeem Khalid Lecturer, Dept. of Computing.
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.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING Dr. Shady Yehia Elmashad.
Functions Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
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.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
 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.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Chapter 15 - C++ As A "Better C"
Overloading C++ supports the concept of overloading Two main types
Chapter 1.2 Introduction to C++ Programming
Pointers and Dynamic Arrays
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
C Stuff CS 2308.
Popping Items Off a Stack Lesson xx
Chapter 15 - C++ As A "Better C"
Arrays Arrays A few types Structures of related data items
Fundamental Programming
C++ Programming Lecture 20 Strings
Presentation transcript:

C to C++ © Bruce M. Reynolds & Cliff Green, C++ Programming Certificate University of Washington Cliff Green

C to C++ © Bruce M. Reynolds & Cliff Green, // bool data type, Quincy Ex. 2.2 #include int main() { bool senior (true);// bool variable, init’ed to true // bool senior = true;// common alternative style // --- test the senior variable if (senior) { std::cout << "Senior citizen rates apply"; } return 0; }

C to C++ © Bruce M. Reynolds & Cliff Green, // default function arguments, Quincy Ex. 3.6 // The function prototype can specify values that will be // filled in by the compiler if they are not included in the // call. #include void show(int = 1, float = 2.3, long = 4); int main() { show(); // all three arguments default show(5); // provide 1st argument show(6, 7.8); // provide 1st two show(9, 10.11, 12L); // provide all three arguments } void show(int first, float second, long third) { std::cout << " first = " << first; std::cout << ", second = " << second; std::cout << ", third = " << third << endl; }

C to C++ © Bruce M. Reynolds & Cliff Green, // overloaded function names, Jim Peckol // Functions can have the same name as long as each has a // unique signature. That includes number, type, and order of // arguments. Return type must be the same. #include using std::cout; using std::endl; void Print(int i); void Print(char c); void Print(char const * s); int main() { char const * s ("This is a string“); Print (10);// Print an int Print('a'); // Print a char Print (s);// Pring a string return 0; } void Print (int i) { cout << "integer: \t" << i << endl; } void Print (char c) { cout << "character: \t" << c << endl; } void Print (char const * s) { cout << "string: \t" << s << endl; }

C to C++ © Bruce M. Reynolds & Cliff Green, // using references, Quincy Ex #include using std::cout; using std::endl; int main() { int actualint = 123; int& otherint = actualint; cout << actualint << endl; cout << otherint << endl; ++otherint; cout << actualint << endl; cout << otherint << endl; ++actualint; cout << actualint << endl; cout << otherint << endl; } // 123 // 124 // 125

C to C++ © Bruce M. Reynolds & Cliff Green, // references to reduce complex notation, Quincy Ex #include using std::cout; using std::endl; // --- a Date structure struct Date { int month, day, year; }; // --- an Employee structure struct Employee { int empno; char name[35]; Date dates[3];// hired, last review, terminated float salary; }; Employee staff[] = { { 1, "Bill", {{12,1,88},{2,24,92},{5,5,95}}, }, { 1, "Paul", {{10,3,87},{5,17,94},{3,7,96}}, }, { 1, "Jim", {{ 9,5,80},{9,11,96},{0,0, 0}}, }, { 0 } };

C to C++ © Bruce M. Reynolds & Cliff Green, // references to reduce complex notation, Quincy Ex // continued int main() { Employee* pEmpl = staff; while (pEmpl->empno != 0){ for (int i = 0; i < 3; ++i){ Date& rd = pEmpl->dates[i]; cout << rd.month << '/' << rd.day << '/' << rd.year << " "; } cout << endl; ++pEmpl; } return 0; }

C to C++ © Bruce M. Reynolds & Cliff Green, // Pass by reference, Quincy Ex // Default argument passing is by value. For better // performance, pass by reference. #include using std::cout; using std::endl; // a big structure struct bigone { int serno; char text[1000]; // a lot of chars }; // -- two functions with a structure parameter void slowfunc(bigone p1); // call by value void fastfunc(bigone const & p1); // call by reference int main() { static bigone bo = { 123, "This is a BIG structure"}; slowfunc(bo); // this will take a while fastfunc(bo); // this will be faster return 0; }

C to C++ © Bruce M. Reynolds & Cliff Green, // Pass by reference, Quincy Ex continued // ---- a call-by-value function void slowfunc(bigone p1) { cout << p1.serno << endl; cout << p1.text << endl; } // ---- a call by reference function void fastfunc(bigone const & p1) { cout << p1.serno << endl; cout << p1.text << endl; }

C to C++ © Bruce M. Reynolds & Cliff Green, // Return by reference, Quincy Ex #include using std::cout; using std::endl; using std::cin; // a date structure struct Date { int month, day, year; }; // an array of dates Date birthdays[] = { {12, 17, 37}, {10, 31, 38}, { 6, 24, 40}, {11, 23, 42}, { 8, 5, 44}, }; // a function to retrieve a date Date const & getdate(int n) { return birthdays[n-1]; }

C to C++ © Bruce M. Reynolds & Cliff Green, // Return by reference, Quincy Ex continued int main() { int dt = 99; while (dt != 0){ cout << endl << "Enter date # (1-5, 0 to quit): "; cin >> dt; if (dt > 0 && dt < 6){ Date const & bd = getdate(dt); cout << bd.month << '/' << bd.day << '/' << bd.year << endl; } return 0; }

C to C++ © Bruce M. Reynolds & Cliff Green, // new and delete, Quincy, Ex #include using std::cout; struct Date { // a date structure int month; int day; int year; }; int main() { Date* birthday = new Date; // get memory for a date birthday->month = 6; // assign a value to the date birthday->day = 24; birthday->year = 1940; cout << "I was born on " // display the date month << '/' day << '/' year; delete birthday; // return memory to the heap return 0; }

C to C++ © Bruce M. Reynolds & Cliff Green, // new and delete, Jim Peckol #include #include // C library functions using std::cout; using std::endl; char *growString (char const * myString, char const * aString); int main() { char const * base_str = “My String”; char const * stuff[] = {" is ", "getting ", "longer "}; char* s = new char [strlen (base_str) +1]; strcpy (s, base_str); for (int i = 0; i < 3; ++i) { s = growString(s, stuff[i]); cout << s << endl; } return 0; } // Dynamically grow a string by adding a string of characters // to the tail. char *growString(char const * myString, char const * aString) { char *tString = new char [strlen(myString) +strlen(aString) + 1]; strcpy (tString, myString); strcat (tString, aString); delete[] myString; return tString; }

C to C++ © Bruce M. Reynolds & Cliff Green, // file and local scope, Jim Peckol #include using std::cout; using std::endl; int i = 10; int main() { cout << "At file scope i = " << :: i << endl; for (int i = 0; i< 3; i++) { cout << "\tLocal Scope 1 i = " << i << endl; for (int i = 4; i < 7; i++) cout << "\t\t Local Scope 2 i = " << i << endl; cout << "\tLocal Scope 2 i = " << i << endl << endl; } cout << "\tLocal Scope 1 i = " << i << endl; return 0; }

C to C++ © Bruce M. Reynolds & Cliff Green, // file and local scope, Jim Peckol, continued /* * This will print: *At file scope i = 10 *Local Scope 1 i = 0 *Local Scope 2 i = 4 *Local Scope 2 i = 5 *Local Scope 2 i = 6 *Local Scope 2 i = 7 * *Local Scope 1 i = 1 *Local Scope 2 i = 4 *Local Scope 2 i = 5 *Local Scope 2 i = 6 *Local Scope 2 i = 7 * *Local Scope 1 i = 2 *Local Scope 2 i = 4 *Local Scope 2 i = 5 *Local Scope 2 i = 6 *Local Scope 2 i = 2 * *Local Scope 1 i = 10 */

C to C++ © Bruce M. Reynolds & Cliff Green, // inline #include using std::cout; using std::endl; // Macro to compute area of a circle #define CIRCLE_AREA(x) ( * ( x ) * ( x ) ) // Function to compute area of a circle inline double CircleArea( double x ) { return * x * x; }

C to C++ © Bruce M. Reynolds & Cliff Green, // inline, continued int main() { double radius; double area1, area2, area3; radius = 4.0; area1 = CIRCLE_AREA( radius ); area2 = CIRCLE_AREA( radius++ ); radius = 4.0; area3 = CircleArea( radius++ ); cout << "Circle r=4, area = " << area1 << endl; cout << "Circle r=4, area = " << area2 << endl; cout << "Circle r=4, area = " << area3 << endl; } // Circle r=4, area = // Circle r=4, area = // Circle r=4, area =