Informatyka Wy-08 Klasy mgr inż. Krzysztof Kołodziejczyk krzysztof.m.kolodziejczyk@pwr.edu.pl 17.04.2019
Strona kursu http://w12.pwr.wroc.pl/inf/
Konsultacje Piątek 13:00 C2/111a
Konspekt 1 Struktura a klasa 2 Klasy 3 Zarządzanie pamięcią 4 Klasa a obiekt 5 C a C++
Konspekt 1 Struktura a klasa 2 Klasy 3 Zarządzanie pamięcią 4 Klasa a obiekt 5 C a C++
Struktura typedef s t r u c t number_s { char type ; union { i n t i ; f l o a t f ; value ; Number ; Number a r r [ 3 ] ; a r r [ 0 ]. type = i ; a r r [ 0 ]. value. i = 1 0 ; a r r [ 1 ]. type = i ; a r r [ 1 ]. value. i = 2 0 ; a r r [ 2 ]. type = f ; a r r [ 2 ]. value. f = 2. 0 ;
Struktura typedef s t r u c t number_s { char type ; union { i n t i ; f l o a t f ; value ; Number ; f o r ( i =0; i <3; i ++){ i f ( i == a r r [ i ]. type ){ p r i n t f ( " i n t e g e r = %d\n", a r r [ i ]. value. i ) ; e l s e i f ( f == a r r [ i ]. type ){ p r i n t f ( " f l o a t = %f \n", a r r [ i ]. value. f ) ;
Struktura #i n c l u d e <s t d i o. h> union f i { i n t i ; f l o a t f ; ; void print_i ( union f i value ){ p r i n t f ( " i n t e g e r = %d\n", value. i ) ; void print_f ( union f i value ){ p r i n t f ( " f l o a t = %f \n", value. f ) ; typedef s t r u c t t e s t { union f i value ; void ( p r i n t ) ( union f i ) ; Test ;
Struktura i n t main ( void ){ Test a r r [ 3 ] ; i n t i ; a r r [ 0 ]. value. i =3; a r r [ 0 ]. p r i n t=&print_i ; a r r [ 1 ]. value. f =2.2; a r r [ 1 ]. p r i n t=&print_f ; a r r [ 2 ]. value. i =7; a r r [ 2 ]. p r i n t=&print_i ; f o r ( i =0; i <3; i ++){ ( a r r [ i ]. p r i n t ) ( a r r [ i ]. value ) ;
Klasa c l a s s Rectangle { p r i v a t e : p u b l i c : double width, h e i g h t ; double get_area ( void ) { return width h e i g h t ; double get_perimeter ( void ) { return 2 width + 2 h e i g h t ; ;
Konspekt 1 Struktura a klasa 2 Klasy 3 Zarządzanie pamięcią 4 Klasa a obiekt 5 C a C++
Klasy
Klasa c l a s s R e ctangle t y p e d e f s t r u c t r e c t a n g l e _ s { { p u b l i c : double width, height ; double width, height ; R ectangle double get_area ( void ) double c a l c _ a r e a ( R e c t a n g l e r ) { { return width height ; r e t u r n r. width r. h e i g h t ; double get_perimeter ( void ) double c a l c _ p e r i m e t e r ( R e c t a n g l e r ) { { return 2 width + 2 height ; r e t u r n 2 r. width + 2 r. h e i g h t ; ;
Wielbład czy wąż? camelcasename PascalCaseName snake_case_name kebab-case-name
Wielbład czy wąż? camelcasename PascalCaseName snake_case_name kebab-case-name l337_3nc0d3d_n4m3
Konspekt 1 Struktura a klasa 2 Klasy 3 Zarządzanie pamięcią Porównanie do struktury Konstruktor, destruktor Zwalnianie pamięci 4 Klasa a obiekt 5 C a C++
Alokacja pamięci (struktury) Rectangle r e c t ; double area ; r e c t. width = 2 ; r e c t. height = 3 ; area = calc_area ( r e c t ) ; brak wartości domyślnych wymagana szczególowa wiedza
Alokacja pamięci (klasy) c l a s s Student { s t r i n g imie ; s t r i n g nazwisko ; i n t index ; ; Student stud ; stud. imie = "Jan" ; stud. nazwisko = " Kowalski " ; stud. index = 123456;
Konstruktor c l a s s Rectangle { p u b l i c : Rectangle ( i n t width, i n t h e i g h t ) { t h i s >width = width ; t h i s >h eight = h e i g h t ; ; i n t main ( void ){ Rectangle r e c t ( 2, 3 ) ; r e c t. get_area ( ) ; // Rectangle r e c t ; // Rectangle r e c t = Rectangle ( 2, 3 ) ; // Rectangle ( 2, 3 ). get_area ( ) ;
Konstruktor c l a s s Student { s t r i n g imie, nazwisko ; char p l e c ; p u b l i c : Student ( s t r i n g imie, s t r i n g nazwisko ) { t h i s >imie = imie ; t h i s >nazwisko = nazwisko ; p l e c = imie. back ( ) == a? k : m ;
Konstruktor c l a s s Student { s t r i n g imie, nazwisko ; char p l e c ; p u b l i c : Student ( s t r i n g imie, s t r i n g nazwisko ) { t h i s >imie = imie ; t h i s >nazwisko = nazwisko ; p l e c = imie. back ( ) == a? k : m ; Student("Barnaba","Nowak");
new Student s1 ; Student s2 ; s2 = new Student ( "Jan", " Kowalski " ) ; // s2 = malloc ( s i z e o f ( Student ) ) // s2. imie="jan " ; // s2. nazwisko="kowalski " ;
new a malloc 1 alokuje pamięć 2 malloc jest funkcją 3 wymaga podania rozmiaru danych 4 zwraca void* 5 w przypadku błędu zwraca NULL 1 po zaalokowaniu pamięci uruchamia konstruktor 2 new jest operatorem 3 określa wielkość danych na podstawie klasy 4 zwraca wskaźnik na konkretną klasę 5 w przypadku błędu rzuca wyjątek
new Student rok1 [ 2 0 0 ] ; Student rok3 ; rok3 = new Student [ 4 0 ] ;
delete Student rok1 [ 2 0 0 ] ; Student rok3 ; Student bob = new Student ( "Jan", "Bober" ) ; rok3 = new Student [ 4 0 ] ; d e l e t e bob ; d e l e t e [ ] rok3 ;
Wyjątki //#i n c l u d e <exception > try { rok3 = new Student [ 4 0 ] ; catch ( e x c e p t i o n& e ) { cout << " Exception : " << e. what ( ) << endl ;
Zwalnianie pamięci (struktury) typedef student_s { char imie ; char nazwisko ; i n t index ; Student ; void func ( void ){ Student bob ; s c a n f ( "%ms",&bob. imie ) ; s c a n f ( "%ms",&bob. nazwisko ) ;...
Zwalnianie pamięci (klasy) c l a s s Student { s t r i n g imie, nazwisko ; char p l e c ;
Destruktor c l a s s Grupa { Student s t u d e n c i ; Grupa ( i n t n ){ s t u d e n c i = new Student [ n ] ; ; Grupa rok3 = new Grupa ( 4 0 ) ; d e l e t e rok3 ;
Destruktor c l a s s Grupa { Student s t u d e n c i ; Grupa ( i n t n ){ s t u d e n c i = new Student [ n ] ; ~Grupa ( ) { d e l e t e [ ] s t u d e n c i ; ; Grupa rok3 = new Grupa ( 4 0 ) ; d e l e t e rok3 ;
Konspekt 1 Struktura a klasa 2 Klasy 3 Zarządzanie pamięcią 4 Klasa a obiekt 5 C a C++
Klasa a obiekt Obiekt jest instancją (wystąpieniem) klasy.
Klasa a obiekt Obiekt jest instancją (wystąpieniem) klasy. c l a s s Rectangle { p u b l i c : double a, b ; Rectangle r e c t 1 ; Rectangle r e c t 2 ; r e c t 2 = new Rectangle ( ) ;
Konspekt 1 Struktura a klasa 2 Klasy 3 Zarządzanie pamięcią 4 Klasa a obiekt 5 C a C++
C a C++ #i n c l u d e <s t d i o. h> i n t main ( i n t argc, char argv ) { p r i n t f ( " Helo wrld! \ n" ) ; r eturn 0 ; #i n c l u d e <iostream> using namespace std ; i n t main ( i n t argc, char argv ) { cout<<" H ello world! "<<endl ; r eturn 0 ;
C a C++ cout << setw ( 9 ) << 3.25 << endl ; cout << boolalpha ; cout << true << endl ; cout << noboolalpha ; cout << hex << 127 << endl ; cout << showbase ;
iostream #i n c l u d e <b i t s / c++c o n f i g. h> #i n c l u d e <ostream> #i n c l u d e <istream > namespace std _GLIBCXX_VISIBILITY( d e f a u l t ) { extern istream c i n ; extern ostream cout ; extern ostream c e r r ; extern ostream c l o g ;
Standardy C++ 1990 ANSI C++ 1991 ISO C++ 1992 STL 1998 C++98 2003 C++03 2011 C++11 (!) 2014 C++14 2017 C++17 2020 C++20
Workflow
Standardy C++ g++ -std=gnu++14 program.cpp g++ -std=gnu++98 program.cpp
Standardy C++ g++ -std=gnu++14 program.cpp g++ -std=gnu++98 program.cpp g++: error: unrecognized command line option -std=gnu++99 ; did you mean -std=gnu++98?
Referencje void a s s i g n ( Student &student ) { t h i s >students. push_back ( student ) ;
Referencje namespace ns1 { i n t x = 1 ; i n t x = 2 ; i n t main ( ) { i n t x = 3 ; cout << ns1 : : x << endl ; cout << x << endl ; cout << : : x << endl ; return 0 ;
C a C++ C++ jest w większości zgodne z C nagłówki przestrzenie nazw cin/cout zamiast scanf/printf new/delete zamiast malloc/free programowanie obiektowe zamiast funkcyjne
Bibliografia http://www.cplusplus.com/reference/new/operatornew[]/ http://www.cplusplus.com/reference/string/string/ https://xkcd.com/1172/ https://en.cppreference.com/w/cpp/language/history
Koniec Dziękuję za uwagę!