+ All Categories
Home > Documents > From C to C++ - ariel.ac.il · 2016-03-20 · 2 Why C++ is much more fun than C (C++ FAQ)? 1....

From C to C++ - ariel.ac.il · 2016-03-20 · 2 Why C++ is much more fun than C (C++ FAQ)? 1....

Date post: 13-Jul-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
23
From C to C++
Transcript
Page 1: From C to C++ - ariel.ac.il · 2016-03-20 · 2 Why C++ is much more fun than C (C++ FAQ)? 1. Classes & methods - OO design 2. Generic programming - Templates allow for code reuse

From C to C++

Page 2: From C to C++ - ariel.ac.il · 2016-03-20 · 2 Why C++ is much more fun than C (C++ FAQ)? 1. Classes & methods - OO design 2. Generic programming - Templates allow for code reuse

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

Page 3: From C to C++ - ariel.ac.il · 2016-03-20 · 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

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

Page 4: From C to C++ - ariel.ac.il · 2016-03-20 · 2 Why C++ is much more fun than C (C++ FAQ)? 1. Classes & methods - OO design 2. Generic programming - Templates allow for code reuse

History

4

The C++ Prog. Manual (85-

90) C++98 C++03 C++11 C++14

Default in

g++

Minor

changes

auto,

lambdas,

threading,

Auto

return

type,

generic

lambdas,

binary

literals,…

Minor

changes

Major

changes

Page 5: From C to C++ - ariel.ac.il · 2016-03-20 · 2 Why C++ is much more fun than C (C++ FAQ)? 1. Classes & methods - OO design 2. Generic programming - Templates allow for code reuse

History

5

The C++ Prog. Manual (85-

90) C++98 C++03 C++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

Page 6: From C to C++ - ariel.ac.il · 2016-03-20 · 2 Why C++ is much more fun than C (C++ FAQ)? 1. Classes & methods - OO design 2. Generic programming - Templates allow for code reuse

Course Objectives

6

1. Learn the language

2. Practice of programming data structures

Design

Testing & Debugging

Efficiency & Portability

Modularity

Page 7: From C to C++ - ariel.ac.il · 2016-03-20 · 2 Why C++ is much more fun than C (C++ FAQ)? 1. Classes & methods - OO design 2. Generic programming - Templates allow for code reuse

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

Page 8: From C to C++ - ariel.ac.il · 2016-03-20 · 2 Why C++ is much more fun than C (C++ FAQ)? 1. Classes & methods - OO design 2. Generic programming - Templates allow for code reuse

First Program in C++

8

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

}

Page 9: From C to C++ - ariel.ac.il · 2016-03-20 · 2 Why C++ is much more fun than C (C++ FAQ)? 1. Classes & methods - OO design 2. Generic programming - Templates allow for code reuse

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

> hello

Hello class!

>

Page 10: From C to C++ - ariel.ac.il · 2016-03-20 · 2 Why C++ is much more fun than C (C++ FAQ)? 1. Classes & methods - OO design 2. Generic programming - Templates allow for code reuse

Fill in missing types from C, in somewhat

crude way

10

The missing types

Page 11: From C to C++ - ariel.ac.il · 2016-03-20 · 2 Why C++ is much more fun than C (C++ FAQ)? 1. Classes & methods - OO design 2. Generic programming - Templates allow for code reuse

strings in C++

11

#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

Page 12: From C to C++ - ariel.ac.il · 2016-03-20 · 2 Why C++ is much more fun than C (C++ FAQ)? 1. Classes & methods - OO design 2. Generic programming - Templates allow for code reuse

Boolean variables

12

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

}

}

Page 13: From C to C++ - ariel.ac.il · 2016-03-20 · 2 Why C++ is much more fun than C (C++ FAQ)? 1. Classes & methods - OO design 2. Generic programming - Templates allow for code reuse

Enum (C)

Page 14: From C to C++ - ariel.ac.il · 2016-03-20 · 2 Why C++ is much more fun than C (C++ FAQ)? 1. Classes & methods - OO design 2. Generic programming - Templates allow for code reuse

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

Page 15: From C to C++ - ariel.ac.il · 2016-03-20 · 2 Why C++ is much more fun than C (C++ FAQ)? 1. Classes & methods - OO design 2. Generic programming - Templates allow for code reuse

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

15

enum Season {

WINTER, // = 0 by default

SPRING, // = WINTER + 1

SUMMER, // = WINTER + 2

AUTUMN // = WINTER + 3

};

Page 16: From C to C++ - ariel.ac.il · 2016-03-20 · 2 Why C++ is much more fun than C (C++ FAQ)? 1. Classes & methods - OO design 2. Generic programming - Templates allow for code reuse

Use enum to eliminate magic

numbers – alternative to #define

#include <stdio.h>

#include <stdlib.h>

enum{INPUT_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

Page 17: From C to C++ - ariel.ac.il · 2016-03-20 · 2 Why C++ is much more fun than C (C++ FAQ)? 1. Classes & methods - OO design 2. Generic programming - Templates allow for code reuse

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!

enum class Season : char {

WINTER, // = 0 by default

SPRING, // = WINTER + 1

SUMMER, // = WINTER + 2

AUTUMN // = WINTER + 3

};

Page 18: From C to C++ - ariel.ac.il · 2016-03-20 · 2 Why C++ is much more fun than C (C++ FAQ)? 1. Classes & methods - OO design 2. Generic programming - Templates allow for code reuse

enums – why?

More readable code

Code less error prone

Accessible for debugger

Use of the numerical values is not disabled

bad programming usually!

18

Page 19: From C to C++ - ariel.ac.il · 2016-03-20 · 2 Why C++ is much more fun than C (C++ FAQ)? 1. Classes & methods - OO design 2. Generic programming - Templates allow for code reuse

Understand and remember.

• More than syntactic sugar.

• This is how a lot of stuff works under

the hood (e.g. inheritance)

19

Overloading

Page 20: From C to C++ - ariel.ac.il · 2016-03-20 · 2 Why C++ is much more fun than C (C++ FAQ)? 1. Classes & methods - OO design 2. Generic programming - Templates allow for code reuse

#include <stdio.h> 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

Page 21: From C to C++ - ariel.ac.il · 2016-03-20 · 2 Why C++ is much more fun than C (C++ FAQ)? 1. Classes & methods - OO design 2. Generic programming - Templates allow for code reuse

Function overloading – C++

21

#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()

Page 22: From C to C++ - ariel.ac.il · 2016-03-20 · 2 Why C++ is much more fun than C (C++ FAQ)? 1. Classes & methods - OO design 2. Generic programming - Templates allow for code reuse

Default parameters

22

#include <iostream> void foo(int n=5) { std::cout << n; } int main() { foo(); }

Output:

Compile, and print: foo(5)

Page 23: From C to C++ - ariel.ac.il · 2016-03-20 · 2 Why C++ is much more fun than C (C++ FAQ)? 1. Classes & methods - OO design 2. Generic programming - Templates allow for code reuse

Overload resolution

23


Recommended