Download presentation
Presentation is loading. Please wait.
1
From C to C++
2
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
3
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
4
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
5
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
6
Course Objectives Learn the language
Practice of programming data structures Design Testing & Debugging Efficiency & Portability Modularity
7
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
8
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).
9
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! >
10
Fill in missing types from C, in somewhat crude way
The missing types Fill in missing types from C, in somewhat crude way
11
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: Strings are *similar* but not the same as java strings. They are not part of the language but part of the standard library
12
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"; } }
13
Enum (C)
14
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 AUTUMN // = WINTER + 3 }; enum {SUNDAY=1, MONDAY, TUESDAY, …}; // nameless enum Color {BLACK,RED,GREEN,YELLOW,BLUE,WHITE=7,GRAY}; //
15
enum enum Season { WINTER, // = 0 by default SPRING, // = WINTER + 1
SUMMER, // = WINTER 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
16
Use enum to eliminate magic numbers – alternative to #define
17
C++-11 enum class enum class Season : char { WINTER, // = 0 by default
SPRING, // = WINTER + 1 SUMMER, // = WINTER 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!
18
enums – why? More readable code Code less error prone
Accessible for debugger Use of the numerical values is not disabled bad programming usually!
19
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)
20
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
21
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 and foo(int) may become
22
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 and foo(int) may become
23
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.