Created
November 9, 2025 15:46
-
-
Save Marie-zaj/0a656003f6ef9d5953e8f19e94466afd to your computer and use it in GitHub Desktop.
InheritanceAnimals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<iostream> | |
| #include<Windows.h> | |
| using namespace std; | |
| class Pet | |
| { | |
| protected: | |
| char* name; | |
| int age; | |
| public: | |
| Pet() = default; | |
| Pet(const char* Name, int Age) | |
| { | |
| cout << "Construct Pet\n"; | |
| name = new char[strlen(Name) + 1]; | |
| strcpy_s(name, strlen(Name) + 1, Name); | |
| age = Age; | |
| } | |
| void Print() const | |
| { | |
| cout << "Output Pet\n"; | |
| cout << "Name: " << name << endl | |
| << "Age: " << age << endl; | |
| } | |
| void Input() | |
| { | |
| delete[] name; | |
| char buff[20]; | |
| cin.ignore(); | |
| cin.getline(buff, 20); | |
| name = new char[strlen(buff) + 1]; | |
| strcpy_s(name, strlen(buff) + 1, buff); | |
| cin >> age; | |
| } | |
| ~Pet() | |
| { | |
| if (name != nullptr) | |
| { | |
| delete[] name; | |
| } | |
| cout << "Destructor Pet\n"; | |
| Sleep(1000); | |
| } | |
| }; | |
| class Dog :public Pet | |
| { | |
| char* Breed; | |
| char* Gender; | |
| public: | |
| Dog() = default; | |
| Dog(const char* n, int a, const char* b, const char* g) : Pet(n, a) | |
| { | |
| cout << "Constructor Dog\n"; | |
| Breed = new char[strlen(b) + 1]; | |
| strcpy_s(Breed, strlen(b) + 1, b); | |
| Gender = new char[strlen(g) + 1]; | |
| strcpy_s(Gender, strlen(g) + 1, g); | |
| } | |
| ~Dog() | |
| { | |
| if (Breed != nullptr) | |
| { | |
| delete[] Breed; | |
| } | |
| if (Gender != nullptr) | |
| { | |
| delete[] Gender; | |
| } | |
| cout << "Destructor Dog\n"; | |
| Sleep(1000); | |
| } | |
| void Print() | |
| { | |
| cout << "Output Dog\n"; | |
| Pet::Print(); | |
| cout << "Breed: " << Breed << endl; | |
| cout << "Gender: " << Gender << endl; | |
| } | |
| void Input(const char* b, const char* g) | |
| { | |
| delete[] Breed; | |
| Breed = new char[strlen(b) + 1]; | |
| strcpy_s(Breed, strlen(b) + 1, b); | |
| delete[] Gender; | |
| Gender = new char[strlen(g) + 1]; | |
| strcpy_s(Gender, strlen(g) + 1, g); | |
| Pet::Input(); | |
| } | |
| }; | |
| class Pig :public Pet | |
| { | |
| int Quantity; | |
| char* Color; | |
| public: | |
| Pig() = default; | |
| Pig(const char* n, int a, int q, const char* c) : Pet(n, a) | |
| { | |
| cout << "Constructor Pig\n"; | |
| Quantity = q; | |
| Color = new char[strlen(c) + 1]; | |
| strcpy_s(Color, strlen(c) + 1, c); | |
| } | |
| ~Pig() | |
| { | |
| if (Color != nullptr) | |
| { | |
| delete[] Color; | |
| } | |
| cout << "Destructor Pig\n"; | |
| Sleep(1000); | |
| } | |
| void Print() | |
| { | |
| cout << "Output Pig\n"; | |
| Pet::Print(); | |
| cout << "Quantity: " << Quantity << endl; | |
| cout << "Color: " << Color << endl; | |
| } | |
| void Input(int q, const char* c) | |
| { | |
| Quantity = q; | |
| delete[] Color; | |
| Color = new char[strlen(c) + 1]; | |
| strcpy_s(Color, strlen(c) + 1, c); | |
| Pet::Input(); | |
| } | |
| }; | |
| class Tiger :public Pet | |
| { | |
| char* Teeth; | |
| char* Stripes; | |
| public: | |
| Tiger() = default; | |
| Tiger(const char* n, int a, const char* t, const char* s) : Pet(n, a) | |
| { | |
| cout << "Constructor Tiger\n"; | |
| Teeth = new char[strlen(t) + 1]; | |
| strcpy_s(Teeth, strlen(t) + 1, t); | |
| Stripes = new char[strlen(s) + 1]; | |
| strcpy_s(Stripes, strlen(s) + 1, s); | |
| } | |
| ~Tiger() | |
| { | |
| if (Teeth != nullptr) | |
| { | |
| delete[] Teeth; | |
| } | |
| if (Stripes != nullptr) | |
| { | |
| delete[] Stripes; | |
| } | |
| cout << "Destructor Tiger\n"; | |
| Sleep(1000); | |
| } | |
| void Print() | |
| { | |
| cout << "Output Tiger\n"; | |
| Pet::Print(); | |
| cout << "Teeth: " << Teeth << endl; | |
| cout << "Stripes: " << Stripes << endl; | |
| } | |
| void Input(const char* t, const char* s) | |
| { | |
| delete[] Teeth; | |
| Teeth = new char[strlen(t) + 1]; | |
| strcpy_s(Teeth, strlen(t) + 1, t); | |
| delete[] Stripes; | |
| Stripes = new char[strlen(s) + 1]; | |
| strcpy_s(Stripes, strlen(s) + 1, s); | |
| Pet::Input(); | |
| } | |
| }; | |
| class Cow :public Pet | |
| { | |
| char* Health; | |
| char* Milk; | |
| public: | |
| Cow() = default; | |
| Cow(const char* n, int a, const char* h, const char* m) : Pet(n, a) | |
| { | |
| cout << "Constructor Cow\n"; | |
| Health = new char[strlen(h) + 1]; | |
| strcpy_s(Health, strlen(h) + 1, h); | |
| Milk = new char[strlen(m) + 1]; | |
| strcpy_s(Milk, strlen(m) + 1, m); | |
| } | |
| ~Cow() | |
| { | |
| if (Health != nullptr) | |
| { | |
| delete[] Health; | |
| } | |
| if (Milk != nullptr) | |
| { | |
| delete[] Milk; | |
| } | |
| cout << "Destructor Cow\n"; | |
| Sleep(1000); | |
| } | |
| void Print() | |
| { | |
| cout << "Output Cow\n"; | |
| Pet::Print(); | |
| cout << "Health: " << Health << endl; | |
| cout << "Stripes: " << Milk << endl; | |
| } | |
| void Input(const char* h, const char* m) | |
| { | |
| delete[] Health; | |
| Health = new char[strlen(h) + 1]; | |
| strcpy_s(Health, strlen(h) + 1, h); | |
| delete[] Milk; | |
| Milk = new char[strlen(m) + 1]; | |
| strcpy_s(Milk, strlen(m) + 1, m); | |
| Pet::Input(); | |
| } | |
| }; | |
| int main() | |
| { | |
| Dog a("Oleg", 5, "Buldog", "Female"); | |
| a.Input("Buldog", "Female"); | |
| a.Print(); | |
| Pig b("Buba", 3, 2, "Pink"); | |
| b.Input(2, "Pink"); | |
| b.Print(); | |
| Tiger c("Roar", 6, "Sharp", "Long"); | |
| c.Input("Sharp", "Long"); | |
| c.Print(); | |
| Cow d("Travka", 2, "Good", "Gives"); | |
| d.Input("Good", "Gives"); | |
| d.Print(); | |
| system("pause"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment