C++ Basics. Brian A. Malloy. References Data Expressions Control Structures Functions. Slide 1 of 24. Go Back. Full Screen. Quit.

Similar documents
Introduction to Programming

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

CS242 COMPUTER PROGRAMMING

6.096 Introduction to C++ January (IAP) 2009

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

CS2141 Software Development using C/C++ C++ Basics

Chapter7 Expression and Assignment Statement. Introduction

CE221 Programming in C++ Part 1 Introduction

Introduction to C++ Systems Programming

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

The C++ Language. Arizona State University 1

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

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

Introduction to Programming

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

UNIT- 3 Introduction to C++

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

CS 376b Computer Vision

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

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Introduction to C++ (Extensions to C)

IS 0020 Program Design and Software Tools

CHAPTER 3 BASIC INSTRUCTION OF C++

Chapter 7. Additional Control Structures

Procedures, Parameters, Values and Variables. Steven R. Bagley

Partha Sarathi Mandal

Review of the C Programming Language for Principles of Operating Systems

Cours de C++ Introduction

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

Operators. Lecture 3 COP 3014 Spring January 16, 2018

CSCE 110 PROGRAMMING FUNDAMENTALS

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

LECTURE 02 INTRODUCTION TO C++

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

A First Program - Greeting.cpp

Creating a C++ Program

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Short Notes of CS201

COMP322 - Introduction to C++ Lecture 01 - Introduction

CS201 - Introduction to Programming Glossary By

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Chapter 2 - Control Structures

Numbers. John Perry. Spring 2017

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Review of the C Programming Language

3. Java - Language Constructs I

CHAPTER 3 Expressions, Functions, Output

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Why Is Repetition Needed?

W3101: Programming Languages C++ Ramana Isukapalli

Programming. C++ Basics

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Fast Introduction to Object Oriented Programming and C++

Expressions and Precedence. Last updated 12/10/18

C: How to Program. Week /Mar/05

1.3b Type Conversion

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Chapter 3. Numeric Types, Expressions, and Output

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Chapter 1 Introduction to Computers and C++ Programming

Introduction to Programming EC-105. Lecture 2

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

The New C Standard (Excerpted material)

Chapter 2. C++ Syntax and Semantics, and the Program Development Process. Dale/Weems 1

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

CS103L FALL 2017 UNIT 1: TYPES, VARIABLES,EXPRESSIONS,C++ BASICS

Increment and the While. Class 15

LECTURE 3 C++ Basics Part 2

UNIT-2 Introduction to C++

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program


PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

IS 0020 Program Design and Software Tools

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

Introduction to C ++

Fundamentals of Programming Session 25

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Computer Science II Lecture 1 Introduction and Background

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

Computer Programming : C++

C++ Programming: From Problem Analysis to Program Design, Third Edition

More Programming Constructs -- Introduction

Definition: Data Type A data type is a collection of values and the definition of one or more operations on those values.

Physics 234: Computational Physics

Chapter 2: Using Data

Connecting with Computer Science, 2e. Chapter 15 Programming II

UEE1303(1070) S 12 Object-Oriented Programming in C++

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

APPENDIX A : KEYWORDS... 2 APPENDIX B : OPERATORS... 3 APPENDIX C : OPERATOR PRECEDENCE... 4 APPENDIX D : ESCAPE SEQUENCES... 5

Chapter 3: Operators, Expressions and Type Conversion

Chapter 7. Expressions and Assignment Statements ISBN

Chapter 8. Operator Overloading, Friends, and References. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Transcription:

C++ Basics January 19, 2012 Brian A. Malloy Slide 1 of 24

1. Many find Deitel quintessentially readable; most find Stroustrup inscrutable and overbearing: Slide 2 of 24

1.1. Meyers Texts Two excellent books if you know C++. Scott Meyers provides two references, each with Items that expose subtleties about correct use of the C++ language. I ll refer to these Items throughout the course: Slide 3 of 24

1.2. Templates and STL The Vandevoorde & Josuttis text is a great introduction to C++ templates; and Josuttis STL reference is excellent: Slide 4 of 24

2. Slide 5 of 24

2.1. Simple Types bool can be true or false enums define constant values Named constants are preferable to # define, which is a C artifact (Meyers Item # 1): const char STAR = * ; const unsigned MAX = 100; Slide 6 of 24

2.2. Mixed Type Are promoted or truncated: 1. int(2.3) evaluates to 2 2. float(2/4) evaluates to 0.0 3. 4/8 evaluates to 0 4. float(4)/8 evaluates to 0.5 5. 2.0/4 evaluates to 0.5 Prefer C++ cast (Meyers Item #2): static cast<float>(5/10) evaluates to 0.0 Slide 7 of 24

2.3. Structured Types unions: obviated by inheritance structs: same as classes except for default protection: Default protection of class is private Default protection of struct is public Arrays, like C, are passed by reference Classes are covered in slides about classes Slide 8 of 24

3. Composed of operators, variables, constants and parentheses Logical operators: &&,,! Relational operators: <, >, ==,!=, <=, >= However, an expression can be considered as a Boolean condition where 0 is false and all other values are true: int x = rand(); if (x)... Of course, the rules for mixed types still apply, so 2/4 evaluates to 0 Slide 9 of 24

3.1. Operators unary, binary, and ternary describe the number of operands that an operator uses. For example, -7 is unary minus; i.e., one operand 3-7 is binary minus; i.e., two operands There is only one ternary operator and it s very useful; for example, the following expression evaluates to the larger of the two operands: (a > b)? a : b Slide 10 of 24

3.2. Prefix and Postfix Operators Prefix operators are evaluated in place. Postfix operators are evaluated at the end of the statement 1 #include <iostream> 2 int main() { 3 int i = 0, j = 0; 4 std::cout << ++i << std::endl; //output is 1 5 std::cout << j++ << std::endl; //output is 0 6 std::cout << i << j << std::endl; //output is 11 7 return 0; 8 } Slide 11 of 24

3.3. Insertion/Extraction Operators They are binary, left associative operators that evaluate to the operator For example, the stream insertion operator, operator evaluates to operator, which is why the following expression works: The expression: cout << x << y << endl; is actually: (((cout << x) << y) << endl); where (cout << x) places the value of x into the output stream and evaluates to cout << so that the expression becomes: ((cout << y) << endl); which places y into the output stream and evaluates to (cout << endl); Slide 12 of 24

4. selection: if, if/else, switch repetition: for, while, do/while In general, I much prefer clarity and readability to obfuscated, hacked, terse code. Thus, I prefer the use of brackets because they promote readability. The first example below is preferable to the second: int sum = 0; for (unsigned i = 0; i < MAX; ++i) { sum += i; } int sum = 0; for (unsigned i = 0; i < MAX; ++i) sum += i; Slide 13 of 24

4.1. switch If a switch value matches a case value, then it matches all cases until a break is encountered: int count = 0; int index = 1; switch (index) { case 0: ++count; case 1: ++count; case 2: ++count; case 3: ++count; case 4: ++count; case 5: ++count; default: ++count; } cout << index << endl; // prints 6 Slide 14 of 24

4.2. switch/case/break is useful We may wish to match several values, so multiple case values w/out a break are like logical or. In the next example, we can match either upper or lower case letters: int count = 0; char ch = b ; switch (ch) { case A : case a : ++count; break; case B : case b : ++count; break; case C : case c : ++count; break; default: cout << "Oops" << endl;; } Slide 15 of 24

4.3. Short-circuit Evaluation If evaluation of the first operand obviates evaluation of the second, then the second operand is not evaluated. Short-circuit evaluation can be useful. If number happens to be zero, then we won t get a division by zero error in the following example: float sum = 0.0; int number = rand(); if ( number!= 0 && sum/number > 90.0) {... } Slide 16 of 24

4.4. for The scope of the loop control variable (LCV) (in this case i) is the loop body: for (int i = 0; i < MAX; ++i) { cout << i; } i is out of scope here The following hack would be more readable if the programmer used while (true) // Obfuscated code; great for job security! i = 0; for ( ; ; ) { if (i > MAX) break; cout << ++i; } Slide 17 of 24

5. Can be void or return a value. Each C++ program contains a function called main, which returns an integer. There are two acceptable forms of main: ************************************ int main() { return 0; } ************************************ int main(int argc, char* argv[]) { return 0; } ************************************ and the return statement is optional Slide 18 of 24

5.1. Parameter Transmission Modes The C language has one mode C++ has three modes: 1. value: default; makes local copy 2. reference: use &; pass the address 3. const reference: for large objects 1 #include <iostream> 2 void f(int x) { ++x; } 3 void g(int& x) { ++x; } 4 int main() { 5 int i = 0, j = 0; 6 f(i); 7 g(j); 8 std::cout << i << j << std::endl; //output is 01 9 } Slide 19 of 24

5.2. Arrays are passed by reference 1 #include <iostream> 2 const int MAX = 3; 3 void f(int a[]) { 4 for (int i = 0; i < MAX; ++i) { 5 a[i] = i; 6 } 7 } 8 int main() { 9 int a[3]; 10 f(a); 11 std::cout << a[2] << std::endl; //output is 2 12 } Slide 20 of 24

5.3. Static Function Variables Initialized upon first entry to the function Usually stored in global data segment 1 #include <iostream> 2 void f() { 3 static int count = 0; 4 int index = 0; 5 std::cout << ++count << ++index << std::endl; 6 } 7 int main() { 8 f(); 9 f(); 10 } ************ output **************** 11 21 Slide 21 of 24

5.4. Default Parameter Values If no value is passed to formal parameter, a default value is assigned, left to right. Thus, x, on line 2, is assigned the ascii code for A, which is 65, on line 7: 1 #include <iostream> 2 void f(int x = 0, char ch = Z ) { 3 std::cout << x << ", " << ch << std::endl; 4 } 5 int main() { 6 f(17, B ); 7 f( A ); 8 f(); 9 } ************ output **************** 17, B 65, Z 0, Z Slide 22 of 24

5.5. Function Overload Two functions with same name but different parameter types The function return value cannot be used to resolve overload #include <iostream> void write(double x) { std::cout << "x is " << x << std::endl; } void write(int i) { std::cout << "i is " << i << std::endl; } int main() { double x = 2.5; write(7); // output: i is 7 write(x); // output: x is 2.5 } Slide 23 of 24

5.6. Command Line Parameters You can pass values into main argc is number of parameters passed; argv is an array of C strings containing the values There s always at least one parameter passed: the name of the executable 1 #include <iostream> 2 int main(int argc, char* argv[]) { 3 for (int i = 0; i < argc; ++i) { 4 std::cout << argv[i] << \t ; 5 } 6 std::cout << std::endl; 7 } ************ invocation ************ $./a.out 2 4 cat ************ output ****************./a.out 2 4 cat Slide 24 of 24