
6.5.6 Hàm khởi tạo sao chép – copying constructors (6)
Trong một số trường hợp (đặc biệt khi cấu trúc của lớp phức tạp và cần cẩn thận nhiều hơn trong quá trình tạo đối […]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#include <iostream> #include <string> using namespace std; class Pet { protected: string name; public: Pet(string name) : name(name) {} virtual void MakeSound(void) { cout << name << " is silent :(" << endl; } }; class Dog : public Pet { public: Dog(string name) : Pet(name) {} void MakeSound(void) { cout << name << " says: Woof!" << endl; } }; class GermanShepherd : public Dog { public: GermanShepherd(string name) : Dog(name) {} void MakeSound(void) { cout << name << " says: Wuff!" << endl; } void Laufen(void) { cout << name << " runs (gs)!" << endl; } }; class MastinEspanol : public Dog { public: MastinEspanol(string name) : Dog(name) {} void MakeSound(void) { cout << name << " says: Guau!" << endl; } void Ejecutar(void) { cout << name << " runs (mes)!" << endl; } }; void PlayWithPet(Pet &pet) { pet.MakeSound(); try { dynamic_cast<GermanShepherd &>(pet).Laufen(); } catch(...) {} try { dynamic_cast<MastinEspanol &>(pet).Ejecutar(); } catch(...) {} } int main(void) { Pet pet("creature"); Dog dog("Dog"); GermanShepherd gs("Hund"); MastinEspanol mes("Perro"); PlayWithPet(pet); PlayWithPet(dog); PlayWithPet(gs); PlayWithPet(mes); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
#include <iostream> #include <string> using namespace std; class Pet { protected: string name; public: Pet(string name) : name(name) {} virtual void MakeSound(void) { cout << name << " is silent :(" << endl; } }; class Dog : public Pet { public: Dog(string name) : Pet(name) {} void MakeSound(void) { cout << name << " says: Woof!" << endl; } }; class GermanShepherd : public Dog { public: GermanShepherd(string name) : Dog(name) {} void MakeSound(void) { cout << name << " says: Wuff!" << endl; } void Laufen(void) { cout << name << " runs (gs)!" << endl; } }; class MastinEspanol : public Dog { public: MastinEspanol(string name) : Dog(name) {} void MakeSound(void) { cout << name << " says: Guau!" << endl; } void Ejecutar(void) { cout << name << " runs (mes)!" << endl; } }; void PlayWithPet(Pet &pet) { pet.MakeSound(); dynamic_cast<GermanShepherd &>(pet).Laufen(); dynamic_cast<MastinEspanol &>(pet).Ejecutar(); } int main(void) { Pet pet("creature"); Dog dog("Dog"); GermanShepherd gs("Hund"); MastinEspanol mes("Perro"); PlayWithPet(pet); PlayWithPet(dog); PlayWithPet(gs); PlayWithPet(mes); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
#include <iostream> #include <string> using namespace std; class Pet { protected: string name; public: Pet(string name) : name(name) {} virtual void MakeSound(void) { cout << name << " is silent" << endl; } }; class Dog : public Pet { public: Dog(string name) : Pet(name) {} void MakeSound(void) { cout << name << " says: Woof!" << endl; } }; class GermanShepherd : public Dog { public: GermanShepherd(string name) : Dog(name) {} void MakeSound(void) { cout << name << " says: Wuff!" << endl; } void Laufen(void) { cout << name << " runs (gs)!" << endl; } }; class MastinEspanol : public Dog { public: MastinEspanol(string name) : Dog(name) {} void MakeSound(void) { cout << name << " says: Guau!" << endl; } void Ejecutar(void) { cout << name << " runs (mes)!" << endl; } }; void PlayWithPet(Pet *pet) { GermanShepherd *gs; MastinEspanol *mes; pet -> MakeSound(); if(gs = dynamic_cast<GermanShepherd *>(pet)) { gs -> Laufen(); } if(mes = dynamic_cast<MastinEspanol *>(pet)) { mes -> Ejecutar(); } } int main(void) { Pet *pet = new Pet("creature"); Dog *dog = new Dog("Dog"); GermanShepherd *gs = new GermanShepherd("Hund"); MastinEspanol *mes = new MastinEspanol("Perro"); PlayWithPet(pet); PlayWithPet(dog); PlayWithPet(gs); PlayWithPet(mes); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
#include <iostream> #include <string> using namespace std; class Pet { protected: string name; public: Pet(string name) { this -> name = name; } void MakeSound(void) { cout << name << " is silent :(" << endl; } }; class Dog : public Pet { public: Dog(string name) : Pet(name) {} void MakeSound(void) { cout << name << " says: Woof!" << endl; } }; class GermanShepherd : public Dog { public: GermanShepherd(string name) : Dog(name) {} void MakeSound(void) { cout << name << " says: Wuff!" << endl; } }; class MastinEspanol : public Dog { public: MastinEspanol(string name) : Dog(name) {} void MakeSound(void) { cout << name << " says: Guau!" << endl; } }; void PlayWithPet(Pet *pet) { pet -> MakeSound(); } int main(void) { Pet *pet = new Pet("creature"); Dog *dog = new Dog("Dog"); GermanShepherd *gs = new GermanShepherd("Hund"); MastinEspanol *mes = new MastinEspanol("Perro"); PlayWithPet(pet); PlayWithPet(dog); PlayWithPet(gs); PlayWithPet(mes); return 0; } |
Copyright © 2025 CppDeveloper by Phạm Minh Tuấn (SHUN)