Why C++ is much more fun than C (C++ FAQ)?

Similar documents
Fast Introduction to Object Oriented Programming and C++

Bristol Institute of Technology

Starting to Program in C++ (Basics & I/O)

Bell Bjarne Stroustrup C C with classes Stroustrup C C with classes

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CS11 Intro C++ Spring 2018 Lecture 1

CSE 303: Concepts and Tools for Software Development

Cours de C++ Introduction

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

A brief introduction to C programming for Java programmers

Direct Memory Access. Lecture 2 Pointer Revision Command Line Arguments. What happens when we use pointers. Same again with pictures

CSE 333 Lecture 9 - intro to C++

Programmazione. Prof. Marco Bertini

Dynamic Memory Allocation and Command-line Arguments

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

CS349/SE382 A1 C Programming Tutorial

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

CE221 Programming in C++ Part 1 Introduction

GCC : From 2.95 to 3.2

Programming Language Basics

PROGRAMMING IN C++ CVIČENÍ

Intermediate Programming, Spring 2017*

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

CSE 333 Lecture 7 - final C details

Topic 6: A Quick Intro To C

Arguments of C++ Applications g++ options Libraries

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

CS3157: Advanced Programming. Outline

EMBEDDED SYSTEMS PROGRAMMING Language Basics

Array Initialization

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

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

Project 1: How to Make One Dollar

Lecture 2, September 4

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

CSE 303: Concepts and Tools for Software Development

AMCAT Automata Coding Sample Questions And Answers

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

Introduction to C++ Introduction to C++ 1

Lecture 03 Bits, Bytes and Data Types

Binghamton University. CS-211 Fall Variable Scope

CAAM 420 Daily Note. Scriber: Qijia Jiang. Date: Oct.16. Project 3 Due Wed 23.Oct. Two parts: debug code and library exercise.

Topic 5: Enumerated Types and Switch Statements

just a ((somewhat) safer) dialect.

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

Binghamton University. CS-211 Fall Variable Scope

C++ Tutorial AM 225. Dan Fortunato

Binghamton University. CS-211 Fall Variable Scope

High Performance Programming Programming in C part 1

Structuur van Computerprogramma s 2

Homework 4. Any questions?

Final C Details. CSE 333 Autumn 2018

A Tour of the C++ Programming Language

Multimedia-Programmierung Übung 3

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

Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang

PRINCIPLES OF OPERATING SYSTEMS

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

COMP 2355 Introduction to Systems Programming

Threaded Programming. Lecture 9: Alternatives to OpenMP

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

Modifiers. int foo(int x) { static int y=0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; }

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

Summary of Last Class. Processes. C vs. Java. C vs. Java (cont.) C vs. Java (cont.) Tevfik Ko!ar. CSC Systems Programming Fall 2008

6.096 Introduction to C++ January (IAP) 2009

Lecture 5 Files and Streams

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

PIC 10A Objects/Classes

Bruce Merry. IOI Training Dec 2013

Topics. CS429: Computer Organization and Architecture. File Inclusion. A Simple C Program. Intro to C

CpSc 1010, Fall 2014 Lab 10: Command-Line Parameters (Week of 10/27/2014)

September 10,

Java is an objet-oriented programming language providing features that support

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

CSE 333 Lecture 2 Memory

Lecture 07 Debugging Programs with GDB

CMSC 330: Organization of Programming Languages

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

6.S096 Lecture 4 Style and Structure

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

C++11: 10 Features You Should be Using. Gordon R&D Runtime Engineer Codeplay Software Ltd.

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

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

Programming. Projects with Multiple Files

Intermediate Programming, Spring 2017*

A Tour of the C++ Programming Language

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

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

C for C++ Programmers

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Itron Riva Dev Software Development Getting Started Guide

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

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

CS429: Computer Organization and Architecture

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science

CSE 333 Midterm Exam July 24, Name UW ID#

The C language. Introductory course #1

Transcription:

From C to C++

Why C++ is much more fun than C (C++ FAQ)? 1. Classes & methods - OO design 2. Generic programming - Templates allow for code reuse 3. Stricter type system (e.g. function args) 4. Some run-time checks & memory control A common and mature language that gives you high level and low level control Have fun 2

Why C++ is much more fun than c (C+ FQA)? 1. Tons of corner cases 2. Duplicate features 3. Cryptic syntax 4. Undecidable syntax (uncompilable progarms!) 5. No two compilers agree on it Probably one of the hardest computer languages to master. Have fun 3

History The C++ Prog. Manual (85-90) Default in g++ C++98 C++03 C++11 C++14 Minor changes auto, lambdas, threading, Major changes Auto return type, generic lambdas, binary literals, Minor changes 4

History The C++ Prog. Manual (85-90) Default in g++ C++98 C++03 C++11 C++14 Minor changes auto, lambdas, threading, Major changes Auto return type, generic lambdas, binary literals, Minor changes We ll learn parts of C++-11, Mostly parts that makes C++ more pythonic while keeping it efficient 5

Course Objectives 1. Learn the language 2. Practice of programming data structures Design Testing & Debugging Efficiency & Portability Modularity 6

Course structure Overloading The basic mechanism underlying many of the extensions. Object Oriented programing The C++ version. Copying and Conversion A hidden monster Compile time polymorphism Templates - Generics++. Other topics Statics, etc. 7

First Program in C++ // This line includes the standard // I/O library file (similar to copy here this file ) #include <iostream> int main() std::cout << "Hello class!\n"; return 0; 8

Compiling & Running In this course we ll use ubuntu standalone or c9io (ubuntu on the cloud) with the gcc (gnu compiler collection): > g++ -Wall -Wvla -Werror -g -D_GLIBCXX_DEBUG -std=c++11 Wall hello.cpp o hello > hello Hello class! > 9

The missing types Fill in missing types from C, in somewhat crude way 10

11 strings in C++ #include <iostream> More about string functions: #include <string> int main() http://www.cppreference.com/cppstring std::string str; int a; double b; std::cin >> str >> a >> b; if(std::cin.fail()) std::cerr << "input problem\n"; return 1; std::cout << "I got: "<< str << ' ' << a << ' ' << b << std::endl;

Boolean variables #include <iostream> int main() int a = 5; bool iszero = (a == 0); // same conditions if(!iszero && iszero==false && iszero!=true &&!!! iszero && a ) std::cout << "a is not zero\n"; 12

Enum (C)

User Defined Type - enum Enumerated data - a set of named constants. Usage: enum [identifier]enumerator list enum Season WINTER, // = 0 by default SPRING, // = WINTER + 1 SUMMER, // = WINTER + 2 AUTUMN // = WINTER + 3 ; enum SUNDAY=1, MONDAY, TUESDAY, ; // nameless enum Color BLACK,RED,GREEN,YELLOW,BLUE,WHITE=7,GRAY; // 0 1 2 3 4 7 8 14

enum enum Season WINTER, // = 0 by default SPRING, // = WINTER + 1 SUMMER, // = WINTER + 2 AUTUMN // = WINTER + 3 ; enum Season curr_season; curr_season= AUTUMN; curr_season= 19; // legal, but ugly, g++: warning int SUMMER; // error, redefinition int prev_season = SUMMER; // legal, but ugly, g++ warning 15

Use enum to eliminate magic numbers alternative to #define #include <stdio.h> #include <stdlib.h> enuminput_file_name = 1,OUTPUT_FILE_NAME,ARGS_NUM; int main(int argc, char* argv[]) if(argc!= ARGS_NUM) printf("usage: wc <input file name> <output file name>\n"); exit(1); FILE* infile, outfile; infile = fopen(argv[input_file_name],"r"); if( infile == NULL) printf("error reading file: %s\n", argv[input_file_name]); exit(1); 16

C++-11 enum class enum class Season : char WINTER, // = 0 by default SPRING, // = WINTER + 1 SUMMER, // = WINTER + 2 AUTUMN // = WINTER + 3 ; Season curr_season; curr_season= Season::AUTUMN; curr_season= SUMMER; // won t compile! curr_season= 19; // won t compile! int prev_season= Season::SUMMER; // won t compile! 17

enums why? More readable code Code less error prone Accessible for debugger Use of the numerical values is not disabled bad programming usually! 18

Overloading Understand and remember. More than syntactic sugar. This is how a lot of stuff works under the hood (e.g. inheritance) 19

Function overloading - C #include <stdio.h> void foo() printf ("foo()\n"); void foo(int n) printf ("foo(%d)\n", n); int main() foo(12); foo(); return 0; Compilation output: Error: Multiple definition of foo 20

Function overloading C++ #include <iostream> void foo() std::cout << "foo()\n"; void foo(int n) std::cout<<"foo("<<n<<")\n"; int main() foo(12); foo(); Output: Compile, and print: foo(12) foo() 21

Default parameters #include <iostream> void foo(int n=5) std::cout << n; int main() foo(); Output: Compile, and print: foo(5) 22

Overload resolution 1. Find all functions with same name candidates. Let s call them Ω 1 2. Find Ω 2 Ω 1 which have the correct number of arguments - viable candidates 3. Find Ω 3 Ω 2 with best matching arguments. if Ω 3 = 1: use that function. else (0 or more than 1): emit compilation error. 23