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.

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

C Language.
What's new in Microsoft Visual C Preview
Introduction to ML – Part 1 Frances Spalding. Assignment 1 chive/fall05/cos441/assignments/a1.ht m
CS 4800 By Brandon Andrews.  Specifications  Goals  Applications  Design Steps  Testing.
Chapter 10.
CS414 C Programming Tutorial Ben Atkin
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.
Plab - administration TA’s:TA’s: –Yoseph Barash –Liad Blumrosen –Michael Okun Contact ONLY ONLY website:
Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.
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.
COMP2004 Programming Practice Sam Holden Department of Computer Science University of Sydney.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
Introduction to C++CS-2303, C-Term Introduction to C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles C/C++ Emery Berger and Mark Corner University of Massachusetts.
. Memory Management. Memory Organization u During run time, variables can be stored in one of three “pools”  Stack  Static heap  Dynamic heap.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
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.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
Chapter 1 Working with strings. Objectives Understand simple programs using character strings and the string library. Get acquainted with declarations,
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:
Applied Computing Technology Laboratory QuickStart C# Learning to Program in C# Amy Roberge & John Linehan November 7, 2005.
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.
Working with Batches of Data Lecture 4 Hartmut Kaiser
Copyright © Curt Hill Structured Data What this course is about.
Introduction to Programming
1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
1 Review. 2 Creating a Runnable Program  What is the function of the compiler?  What is the function of the linker?  Java doesn't have a linker. If.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
Object Oriented Software Development 4. C# data types, objects and references.
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Introduction to C# By: Abir Ghattas Michel Barakat.
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 C and Computer Structure #include int main() { printf ("hello class\n"); return 0; }
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.
1 Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
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"
Introduction to C++ (Extensions to C)
Computer Science 210 Computer Organization
From C to C++.
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Introduction to C++ Systems Programming.
Working with Strings Lecture 2 Hartmut Kaiser
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.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
Working with Strings Lecture 2 Hartmut Kaiser
Govt. Polytechnic,Dhangar
Your first C and C++ programs
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Java 5 New Features 1-May-19.
Enum.
Corresponds with Chapter 5
Presentation transcript:

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 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 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

3 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 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

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

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

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

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

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

Compiling & Running… 9 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! >

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

strings in C++ 11 #include #include 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:

Boolean variables 12 #include 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}; // 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}; //

enum 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 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 enum Season { WINTER,// = 0 by default SPRING,// = WINTER + 1 SUMMER,// = WINTER + 2 AUTUMN// = WINTER + 3 }; enum Season { WINTER,// = 0 by default SPRING,// = WINTER + 1 SUMMER,// = WINTER + 2 AUTUMN// = WINTER + 3 };

Use enum to eliminate magic numbers – alternative to #define 16

C++-11 enum class 17 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! class char enum class Season : char { WINTER,// = 0 by default SPRING,// = WINTER + 1 SUMMER,// = WINTER + 2 AUTUMN// = WINTER + 3 };

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

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

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

Function overloading – C++ 21 #include 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()

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

Overload resolution 23