Programmazione. Prof. Marco Bertini

Similar documents
Programmazione. Prof. Marco Bertini

AN OVERVIEW OF C++ 1

PROGRAMMING IN C++ CVIČENÍ

Object Oriented Design

Looping and Counting. Lecture 3 Hartmut Kaiser hkaiser/fall_2012/csc1254.html

Looping and Counting. Lecture 3. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

CE221 Programming in C++ Part 1 Introduction

Intermediate Programming, Spring 2017*

Fast Introduction to Object Oriented Programming and C++

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Introduction to C++ Introduction to C++ 1

Working with Strings. Lecture 2. Hartmut Kaiser. hkaiser/spring_2015/csc1254.html

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

Introduction to C++ (Extensions to C)

Computing and Statistical Data Analysis Lecture 3

C++ Namespaces, Exceptions

CS242 COMPUTER PROGRAMMING

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

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

Intermediate Programming, Spring 2017*

Programmazione. Prof. Marco Bertini

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

PROGRAMMING IN C++ KAUSIK DATTA 18-Oct-2017

OBJECT ORIENTED PROGRAMMING USING C++

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

C++ For Science and Engineering Lecture 2

C++ basics Getting started with, and Data Types.

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

Introduction to C++ Systems Programming

Exercise 1.1 Hello world

Laboratorio di Tecnologie dell'informazione

CS11 Intro C++ Spring 2018 Lecture 1

CSE 303: Concepts and Tools for Software Development

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

Your first C++ program

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

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

Program Organization and Comments

This chapter introduces the notion of namespace. We also describe how to manage input and output with C++ commands via the terminal or files.

COMP322 - Introduction to C++

Types, Values, Variables & Assignment. EECS 211 Winter 2018

PIC 10A Objects/Classes

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

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

C++ Code Structure. Cooperating with the Compiler

Programmazione. Prof. Marco Bertini

Introduction to C++: I

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Lab 1: First Steps in C++ - Eclipse

C++ Constructor Insanity

GCC : From 2.95 to 3.2

CS 247: Software Engineering Principles. ADT Design

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

Introduction to Programming

Separate Compilation of Multi-File Programs

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

Cours de C++ Introduction

Programming with C++ as a Second Language

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

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

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

Fundamentals of Programming. Lecture 19 Hamed Rasifard

Lecture 7. Log into Linux New documents posted to course webpage

Module 7 b. -Namespaces -Exceptions handling

Binomial pricer (1.1)

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

Chapter 2. Procedural Programming

Laboratorio di Tecnologie dell'informazione

CSE 333 Lecture 9 - intro to C++

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

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

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

Laboratorio di Tecnologie dell'informazione. Ing. Marco Bertini

Your first C and C++ programs

Arrays. Week 4. Assylbek Jumagaliyev

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

This watermark does not appear in the registered version - Slide 1

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

OBJECT ORIENTED PROGRAMMING

CS 376b Computer Vision

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

Functions and Recursion

10. Functions (Part 2)

Laboratorio di Tecnologie dell'informazione

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

Communication With the Outside World

4. Structure of a C++ program

Understanding main() function Input/Output Streams

Streams. Rupesh Nasre.

COEN244: Class & function templates

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

Lecture 5 Files and Streams

CS302 - Data Structures using C++

Computer Science II Lecture 2 Strings, Vectors and Recursion

Functions, Arrays & Structs

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

PHY4321 Summary Notes

C++ Lab 03 - C++ Functions

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

CS 11 C++ track: lecture 1

Transcription:

Programmazione Prof. Marco Bertini marco.bertini@unifi.it http://www.micc.unifi.it/bertini/

Hello world : a review

Some differences between C and C++ Let s review some differences between C and C++ looking at the Hello world program #include <iostream> int main() { } // let s greet std::cout << "Hello, "; std::string name = "world"; std::cout << name << std::endl;

The main function In C++ there are two variants of the function main(): int main() int main(int argc, char **argv) It is not required to use an explicit return statement at the end of main. If omitted main returns 0.

The main function In C++ there are two variants of the function main(): int main() new! int main(int argc, char **argv) It is not required to use an explicit return statement at the end of main. If omitted main returns 0.

Comments There s a new (compact) comment style: Two types: C Style: /* block comment */ Usually only used for block comments at the top of programs, classes, functions etc. Can extend over multiple lines C++ style: // comment to end of line Usually used for most in line comments against variables or algorithm code

Comments There s a new (compact) comment style: Two types: C Style: /* block comment */ Usually only used for block comments at the top of programs, classes, functions etc. Can extend over multiple lines C++ style: // comment to end of line new! Usually used for most in line comments against variables or algorithm code

C++ library headers In C++ all the library headers have names without the.h extension You can still use C header files with the.h extension (but you shouldn t). Instead, C++ versions of C header files have the same name with a c in front, e.g.: #include <cmath>

Variable declared as needed In C we are required to declare all variables either globally or at the top of a function before any executable code In C++ we can declare variable anywhere, but always before they are used It is better to declare variables only when we need them and as close as possible to their use. A control variable for a for loop is often declared inside the for loop, e.g. : for (int i = 0; i < 100; i++) {

Default function arguments In C++ it is possible to provide default arguments when defining a function. These arguments are supplied by the compiler when they are not specified by the programmer. Functions may be defined with more than one default argument. They are used only in function calls where trailing arguments are omitted they must be the last argument(s). Default arguments must be known at compile-time since at that moment arguments are supplied to functions. Therefore, the default arguments must be mentioned at the function's declaration, rather than at its implementation.

Default function arguments - cont. Example: // sample header file void three_ints(int a, int b=4, int c=3); // code of function in.cpp file void three_ints(int a, int b, int c) { }...

Type-safe C++ Stream I/O Avoid using the type-unsafe C I/O library (printf, scanf, etc.), where you need to specify the format of the data (e.g. %d ). C++ overloads the right-shift operator (>>) for input and the left-shift operator (<<) for output. The target of the I/O is a stream, eg. cin (standard input), cout (standard output) or a file stream.

Operator overloading We ll come back on this later but notice: in C++ it is possible to define functions and operators having identical names but performing different actions. The functions must differ in their parameter lists (and/or in their const attribute). In this example we used overloaded << and >>, that are otherwise bit shift operators.

C++ Style Strings Type safe, space-safe! Contrast with C style nul terminated strings Can input or manipulate C++ strings without worrying about overrunning the available memory - the string will expand as required! But the only way to easily declare a constant string or string literal is as a C-style nul terminated string., e.g. : std::string s = "world";

Namespace A facility for partitioning names (of types, variables, functions etc.) Allows large programs to be built from various components without risk of name clashes. A namespace is essentially the ability of the compiler to keep names of functions, variables and type, in separate groupings so that names in different groups can be the same without clashing with each other. C++ Standard Library has names in a namespace called std as in std::cout

Qualified names A name can be qualified, using the :: (scope resolution) operator, with the namespace in which it has been declared. For example, std::cin refers to the name cin from the namespace std. the operator :: resolves the scope of the variable/method we are selecting: we are looking for the one of the class whose name is to its left

using directive If a name is used frequently within a segment of code you can place a using declaration into the source file, e.g. using std::cin; would say that all the cins refer to the one from the namespace std. To refer to any of the names in a namespace as if they were declared globally, you can place a using directive into the source file, e.g.: using namespace std; brings all the names from the namespace std into use.

Using C functions in C++ Name mangling (sometimes called name decoration) is a technique used to solve various problems caused by the need to resolve unique names for programming entities in many modern programming languages. It provides a way of encoding additional information in the name of a function, structure, class or another datatype in order to pass more semantic information from the compilers to linkers. C++ compilers use it, C compilers do not. As C language definitions are unmangled, the C++ compiler needs to avoid mangling references to these identifiers.

Using C functions in C++ - cont. To use a C function in a C++ program we need to declare it as... C function, e.g.: extern "C" void *xmalloc(int size); or: extern "C"{ } or: // C-declarations go in here void *xmalloc(int size); extern "C"{ } #include <xmalloc.h> // contains declaration of xmalloc

Using C functions in C++ - cont. Another common solution is to exploit the cplusplus symbol defined by the C++ compilers: // This is xmalloc.h #ifdef cplusplus extern "C" { #endif /* declaration of C-data and functions are inserted here. */ void *xmalloc(int size); #ifdef cplusplus } #endif

Credits These slides are (heavily) based on the material of Dr. Ian Richards, CSC2402, Univ. of Southern Queensland