From C to C++.

Slides:



Advertisements
Similar presentations
Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Advertisements

Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Unix Continuum of Tools Do something once: use the command line Do something many times: –Use an alias –Use a shell script Do something that is complex.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles C/C++ Emery Berger and Mark Corner University of Massachusetts.
Guide To UNIX Using Linux Third Edition
. Memory Management. Memory Organization u During run time, variables can be stored in one of three “pools”  Stack  Static heap  Dynamic heap.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.
Working with Strings Lecture 2 Hartmut Kaiser
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
Object Oriented Programming Elhanan Borenstein copyrights © Elhanan Borenstein.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan Snd Term Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
Introduction to Programming
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Welcome to Data Structures in C++. Course Staff 2 Teacher : Ofir Pele TA: Teachers of other groups: Roman Yavich.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
From C to C++. 2 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.
Enum. enum – a new type 2 enum is a set of constant int values, that defines a type: enum Season { WINTER,// = 0 by default SPRING,// = WINTER + 1 SUMMER,//
Chapter 15 - C++ As A "Better C"
CSE 374 Programming Concepts & Tools
Chapter 1.2 Introduction to C++ Programming
CSE 374 Programming Concepts & Tools
Great way to learn is by example so fire up Visual Studios C (at home make sure you custom install with C++ - no longer default) by Deborah R. Fowler.
Introduction to C++ (Extensions to C)
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
User-Written Functions
The Machine Model Memory
Computer Science 210 Computer Organization
Chapter 1.2 Introduction to C++ Programming
A bit of C programming Lecture 3 Uli Raich.
Introduction to Python
Introduction to C++ Systems Programming.
A Lecture for the c++ Course
Working with Strings Lecture 2 Hartmut Kaiser
Topic: Functions – Part 2
From C to C++.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
(5 - 1) Object-Oriented Programming (OOP) and C++
Computer Science 210 Computer Organization
Lecture 11 C Parameters Richard Gesick.
Working with Strings Lecture 2 Hartmut Kaiser
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Govt. Polytechnic,Dhangar
Your first C and C++ programs
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
(5 - 1) Object-Oriented Programming (OOP) and C++
Let’s start from the beginning
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Java 5 New Features 1-May-19.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
CISC101 Reminders Assignment 3 due today.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Enum.
Corresponds with Chapter 5
Functions that return a value
Presentation transcript:

From C to C++

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

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

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

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

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

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. Some in the lecture, some in the Tirgul

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; } << operator – overload of left shift operator because of visual similarity to "pushing" (with some image nation…) std::cout as a global object. Type checking (does not exist in scanf / printf).

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 hello.cpp –o hello > hello Hello class! >

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

strings in C++ #include <iostream> #include <string> int main() {    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; } More about string functions: http://www.cppreference.com/cppstring Strings are *similar* but not the same as java strings. They are not part of the language but part of the standard library

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";    } }

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

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

Use enum to eliminate magic numbers – alternative to #define

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!

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

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

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

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() How does the compiler solve this in the object file? Name mangeling void Foo() becomes for example foo@void and foo(int) may become foo@I32

Default parameters #include <iostream> void foo(int n=5) {    std::cout << n; } int main() {       foo(); } Output: Compile, and print: foo(5) How does the compiler solve this in the object file? Name mangeling void Foo() becomes for example foo@void and foo(int) may become foo@I32

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