Ειδικά Θέματα Ι Σήμερα! Static Member Πρόσβαση χωρίς αντικείμενο private static member Static Member Functions Πρόσβαση συνάρτησης χωρίς αντικείμενο Δείκτες σε συνάρτηση η Πίνακες δεικτών σε συνάρτηση 2 1
Static Member/1 #include <iostream.h> class Cat public: Cat(int age):itsage(age)howmanycats++; virtual ~Cat() HowManyCats ; virtual int GetAge() return itsage; virtual void SetAge(int age) itsage = age; static int HowManyCats; private: int itsage; ; int Cat::HowManyCats = 0; 3 Static Member/2 int main() const int MaxCats = 5; int i; Cat *CatHouse[MaxCats]; C for (i = 0; i<maxcats; i++) CatHouse[i] = new Cat(i); for (i = 0; i<maxcats; i++) cout << ʺThere are ʺ; cout << Cat::HowManyCats; cout << ʺ cats left!\nʺ; cout << ʺDeleting the one which is ʺ; cout << CatHouse[i] >GetAge(); cout << ʺ years old\nʺ; delete CatHouse[i]; CatHouse[i] = 0; return 0; 4 2
Output There are 5 cats left! Deleting the one which is 0 years old There are 4 cats left! Deleting the one which is 1 years old There are 3 cats left! Deleting the one which is 2 years old There eeae are 2 cats left! Deleting the one which is 3 years old There are 1 cats left! Deleting the one which is 4 years old 5 Πρόσβαση χωρίς αντικείμενο/1 #include <iostream.h> class Cat public: Cat(int age):itsage(age)howmanycats++; virtual ~Cat() HowManyCats ; virtual int GetAge() return itsage; virtual void SetAge(int age) itsage = age; static int HowManyCats; private: int itsage; ; int Cat::HowManyCats = 0; 6 3
Πρόσβαση χωρίς αντικείμενο/2 void TelepathicFunction(); int main() const int MaxCats = 5; int i; Cat *CatHouse[MaxCats]; for (i = 0; i<maxcats; i++) CatHouse[i] = new Cat(i); TelepathicFunction(); for ( i = 0; i<maxcats; i++) delete CatHouse[i]; TelepathicFunction(); return 0; 7 Πρόσβαση χωρίς αντικείμενο/3 void TelepathicFunction() cout << ʺThere are ʺ; cout << Cat::HowManyCats << ʺ cats alive!\nʺ; 8 4
Output There are 1 cats alive! There are 2 cats alive! There are 3 cats alive! There are 4 cats alive! There are 5 cats alive! There are 4 cats alive! There eeae are 3 cats alive! ai There are 2 cats alive! There are 1 cats alive! There are 0 cats alive! 9 private static member/1 #include <iostream.h> class Cat public: Cat(int age):itsage(age)howmanycats++; virtual ~Cat() HowManyCats ; virtual int GetAge() return itsage; virtual void SetAge(int age) itsage = age; virtual int GetHowMany() return HowManyCats; private: int itsage; static int HowManyCats; ; int Cat::HowManyCats = 0; 10 5
private static member/2 int main() const int MaxCats = 5; int i; Cat *CatHouse[MaxCats]; for (i = 0; i<maxcats; i++) CatHouse[i] = new Cat(i); for (i = 0; i<maxcats; i++) cout << ʺThere are ʺ; cout << CatHouse[i] >GetHowMany(); cout << ʺ cats left!\nʺ; cout << ʺDeleting the one which is ʺ; cout << CatHouse[i] >GetAge()+2; cout << ʺ years old\nʺ; delete CatHouse[i]; CatHouse[i] = 0; return 0; 11 Output There are 5 cats left! Deleting the one which is 2 years old There are 4 cats left! Deleting the one which is 3 years old There are 3 cats left! Deleting the one which is 4 years old There are 2 cats left! Deleting the one which is 5 years old There are 1 cats left! Deleting the one which is 6 years old 12 6
Static Member Functions/1 #include <iostream.h> class Cat public: Cat(int age):itsage(age)howmanycats++; virtual ~Cat() HowManyCats ; virtual int GetAge() return itsage; virtual void SetAge(int age) itsage = age; static int GetHowMany() return HowManyCats; private: int itsage; static int HowManyCats; ; 13 Static Member Functions/2 int Cat::HowManyCats = 0; void TelepathicFunction(); int main() const int MaxCats = 5; Cat *CatHouse[MaxCats]; int i; for (i = 0; i<maxcats; i++) CatHouse[i] = new Cat(i); TelepathicFunction(); for ( i = 0; i<maxcats; i++) delete CatHouse[i]; TelepathicFunction(); return 0; void TelepathicFunction() cout << ʺThere are ʺ << Cat::GetHowMany() << ʺ cats alive!\nʺ; 14 7
Output There are 1 cats alive! There are 2 cats alive! There are 3 cats alive! There are 4 cats alive! There are 5 cats alive! There are 4 cats alive! There are 3 cats alive! There are 2 cats alive! There are 1 cats alive! There are 0 cats alive! 15 Πρόσβαση συνάρτησης χωρίς αντικείμενο/1 class Cat public: static int GetHowMany() return HowManyCats; private: static int HowManyCats; ; int Cat::HowManyCats = 0; int main() int main() int howmany; Cat thecat; howmany = thecat.gethowmany(); howmany = Cat::GetHowMany(); 16 8
Δείκτες σε συνάρτηση Όπως ένα όνομα πίνακα είναι μια σταθερά δείκτη στο πρώτο στοιχείο του πίνακα, ένα όνομα συνάρτησης είναι μια σταθερά δείκτη για τη συνάρτηση. Είναι δυνατόν να δηλώσετε έναν δείκτη που παραπέμπει σε μια συνάρτηση, και να καλέσετε τη συνάρτηση χρησιμοποιώντας το δείκτη. Ένας δείκτης σε μια συνάρτηση, πρέπει να υποδηλώνει τον κατάλληλο τύπο επιστροφής. 17 Δείκτης σε συνάρτηση/1 #include <iostream.h> void Square (int&,int&); void Cube (int&, int&); void Swap (int&, int &); void GetVals(int&, int&); void PrintVals(int, int); enum BOOL FALSE, TRUE ; int main() void (* pfunc) (int &, int &); BOOL fquit = FALSE; int valone=1, valtwo=2; int choice; 18 9
Δείκτης σε συνάρτηση/2 while (fquit == FALSE) cout << ʺ(0)Quit ( (1)Change Values (2)Square (3)Cube (4)Swap: ʺ; ; cin >> choice; switch (choice) case 1: pfunc = GetVals; break; case 2: pfunc = Square; break; case 3: pfunc = Cube; break; case 4: pfunc = Swap; break; default : fquit = TRUE; break; if (fquit) break; pfunc(valone, valtwo); return 0; 19 Δείκτης σε συνάρτηση/3 void PrintVals(int x, int y) cout << ʺx: ʺ << x << ʺ y: ʺ << y << endl; void Square (int & rx, int & ry) rx *= rx; ry *= ry; void Cube (int & rx, int & ry) int tmp; tmp = rx; rx *= rx; rx = rx * tmp; tmp = ry; ry *= ry; ry = ry * tmp; 20 10
Δείκτης σε συνάρτηση/4 void Swap(int & rx, int & ry) int temp; temp = rx; rx = ry; ry = temp; void GetVals (int & rvalone, int & rvaltwo) cout << ʺNew value for ValOne: ʺ; cin >> rvalone; cout << ʺNew value for ValTwo: ʺ; cin >> rvaltwo; 21 Output (0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 1 x: 1 y: 2 New value for ValOne: 2 New value for ValTwo: 3 x: 2 y: 3 (0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 3 x: 2 y: 3 x: 8 y: 27 (0)Quit (1)Change Values aue (2)Square e( (3)Cube e( (4)Swap: 2 x: 8 y: 27 x: 64 y: 729 (0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 4 x: 64 y: 729 x: 729 y: 64 (0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 0 22 11
Χωρίς δείκτες σε συνάρτηση/1 #include <iostream.h> void Square (int&,int&); void Cube (int&, int&); void Swap (int&, int &); void GetVals(int&, int&); void PrintVals(int, int); enum BOOL FALSE, TRUE ; int main() BOOL fquit = FALSE; int valone=1, valtwo=2; int choice; 23 Χωρίς δείκτες σε συνάρτηση/2 while (fquit == FALSE) cout << ʺ(0)Quit ( (1)Change Values (2)Square (3)Cube (4)Swap: ʺ; ; cin >> choice; switch (choice) case 1: GetVals(valOne, valtwo); break; case 2: Square(valOne,valTwo); break; 24 12
Χωρίς δείκτες σε συνάρτηση/3 case 3: Cube(valOne, valtwo); break; case 4: Swap(valOne, valtwo); PrintVals(valOne, a valtwo); break; default : fquit = TRUE; break; if (fquit) break; return 0; 25 Output (0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 1 x: 1 y: 2 New value for ValOne: 2 New value for ValTwo: 3 (0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 3 x: 2 y: 3 x: 8 y: 27 (0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 2 x: 8 y: 27 x: 64 y: 729 (0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 4 x: 64 y: 729 x: 729 y: 64 (0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 0 26 13
Πίνακες δεικτών σε συνάρτηση/1 #include <iostream.h> void Square (int&,int&); void Cube (int&, int&); void Swap (int&, int &); void GetVals(int&, int&); void PrintVals(int, int); enum BOOL FALSE, TRUE ; int main() int valone=1, valtwo=2; int choice, i; const MaxArray = 5; void (*pfuncarray[maxarray])(int&, int&); 27 Πίνακες δεικτών σε συνάρτηση/2 for (i=0;i<maxarray;i++) cout << ʺ(1)Change ( ) g Values (2)Square (3)Cube (4)Swap: ʺ; ; cin >> choice; switch (choice) case 1:pFuncArray[i] = GetVals; break; case 2:pFuncArray[i] = Square; break; case 3:pFuncArray[i] = Cube; break; case 4:pFuncArray[i] = Swap; break; default:pfuncarray[i] = 0; for (i=0;i<maxarray; i++) pfuncarray[i](valone,valtwo); PrintVals(valOne,valTwo); return 0; 28 14
Output (1)Change Values (2)Square (3)Cube (4)Swap: 1 (1)Change Values (2)Square (3)Cube (4)Swap: 2 (1)Change Values (2)Square (3)Cube (4)Swap: 3 (1)Change Values (2)Square (3)Cube (4)Swap: 4 (1)Change Values (2)Square (3)Cube (4)Swap: 2 New Value for ValOne: 2 New Value for ValTwo: 3 x: 2 y: 3 x: 4 y: 9 x: 64 y: 729 x: 729 y: 64 x: 7153 y:4096 29 15