- - uzupełnienie notatek: dr Jerzy Białkowski
- - 1-2 -
- - 1 #include <i o s t r e a m > 2 #include <c s t d l i b > 3 # include <cmath> 4 using namespace std ; 5 6 class Punkt { 7 8 private : 9 double x, y ; 10 11 public : 12 Punkt ( ) ; 13 Punkt ( double x, double y ) ; 14 void losuj_wsp ( double a, double b, 15 double c, double d ) ; 16 double dlugosc_wek ( ) ; 17 double getx ( ) ; 18 double gety ( ) ; 19 void setx ( double x ) ; 20 void sety ( double y ) ; 21 22 } ;
- - 23 double losuj ( ) ; 24 double losuj ( double a, double b ) ; 25 26 int main ( ) { 27 srand ( time ( 0 ) ) ; 28 29 Punkt P1, P2 ( 3, 4 ) ; 30 31 P1. losuj_wsp ( 0, 1 0, 0, 1 0 ) ; 32 33 cout << "P1=(" << P1. getx ( ) << ", " 34 << P1. gety ( ) << ")\n" ; 35 36 cout << "P2=(" << P2. getx ( ) << ", " 37 << P2. gety ( ) << ")\n" ; 38 39 // cout<< P1 << \n ; Blad 40 //P = P1 + P2?, i f ( P1==P2 )? 41 42 }// main 43 44
- - 45 Punkt : : Punkt ( ) { 46 this >x = 0 ; 47 this >y = 0 ; 48 } 49 50 Punkt : : Punkt ( double x, double y ) { 51 this >x = x ; 52 this >y = y ; 53 } 54 55 void Punkt : : losuj_wsp ( double a, double b, 56 double c, double d ) { 57 this >x = losuj ( a, b ) ; 58 this >y = losuj ( c, d ) ; 59 } 60 61 double Punkt : : dlugosc_wek ( ) { 62 return sqrt ( this >x* this >x 63 + this >y* this >y ) ; 64 } 65 66
- - 67 double Punkt : : getx ( ) { 68 return this >x ; 69 } 70 71 double Punkt : : gety ( ) { 72 return this >y ; 73 } 74 75 void Punkt : : setx ( double x ) { 76 this >x = x ; 77 } 78 79 void Punkt : : sety ( double y ) { 80 this >y = y ; 81 } 82 83 double losuj ( ) { 84 return rand ( ) /double ( RAND_MAX ) ; 85 } 86 double losuj ( double a, double b ) { 87 return ( b a ) *losuj ( ) +a ; 88 }
Argumenty i wskaźniki - - 1 #include <i o s t r e a m > 2 using namespace std ; 3 4 void fun1 ( int a, int *b, int &c ) { 5 a++; 6 (* b )++; 7 c++; 8 } 9 10 main ( ) { 11 int a=1, b=1, c=1; 12 13 fun1 ( a, &b, c ) ; 14 cout << a << " " << b << " " << c <<"\n" ; 15 16 }
Przedefiniowanie operatorów - - Można przedefiniować, np. =, ==, <<,... oprócz.,?:, sizeof, ::
Przedefiniowanie operatorów - - Można przedefiniować, np. =, ==, <<,... oprócz.,?:, sizeof, :: Deklaracja operatora == bool operator==(const Point& P, const Point& Q);
Przedefiniowanie operatorów - - Można przedefiniować, np. =, ==, <<,... oprócz.,?:, sizeof, :: Deklaracja operatora == bool operator==(const Point& P, const Point& Q); deklaracja wewnątrz klasy bool operator==(const Point& Q) const;
- - 1 #include <i o s t r e a m > 2 #include <c s t d l i b > 3 # include <cmath> 4 using namespace std ; 5 6 class Punkt { 7 8 private : 9 double x, y ; 10 11 public : 12 Punkt ( ) ; 13 Punkt ( double x, double y ) ; 14 void losuj_wsp ( double a, double b, 15 double c, double d ) ; 16 double dlugosc_wek ( ) ; 17 double getx ( ) ; 18 double gety ( ) ; 19 void setx ( double x ) ; 20 void sety ( double y ) ; 21 bool operator==(const Punkt& Q ) const ; 22 } ;
- - 23 double losuj ( ) ; 24 double losuj ( double a, double b ) ; 25 26 int main ( ) { 27 28 srand ( time ( 0 ) ) ; 29 30 Punkt P1, P2 ( 3, 4 ) ; 31 32 P1. losuj_wsp ( 0, 1 0, 0, 1 0 ) ; 33 34 cout << "P1=(" << P1. getx ( ) << ", " 35 << P1. gety ( ) << ")\n" ; 36 37 cout << "P2=(" << P2. getx ( ) << ", " 38 << P2. gety ( ) << ")\n" ; 39 40 if ( P1 == P2 ) 41 cout << " Punkty sa rowne\n" ; 42 43 }// main 44
- - 45 bool Punkt : : operator==(const Punkt& Q ) const { 46 return ( ( x == Q. x ) && ( y == Q. y ) ) ; 47 } 48 49 Punkt : : Punkt ( ) { 50 this >x = 0 ; 51 this >y = 0 ; 52 } 53 Punkt : : Punkt ( double x, double y ) { 54 this >x = x ; 55 this >y = y ; 56 } 57 58 void Punkt : : losuj_wsp ( double a, double b, 59 double c, double d ) { 60 this >x = losuj ( a, b ) ; 61 this >y = losuj ( c, d ) ; 62 } 63 double Punkt : : dlugosc_wek ( ) { 64 return sqrt ( this >x* this >x 65 + this >y* this >y ) ; 66 }
- - 67 double Punkt : : getx ( ) { 68 return this >x ; 69 } 70 71 double Punkt : : gety ( ) { 72 return this >y ; 73 } 74 75 void Punkt : : setx ( double x ) { 76 this >x = x ; 77 } 78 79 void Punkt : : sety ( double y ) { 80 this >y = y ; 81 } 82 83 double losuj ( ) { 84 return rand ( ) /double ( RAND_MAX ) ; 85 } 86 double losuj ( double a, double b ) { 87 return ( b a ) *losuj ( ) +a ; 88 }
Przedefiniowanie operatora «- - Deklaracja operatora << ostream& operator<<(ostream& os, const Punkt& P);
- - 1 #include <i o s t r e a m > 2 #include <c s t d l i b > 3 # include <cmath> 4 using namespace std ; 5 6 class Punkt { 7 8 private : 9 double x, y ; 10 11 public : 12 Punkt ( ) ; 13 Punkt ( double x, double y ) ; 14 void losuj_wsp ( double a, double b, 15 double c, double d ) ; 16 double dlugosc_wek ( ) ; 17 double getx ( ) const ; 18 double gety ( ) const ; 19 void setx ( double x ) ; 20 void sety ( double y ) ; 21 bool operator==(const Punkt& Q ) const ; 22 } ;
- - 23 double losuj ( ) ; 24 double losuj ( double a, double b ) ; 25 ostream& operator<<(ostream& os, 26 const Punkt& P ) ; 27 28 int main ( ) { 29 30 srand ( time ( 0 ) ) ; 31 32 Punkt P1, P2 ( 3, 4 ) ; 33 34 P1. losuj_wsp ( 0, 1 0, 0, 1 0 ) ; 35 36 cout << P1 ; 37 cout << P2 ; 38 39 if ( P1 == P2 ) 40 cout << " Punkty sa rowne\n" ; 41 42 }// main 43 44
- - 45 ostream& operator<<(ostream &os, 46 const Punkt &P ) { 47 os << "("<< P. getx ( ) << ", " 48 << P. gety ( ) << ")\n" ; 49 return os ; 50 } 51 52 bool Punkt : : operator==(const Punkt& Q ) const { 53 return ( ( x == Q. x ) && ( y == Q. y ) ) ; 54 } 55 56 57 Punkt : : Punkt ( ) { 58 this >x = 0 ; 59 this >y = 0 ; 60 } 61 62 Punkt : : Punkt ( double x, double y ) { 63 this >x = x ; 64 this >y = y ; 65 } 66
- - 67 void Punkt : : losuj_wsp ( double a, double b, 68 double c, double d ) { 69 this >x = losuj ( a, b ) ; 70 this >y = losuj ( c, d ) ; 71 } 72 73 double Punkt : : dlugosc_wek ( ) { 74 return sqrt ( this >x* this >x 75 + this >y* this >y ) ; 76 } 77 78 double Punkt : : getx ( ) const { 79 return this >x ; 80 } 81 82 double Punkt : : gety ( ) const { 83 return this >y ; 84 } 85 86 87 88
- - 89 90 void Punkt : : setx ( double x ) { 91 this >x = x ; 92 } 93 94 void Punkt : : sety ( double y ) { 95 this >y = y ; 96 } 97 98 double losuj ( ) { 99 return rand ( ) /double ( RAND_MAX ) ; 100 } 101 102 double losuj ( double a, double b ) { 103 return ( b a ) *losuj ( ) +a ; 104 }
Przedefiniowanie operatora = - - Deklaracja operatora = Domyślmy operator = kopiuje pola obiektów. P1=P2;
Przedefiniowanie operatora = - - Deklaracja operatora = Domyślmy operator = kopiuje pola obiektów. P1=P2; Należy przedefiniować operator =, jeśli pola są wskaźnikami Punkt& operator=(const Punkt& Q)
Dziedziczenie - - Dziedziczenie polega na stworzeniu nowej klasy na bazie klasy już istniejącej. W klasie pochodnej mamy dostęp do pól i metod public i protected
Dziedziczenie - - Dziedziczenie polega na stworzeniu nowej klasy na bazie klasy już istniejącej. W klasie pochodnej mamy dostęp do pól i metod public i protected Nie dziedziczy się: ˆ konstruktorów ˆ destruktora ˆ operatora =
- - 1 #include <i o s t r e a m > 2 using namespace std ; 3 4 class Zwierze { 5 6 public : 7 void jedz ( ) { 8 cout << " jem" <<endl ; 9 } 10 11 protected : 12 void jedzszybko ( ) { 13 cout << " jem szybko" <<endl ; 14 } 15 16 private : 17 void jedzszybko2 ( ) { 18 cout << " jem private" <<endl ; 19 } 20 21 } ; 22
- - 23 class Kot : public Zwierze { 24 public : 25 void mial ( ) { 26 cout << " miau" <<endl ; 27 } 28 } ; 29 30 class Pies : public Zwierze { 31 public : 32 void hal ( ) { 33 cout << " hal" <<endl ; 34 jedzszybko ( ) ; 35 // j e d z s z y b k o 2 ( ) ; BLAD! 36 } 37 } ; 38 39 int main ( ) { 40 Pies reksio ; 41 reksio. hal ( ) ; 42 reksio. jedz ( ) ; // r e k s i o. j e d z s z y b k o ( ) ; BLAD! 43 }
- - Dziękuję za uwagę.