C++ As A "Better C" Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Similar documents
Chapter 15 - C++ As A "Better C"

Functions and Recursion

Introduction to C++ Systems Programming

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

Chapter 1 Introduction to Computers and C++ Programming

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Introduction to C++ (Extensions to C)

CHAPTER 3 ARRAYS. Dr. Shady Yehia Elmashad

Chapter 3 - Functions

Chapter 3 - Functions

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Chapter 3 - Functions. Chapter 3 - Functions. 3.1 Introduction. 3.2 Program Components in C++

Introduction to C++ Introduction to C++ 1

C Functions. Object created and destroyed within its block auto: default for local variables

CS242 COMPUTER PROGRAMMING

Introduction to Programming

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

Introduction to C++ Programming. Adhi Harmoko S, M.Komp

Chapter 19 C++ Inheritance

6.5 Function Prototypes and Argument Coercion

2.11 Assignment Operators. Assignment expression abbreviations c = c + 3; can be abbreviated as c += 3; using the addition assignment operator

Pointers and Strings Prentice Hall, Inc. All rights reserved.

Chapter 21 - C++ Stream Input/Output

Chapter 21 - C++ Stream Input/Output

Chapter 3 - Functions

Functions. Function Prototypes. Function prototype is needed if the function call comes before the function definition in the program.

Pointers and Strings. Adhi Harmoko S, M.Komp

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Chapter 2 - Control Structures

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Engineering Problem Solving with C++, Etter/Ingber

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) A few types

C++, How to Program. Spring 2016 CISC1600 Yanjun Li 1

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

Control Statements: Part Pearson Education, Inc. All rights reserved.

Input and Output. Data Processing Course, I. Hrivnacova, IPN Orsay

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program)

CSc 10200! Introduction to Computing. Lecture 4-5 Edgardo Molina Fall 2013 City College of New York

Outline. Introduction. Pointer variables. Pointer operators. Calling functions by reference. Using const with pointers. Examples.

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

by Pearson Education, Inc. All Rights Reserved. 2

An inline function is one in which the function code replaces the function call directly. Inline class member functions

IS 0020 Program Design and Software Tools

Unit 1 : Principles of object oriented programming

Lecture 4 Tao Wang 1

C++ Quick Guide. Advertisements

Fast Introduction to Object Oriented Programming and C++

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Arrays. Week 4. Assylbek Jumagaliyev

Dr. Md. Humayun Kabir CSE Department, BUET

Fundamentals of Programming Session 25

Piyush Kumar. input data. both cout and cin are data objects and are defined as classes ( type istream ) class

Object Oriented Design

Chapter 2 - Control Structures

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Chapter 2 - Control Structures

Object Oriented Design

Another Simple Program: Adding Two Integers

Getting started with C++ (Part 2)

CS2141 Software Development using C/C++ Stream I/O

C++ Input/Output: Streams

More C++ Classes. Systems Programming

Programmazione. Prof. Marco Bertini

Computer Programming with C++ (21)

C Pointers. 7.2 Pointer Variable Definitions and Initialization

BITG 1233: Introduction to C++

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

IS 0020 Program Design and Software Tools

57:017, Computers in Engineering C++ Classes

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Chapter 12 - C++ Stream Input/Output

Arrays. Outline. Multidimensional Arrays Case Study: Computing Mean, Median and Mode Using Arrays Prentice Hall, Inc. All rights reserved.

Streams. Ali Malik

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

Chapter 2. Outline. Simple C++ Programs

Cours de C++ Introduction

Exercise 1.1 Hello world

Structured Program Development

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

CS3157: Advanced Programming. Outline

C++ Programming Lecture 11 Functions Part I

Lecture 3. Input and Output. Review from last week. Variable - place to store data in memory. identified by a name should be meaningful Has a type-

Exception Handling Pearson Education, Inc. All rights reserved.

Streams. Rupesh Nasre.

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

Lecture 9. Introduction

Chapter 18 - C++ Operator Overloading

Data Structures and Programming with C++

Scientific Computing

Objectivities. Experiment 1. Lab6 Array I. Description of the Problem. Problem-Solving Tips

Lab 6. Review of Variables, Formatting & Loops By: Dr. John Abraham, Professor, UTPA

Chapter 20 - C++ Virtual Functions and Polymorphism

Course "Data Processing" Name: Master-1: Nuclear Energy Session /2018 Examen - Part A Page 1

Functions and Recursion

cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... C++ vs Java identifiers.

Transcription:

C++ As A "Better C" Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2013 Fall

Outline 15.2 C++ 15.3 A Simple Program: Adding Two Integers 15.5 Header Files 15.6 Inline Functions 15.9 Unary Scope Resolution Operator 15.10 Function Overloading C.E., NCU, Taiwan 2

15.2 C++ C++ Object-oriented Increases software quality and reusability Developed by Bjarne Stroustrup at Bell Labs Called "C with classes" C++ (increment operator) - enhanced version of C Superset of C Can use a C++ compiler to compile C programs Gradually evolve the C programs to C++ ANSI C++ Final version at http://www.ansi.org/ Free, older version at http://www.cygnus.com/misc/wp/ C.E., NCU, Taiwan 3

15.3 A Simple Program: Adding Two Integers (1/3) File extensions C files:.c C++ files:.cpp (which we use),.cxx,.c (uppercase) Differences "comment out" a line : // <iostream> - input/output stream header file all functions must declare their return type C does not require it vcariables in C++ can be defined almost anywhere C.E., NCU, Taiwan 4

15.3 A Simple Program: Adding Two Integers (2/3) Input/Output in C++ Performed with streams of characters Streams sent to input/output objects Output std::cout - standard output stream (connected to screen) << stream insertion operator ("put to") std::cout << "hi"; Puts "hi" to std::cout, which prints it on the screen Input std::cin - standard input object (connected to keyboard) >> stream extraction operator ("get from") std::cin >> myvariable; Gets stream from keyboard and puts it into myvariable 5

15.3 A Simple Program: Adding Two Integers (3/3) std::endl "end line" Stream manipulator - prints a newline and flushes output buffer Some systems do not display output until "there is enough text to be worthwhile" std::endl forces text to be displayed using statements Allow us to remove the std:: prefix Discussed later Cascading Can have multiple << or >> operators in a single statement std::cout << "Hello " << "there" << std::endl; 6

1 // Fig. 15.1: fig15_01.cpp 2 // Addition program 3 #include <iostream> 4 5 int main() 6 { 7 int integer1; 8 9 std::cout << "Enter first integer\n" n"; 10 std::cin >> integer1; 11 12 int integer2, sum; // declaration 13 14 std::cout << "Enter second integer\n" n"; 15 std::cin >> integer2; 16 sum = integer1 + integer2; 17 std::cout << "Sum is " << sum << std::endl; 18 19 return 0; // indicate that program ended successfully 20 } // end function main Enter first integer 45 Enter second integer 72 Sum is 117 7

15.6 inline Functions Function calls Cause execution-time overhead Qualifier inline before function return type "advises" a function to be inlined Puts copy of function's code in place of function call Speeds up performance but increases file size Compiler can ignore the inline qualifier inline double cube( const double s ) { return s * s * s; } Using statements By writing using std::cout; we can write cout instead of std::cout in the program Same applies for std::cin and std::endl C.E., NCU, Taiwan 8

1 // Fig. 15.3: fig15_03.cpp 2 // Using an inline function to calculate 3 // the volume of a cube. 4 #include <iostream> 5 6 using std::cout; fig15_03.cpp std::cin; std::endl; 7 using std::cin; 8 using std::endl; 9 10 inline double cube( const double s ) { return s * s * s; } 11 12 int main() 13 { 14 double side; 15 16 for ( int k = 1; k < 4; k++ ) { 17 cout << "Enter the side length of your cube: "; 18 cin >> side; 19 cout << "Volume of cube with side " 20 << side << " is " << cube( side ) << endl; 21 } // end for 22 23 return 0; 24 } // end function main 9

Enter the side length of your cube: 1.0 Volume of cube with side 1 is 1 Enter the side length of your cube: 2.3 Volume of cube with side 2.3 is 12.167 Enter the side length of your cube: 5.4 Volume of cube with side 5.4 is 157.464 10

1 // Fig. 15.8: fig15_08.cpp 2 // Using default arguments 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 int boxvolume( int length = 1, int width = 1, int height = 1 ); 9 10 int main() 11 { 12 cout << "The default box volume is: " << boxvolume() 13 << "\n\nthe nthe volume of a box with length 10,\n" 14 << "width 1 and height 1 is: " << boxvolume( 10 ) 15 << "\n\nthe nthe volume of a box with length 10,\n" 16 << "width 5 and height 1 is: " << boxvolume( 10, 5 ) 17 << "\n\nthe nthe volume of a box with length 10,\n" 18 << "width 5 and height 2 is: " << boxvolume( 10, 5, 2 ) 19 << endl; 20 21 return 0; 22 } // end function main 23 11

24 // Calculate the volume of a box 25 int boxvolume( int length, int width, int height ) 26 { 27 return length * width * height; 28 } // end function boxvolume The default box volume is: 1 The volume of a box with length 10, width 1 and height 1 is: 10 The volume of a box with length 10, width 5 and height 1 is: 50 The volume of a box with length 10, width 5 and height 2 is: 100 12

15.9Unary Scope Resolution Operator Unary scope resolution operator (::) Access global variables if a local variable has same name Instead of variable use ::variable static_cast<newtype> (variable) Creates a copy of variable of type newtype Convert ints to floats, etc. Stream manipulators Can change how output is formatted setprecision - set precision for floats (default 6 digits) setiosflags - formats output setwidth - set field width C.E., NCU, Taiwan 13

1 // Fig. 15.9: fig15_09.cpp 2 // Using the unary scope resolution operator 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 using std::ios; 8 9 #include <iomanip> 10 11 using std::setprecision; 12 using std::setiosflags; 13 using std::setw; 14 15 const double PI = 3.14159265358979; 16 17 int main() 18 { 19 const float PI = static_cast< float >( ::PI ); 20 21 cout << setprecision( 20 ) 22 << " Local float value of PI = " << PI 23 << "\nglobal double value of PI = " << ::PI << endl; 14 24

25 cout << setw( 28 ) << "Local float value of PI = " 26 << setiosflags( ios::fixed ios::showpoint ) 27 << setprecision( 10 ) << PI << endl; 28 return 0; 29 } // end function main Borland C++ command-line compiler output Local float value of PI = 3.141592741012573242 Global double value of PI = 3.141592653589790007 Local float value of PI = 3.1415927410 Microsoft Visual C++ compiler output Local float value of PI = 3.1415927410125732 Global double value of PI = 3.14159265358979 Local float value of PI = 3.1415927410 15

15.10 Function Overloading Function overloading: Functions with same name and different parameters Overloaded functions should perform similar tasks Function to square ints and function to square floats int square( int x) {return x * x;} float square(float x) { return x * x; } Program chooses function by signature Signature determined by function name and parameter types Type safe linkage - ensures proper overloaded function called C.E., NCU, Taiwan 16

1 // Fig. 15.10: fig15_10.cpp 2 // Using overloaded functions 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 fig15_10.cpp 8 int square( int x ) { return x * x; } 9 10 double square( double y ) { return y * y; } 11 12 int main() 13 { 14 cout << "The square of integer 7 is " << square( 7 ) 15 << "\nthe square of double 7.5 is " << square( 7.5 ) 16 << endl; 17 18 return 0; 19 } // end function main The square of integer 7 is 49 The square of double 7.5 is 56.25 17