Róznice Java/C++
Transkrypt
Róznice Java/C++
Różnice Java/C++ Przemysław Białkowski i Michał Kasiński Some time ago Różnice ◮ Referencje i „prawdziwe obiekty” Różnice ◮ ◮ Referencje i „prawdziwe obiekty” Rodzaje konstruktorów Różnice ◮ ◮ ◮ Referencje i „prawdziwe obiekty” Rodzaje konstruktorów Dziedziczenie wielokrotne Różnice ◮ ◮ ◮ ◮ Referencje i „prawdziwe obiekty” Rodzaje konstruktorów Dziedziczenie wielokrotne Różnice składniowo semantyczne Referencje i „prawdziwe obiekty” X x; / / C + + public X x = new X () ; / / J a v a Referencje i „prawdziwe obiekty” X x; / / C + + public X x = new X () ; / / J a v a X y = x; / / C + + public X y = x ; / / Java przypisanie referencji Referencje i „prawdziwe obiekty” X x; / / C + + public X x = new X () ; / / J a v a X y = x; / / C + + public X y = x ; / / X& y = x; / / C + + Java przypisanie referencji Konstruktor kopiujący class MojaKlasa { public : int dana ; MojaKlasa ( int parametrDomyslny ) { / / konstruktor this - > dana = parametrDomyslny ; } inny }; int main () { MojaKlasa obiektMojejKlasy ( 5 ) ; MojaKlasa kopiaObiektu ( obiektMojejKlasy ) ; std :: cout << kopiaObiektu . dana ; / / " 5 " return 0; } Operator przypisania class MojaKlasa { public : int * data ; int len ; MojaKlasa () { / * . . . * / } ~ MojaKlasa () { / * . . . * / } MojaKlasa & operator =( const MojaKlasa & other ) { if ( this != & other ) { delete [] data ; len = other . len ; data = new int [ len ]; memcpy ( data , other . data , len * sizeof ( int ) ) ; return * this ; } }; Konstruktor przenoszący (C++11) class MojaKlasa { public : int * data ; int len ; MojaKlasa () { / * . . . * / } ~ MojaKlasa () { / * . . . * / } MojaKlasa ( MojaKlasa && other ) { data = other . data ; len = other . len ; /* UWAGA */ other . data = NULL ; other . len = 0; } }; Operator przeniesienia (C++11) class MojaKlasa { public : int * data ; int len ; MojaKlasa () { / * . . . * / } ~ MojaKlasa () { / * . . . * / } MojaKlasa & operator =( MojaKlasa && other ) { if ( this !=& other ) { delete [] data ; data = other . data ; len = other . len ; /* UWAGA */ other . data = NULL ; other . len = 0; } } }; Przenoszenie przykład vector < MojaKlasa > v ; v . push_back ( MojaKlasa (25) ) ; Dziedziczenie W Javie wszystkie klasy dziedziczą po klasie Object. W C++ domyślnie klasa nie ma ustalonego rodzica. # include < iostream > using namespace std ; class A { public : A () { cout << 1; } ~ A () { cout << -1; } }; class B : public A { public : B () { cout << 2; } ~ B () { cout << -2; } }; int main () { A * a = new B ; delete a ; return 0; } # include < iostream > using namespace std ; class A { public : A () { cout << 1; } virtual ~ A () { cout << -1; } }; class B : public A { public : B () { cout << 2; } ~ B () { cout << -2; } }; int main () { A * a = new B ; delete a ; return 0; } Dziedziczenie wielobazowe class A : private B , public C { public : C :: f () ; } Dziedziczenie po wielu klasach Dziedziczenie po wielu klasach class L { / * . . . * / }; / / i n d i r e c t b a s e c l a s s class B1 : virtual public L { / * . . . * / }; class B2 : virtual public L { / * . . . * / }; class D : public B1 , public B2 { / * . . . * / }; / / valid Dziedziczenie po wielu klasach class L { public : void f () ; }; class B1 : private virtual L { }; class B2 : public virtual L { }; class D : public B1 , public B2 { public : void f () { L :: f () ; } }; Klasa B2 oferuje większą dostępnośc dlatego funkcja f() jest przez nią dziedziczona. Mniejsze różnice W C++ ◮ Ograniczona biblioteka standardowa <random> dopiero w C++11 ◮ Jest goto W Javie ◮ Brak liczb unsigned ◮ limitowane użycie const Wiele znanych z Javy konstrukcji zostało zaimplementowanych w C++11 np.: foreach <random> C++11 # include < iostream > # include < random > # include < chrono > using std :: cout ; using std :: endl ; int main () { unsigned seed = std :: chrono :: system_clock :: now () . time_since_epoch () . count () ; std :: uniform_real_distribution < double > unif ( -2.0 ,2.0) ; std :: d e f a u l t _ r a n d o m _ e n g i n e re ( seed ) ; cout << unif ( re ) << endl ; return 0; } foreach C++11 # include < list > # include < iostream > using std :: cout ; using std :: endl ; int main () { std :: list < int > l ; l . push_back (44) ; l . push_back (55) ; for ( int v : l ) { cout << v << endl ; } return 0; }