Introduction to C++ Introduction to C++ 1

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

Introduction to C++ Systems Programming

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

6.5 Function Prototypes and Argument Coercion

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

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

AN OVERVIEW OF C++ 1

Functions and Recursion

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

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

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

C++ Programming Lecture 11 Functions Part I

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

Data Structures and Programming with C++

Chapter 3 - Functions

Scientific Computing

CHAPTER 4 FUNCTIONS. 4.1 Introduction

CS242 COMPUTER PROGRAMMING

Fast Introduction to Object Oriented Programming and C++

Chapter 1 Introduction to Computers and C++ Programming

Introducing C++ to Java Programmers

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Partha Sarathi Mandal

Dr. Md. Humayun Kabir CSE Department, BUET

Object-Oriented Programming

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

Chapter 3 - Functions

PIC10B/1 Winter 2014 Exam I Study Guide

Short Notes of CS201

Chapter 3 - Functions

Introduction to C++ (Extensions to C)

CS201 - Introduction to Programming Glossary By

Cpt S 122 Data Structures. Introduction to C++ Part II

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

CSC 307 DATA STRUCTURES AND ALGORITHM ANALYSIS IN C++ SPRING 2011

CSE 333 Lecture 9 - intro to C++

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

Unit 1 : Principles of object oriented programming

Programmazione. Prof. Marco Bertini

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

Functions and an Introduction to Recursion Pearson Education, Inc. All rights reserved.

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

C++ Functions. Last Week. Areas for Discussion. Program Structure. Last Week Introduction to Functions Program Structure and Functions

Tutorial-2a: First steps with C++ programming

Arrays. Week 4. Assylbek Jumagaliyev

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

Lab Instructor : Jean Lai

by Pearson Education, Inc. All Rights Reserved. 2

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

COMP322 - Introduction to C++ Lecture 01 - Introduction

Functions. Lecture 6 COP 3014 Spring February 11, 2018

C Pointers. 7.2 Pointer Variable Definitions and Initialization

Understanding main() function Input/Output Streams

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

PHY4321 Summary Notes

6.096 Introduction to C++ January (IAP) 2009

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Programming. C++ Basics

Homework #3 CS2255 Fall 2012

EP241 Computer Programming

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

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Come and join us at WebLyceum

Pointers and Strings. Adhi Harmoko S, M.Komp

Introduction to C++ with content from

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

C++ Programming Chapter 7 Pointers

Introduction to Programming

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Functions and Recursion

cast.c /* Program illustrates the use of a cast to coerce a function argument to be of the correct form. */

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

CSCE 110 PROGRAMMING FUNDAMENTALS

CSE 303: Concepts and Tools for Software Development

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

Your first C and C++ programs

Preface to the Second Edition Preface to the First Edition Brief Contents Introduction to C++ p. 1 A Review of Structures p.

CHAPTER 3 Expressions, Functions, Output

CS 376b Computer Vision

Classes, Constructors, etc., in C++

C++ Programming: Functions

IS 0020 Program Design and Software Tools

Cours de C++ Introduction

A SHORT COURSE ON C++

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Program Organization and Comments

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

CS3157: Advanced Programming. Outline

September 10,

Object Oriented Design

Functions. Introduction :

CSCI-1200 Data Structures Fall 2017 Lecture 2 STL Strings & Vectors

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

CS201 Some Important Definitions

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

Transcription:

1

What Is C++? (Mostly) an extension of C to include: Classes Templates Inheritance and Multiple Inheritance Function and Operator Overloading New (and better) Standard Library References and Reference Parameters Default Arguments Inline Functions Note: Objective C was invented at about the same time, with similar goals. A few small syntactic differences from C 2

Compiling C++ Use gcc, Visual Studio, etc. File types.cc,.cp,.cpp,.cpp,.cxx,.c++,.c.h,.h Some of these have special properties. 3

Background C++ was developed by Bjarne Stroustrup at Bell Laboratories Originally called C with classes The name C++ is based on C s increment operator (++) Indicating that C++ is an enhanced version of C Widely used in many applications and fields Well-suited to Programming in the Large 4

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

A Simple C++ Program <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. Replaces <stdio.h> of C C++ requires you to specify the return type, possibly void, for all functions. Specifying a parameter list with empty parentheses is equivalent to specifying a void parameter list in C. 6

18.5 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. Header file names ending in.h Are old-style header files Superseded by the C++ Standard Library header files Use #include directive to include class in a program. 7

Fig. 18.2 C++ Standard Library header files C++ Standard Library header files <iostream> <iomanip> <cmath> <cstdlib> <ctime> <vector>, <list> <deque>, <queue>, <stack>, <map>, <set>, <bitset> Explanation Contains function prototypes for the C++ standard input and standard output functions. This header file replaces header file <iostream.h>. This header is discussed in detail in Chapter 26, Stream Input/Output. Contains function prototypes for stream manipulators that format streams of data. This header file replaces header file <iomanip.h>. This header is used in Chapter 26, Stream Input/Output. Contains function prototypes for math library functions. This header file replaces header file <math.h>. Contains function prototypes for conversions of numbers to text, text to numbers, memory allocation, random numbers and various other utility functions. This header file replaces header file <stdlib>. Contains function prototypes and types for manipulating the time and date. This header file replaces header file <time.h>. These header files contain classes that implement the C++ Standard Library containers. Containers store data during a program s execution. 8 Continues for three pages!

Needed for Lab #4 <iostream> cin and cout for stream input and output <cmath> sqrt() 9

References in C++ Definition Reference: An Alternative Name for an Object BIG difference from Java References are only created in declarations and parameters A reference can only appear where the object itself could have appeared 14

Simple References void f() { int j = 1; int &r = j; int x = r; // x now is 1 r = 2; // j now is 2 } //f //r and j refer to same int Sometimes, reference declarations are written as int& r1 = k int k; int &r1 = k; // okay: r1 is initialized int &r2; extern int &r3;//okay; r3 defined elsewhere // error; initializer missing 15

Simple References (continued) void g() { int ii = 0; int &rr = ii; rr++; //ii now is 1 These '*' and '&' are not operators but rather declaration qualifiers. This '&' is the unary address operator brought over from C. int *pp = &rr; } //g // pp now points to ii Note: This declares a pointer exactly as in C, and initializes it with the address of rr (which is another name for ii) 16

Example Usage of a Reference int grid[1000]; int rowsize, x, y;... int &element = grid[x*rowsize+y];... /* computations on integer named element */ 17

Reference Parameters An alias for its corresponding argument in a function call. & placed after the parameter type in the function prototype and function header Example int &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. 18

Reference Parameter Example C version void swap (int *a, int *b) { int temp = *a; *a = *b; *b = temp; } // void swap( ) Hazard: a NULL pointer C++ version void swap (int &a, int &b) { int temp = a; a = b; b = temp; } // void swap( ) Non-hazard: no pointer here 19

Notes on References and Pointers Pointers in C do multiple duty Links, as in linked lists and trees Parameters, where the function needs to return a value to an argument provided by the caller Short-hand, a short way of referring to an object that otherwise would need a complex expression 20

Java vs. C++ References In Java, a reference is a data type. It can be assigned to, compared, copied, stored, etc. Same reference can refer to different objects at different times during execution In C++, a reference is an alias for an object It cannot be assigned to; assignment is through the reference to the underlying object Similar to dereferencing a pointer in C A reference always refers to the same object for the duration of its scope 21

Repeat Three Times A reference is not a pointer, A reference is not a pointer, A reference is not a pointer, And neither of them resembles a Java reference 22