Ch. 3: The C in C++ - Continued -

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

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Tokens, Expressions and Control Structures

BLM2031 Structured Programming. Zeyneb KURT

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

A Fast Review of C Essentials Part I

QUIZ. What is wrong with this code that uses default arguments?

C & Data Structures syllabus


QUIZ. How could we disable the automatic creation of copyconstructors

The Foundation of C++: The C Subset An Overview of C p. 3 The Origins and History of C p. 4 C Is a Middle-Level Language p. 5 C Is a Structured

QUIZ. Source:


QUIZ. Can you find 5 errors in this code?

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

QUIZ. How could we disable the automatic creation of copyconstructors

Review of the C Programming Language for Principles of Operating Systems

Declaration Syntax. Declarations. Declarators. Declaration Specifiers. Declaration Examples. Declaration Examples. Declarators include:

Lecture 3: C Programm

Computer Science 306 Study Guide

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

Ch. 10: Name Control

QUIZ. What are 3 differences between C and C++ const variables?

Review of the C Programming Language

Page 1. Stuff. Last Time. Today. Safety-Critical Systems MISRA-C. Terminology. Interrupts Inline assembly Intrinsics

6.096 Introduction to C++ January (IAP) 2009

EL6483: Brief Overview of C Programming Language

Welcome to Teach Yourself Acknowledgments Fundamental C++ Programming p. 2 An Introduction to C++ p. 4 A Brief History of C++ p.

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

Ch. 11: References & the Copy-Constructor. - continued -

CS3157: Advanced Programming. Outline

CS201 Latest Solved MCQs

C Programming SYLLABUS COVERAGE SYLLABUS IN DETAILS

[0569] p 0318 garbage

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

Short Notes of CS201

CS107 Handout 13 Spring 2008 April 18, 2008 Computer Architecture: Take II

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

CS201 - Introduction to Programming Glossary By

IS 0020 Program Design and Software Tools

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Basic Types, Variables, Literals, Constants

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Variables. Data Types.

Lectures 5-6: Introduction to C

CS Programming In C

Model Viva Questions for Programming in C lab

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

September 10,

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Fixed-Point Math and Other Optimizations

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

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

Contents Lecture 3. C Preprocessor, Chapter 11 Declarations, Chapter 8. Jonas Skeppstedt Lecture / 44

Absolute C++ Walter Savitch

Important From Last Time

CS 361 Computer Systems Fall 2017 Homework Assignment 1 Linking - From Source Code to Executable Binary

Hacking in C. Pointers. Radboud University, Nijmegen, The Netherlands. Spring 2019

OBJECT ORIENTED PROGRAMMING USING C++

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

Midterm 2. 7] Explain in your own words the concept of a handle class and how it s implemented in C++: What s wrong with this answer?

Special Topics for Embedded Programming

CS11 Advanced C++ Fall Lecture 7

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Data Structures Unit 02

CODE TIME TECHNOLOGIES. Abassi RTOS MISRA-C:2004. Compliance Report

Programming in C and C++

Computers Programming Course 5. Iulian Năstac

IBM i Version 7.2. Programming IBM Rational Development Studio for i ILE C/C++ Language Reference IBM SC

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

Introduction to Programming

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

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

CE221 Programming in C++ Part 1 Introduction

Chapter 2: Basic Elements of C++

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

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

Contents of Lecture 3

C++ Undefined Behavior What is it, and why should I care?

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science

A Fast Review of C Essentials Part II

Where do we stand on inheritance?

C++ for Java Programmers

Programming, numerics and optimization

Ch. 12: Operator Overloading

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

EEE145 Computer Programming

Quiz 0 Answer Key. Answers other than the below may be possible. Multiple Choice. 0. a 1. a 2. b 3. c 4. b 5. d. True or False.

IBM i Version 7.3. Programming IBM Rational Development Studio for i ILE C/C++ Language Reference IBM SC

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Introduction to C Final Review Chapters 1-6 & 13

C++ Coding Standards and Practices. Tim Beaudet March 23rd 2015

This wouldn t work without the previous declaration of X. This wouldn t work without the previous declaration of y

Come and join us at WebLyceum

Transcription:

Ch. 3: The C in C++ - Continued -

QUIZ What are the 3 ways a reference can be passed to a C++ function?

QUIZ True or false: References behave like constant pointers with automatic dereferencing.

QUIZ What are 3 differences between references and pointers?

Someone says: The very example used in the text to illustrate references shows that a reference does have an address!

What s wrong with this swap() function? What will be printed?

Specifying storage allocation

Ignore the author s special comments any decent IDE will offer better ways to manage separate compilation and linking.

Static illustrates the difference between scope and lifetime of objects!

What would the output be if static were omitted?

Can we refer to i in main() or other functions?

Can we refer to i in main() or other functions? No! Although I still exists, it is out of scope anywhere outside func()!

Types of linkage variables can have Internal (static) the name can be reused in other files External (extern) the name cannot be reused in other files No linkage, i.e. the linker does not know about these variables (auto)

Constants Both C and C++ allow preprocessor constants (although their use is discouraged in C++): Downsides: No type checking No possibility to control their scope (they have file scope) Cannot access their address

Constants Both C and C++ allow constant variables (they existed in C++ from the beginning 1983 -, but they were introduced in C only in C89): In C++, a const must always have an initialization value (in C, this is not true)

What the author means to say is that in C const variables have external linkage, whereas in C++ they have internal linkage.

Another difference between C and C++ const variables In C++ const variables must be initialized, whereas in C they can be uninitialized.

Individual work for next time: End-of-chapter problems #9 and #10. EOL 9

QUIZ If a variable is declared static in C or C++, what is its: lifetime? scope?

QUIZ If a variable is declared static in C or C++, what is its: lifetime? The lifetime of the entire program scope? The block in which it was declared. If declared global, it has file scope (i.e. it cannot be accessed in a different file!)

QUIZ What are 2 differences between C and C++ const variables?

QUIZ What are 2 differences between C and C++ const variables? In C const variables have external linkage, whereas in C++ they have internal linkage. In C const variables may be uninitialized, whereas in C++ they must be initialized.

QUIZ What is the meaing of the keyword volatile in C and C++? Why is it needed?

Operators and their use This section is largely meant for reading and reviewing. Our lecture coverage will focus on the features that are either tricky, or new in C++, compared to C.

Preprocessor macros......

Preprocessor macros Warning: Macros are considered unsafe, and their use is discouraged! They have no scope, rather they are processed by the preprocessor from top to bottom in the source code, irrespective in which blocks or functions they appear. This can have unintuitive effects. Consider using functions instead! (More details in Ch.9)

Preprocessor macros We shall not require macro programming in this class! (Actually, we re taking points off for macros )

Not a good idea to compare floating point numbers for equality!

Not a good idea to compare floating point numbers for equality! What to do?

Example application for shift operator Explain what happens in binary!

Even though most computer CPUs provide a machinelevel rotate command (so you ll see it in the assembly language for that processor), there is no direct support for rotate in C or C++. Presumably the designers of C felt justified in leaving rotate off (aiming, as they said, for a minimal language) because you can build your own rotate command.

Explain what happens in binary!

Casting operators The compiler will automatically change one type of data into another if it makes sense. For instance, if you assign an integral value to a floating-point variable, the compiler will secretly call a function (or more probably, insert code) to convert the int to a float. Casting allows you to make this type conversion explicit, or to force it when it wouldn t normally happen.

C++ explicit casts

static_cast The author s machine has 16-bit int

const_cast

reinterpret_cast

...

Composite type creation This section is also largely meant for reading and reviewing. Our lecture coverage will focus on the features that are either tricky, or new in C++, compared to C.

Aliasing names with typedef Starting with C++11, the keyword using is a better alternative to typedef!

What is the problem with typedef? A small but annoying practical problem: typedef s syntax is in reverse from define s

What is the problem with typedef? In the greater scheme of C++ things, using allows template typedef (later), so it is more general.

QUIZ: Rewrite with using instead of typedef Optional (and misleading in this example, since the new type is also called Structure3)

QUIZ: Rewrite with using instead of typedef

The following topics will be covered in the lab: Arguments of main(): argv[] and argc Complicated declarations & definitions

SKIP: Debugging hints Arrays of pointers to functions Make: managing separate compilation

The remainder of Ch.3 is assigned for reading and reviewing: Structures Unions Arrays (and pointers, pointer arithmetic) Enumerations Function addresses and function pointers

Homework for ch. 3 Provided as separate handout (also available on our webpage --> agapie.net) Due Friday, Sept. 25, at the beginning of class. Please hand in a hard-copy, do not email! EOL 10