Created
April 22, 2018 14:52
-
-
Save 8Observer8/c7d3e93c37b621355476e6f9810d0d05 to your computer and use it in GitHub Desktop.
This file contains 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 <string> | |
// (1) абстрактный класс | |
class Animal { | |
// (2) приватные свойства | |
private: | |
// (3) статическое свойство класса | |
static size_t animalCounter; | |
// (4) защищенные свойства и методы | |
protected: | |
std::string name; | |
size_t age; | |
size_t OnYearPassed() { return ++age; } | |
// публичные методы | |
public: | |
std::string GetName() const { return name; } | |
size_t GetAge() const { return age; } | |
// (5) "абстрактный" метод | |
virtual void SayHello() = 0; | |
Animal(std::string _name = "unnamed", size_t _age = 0) | |
: | |
name(_name), | |
age(_age) { | |
std::cout << name << " has joined to main.cpp :)" << std::endl; | |
std::cout << "++animalCounter: " << ++animalCounter << std::endl; | |
} | |
// (6) деструкторы | |
virtual ~Animal() { | |
std::cout << "--animalCounter: " << --animalCounter << std::endl; | |
std::cout << name << " has left the main.cpp ;(" << std::endl << std::endl; | |
} | |
}; | |
size_t Animal::animalCounter = 0; | |
class Cat : Animal { | |
private: | |
static size_t catCounter; | |
protected: | |
std::string breed; | |
public: | |
std::string GetBreed() const { return breed; } | |
virtual void SayHello() { std::cout << "meow!" << std::endl; } | |
// (7) вызов конструктора базового класса | |
// с передачей в него параметров | |
Cat(std::string _name = "unnamed", size_t _age = 0, std::string _breed = "unknown") | |
: | |
Animal(_name, _age), | |
breed(_breed) { | |
std::cout << "++catCounter: " << ++catCounter << std::endl << std::endl; | |
} | |
virtual ~Cat() { std::cout << std::endl << "--catCounter: " << --catCounter << std::endl; } | |
}; | |
size_t Cat::catCounter = 0; | |
// еще одно животное | |
// класс аналогичен классу Cat | |
class Dog : Animal { | |
private: | |
static size_t dogCounter; | |
protected: | |
enum Type { LAZY, ANGRY } type; | |
public: | |
Type GetType() const { return type; } | |
virtual void SayHello() { std::cout << "WOOF!" << std::endl; } | |
Dog(std::string _name = "unnamed", size_t _age = 0, Type _type = LAZY) | |
: | |
Animal(_name, _age), | |
type(_type) { | |
std::cout << "++dogCounter: " << ++dogCounter << std::endl << std::endl; | |
} | |
virtual ~Dog() { std::cout << std::endl << "--dogCounter: " << --dogCounter << std::endl; } | |
}; | |
size_t Dog::dogCounter = 0; | |
int main(int argc, char *argv[]) { | |
// (1) Попытка создать объект абстрактного класса | |
//Animal a; | |
// main.cpp:70: ошибка: cannot declare variable 'a' to be of abstract type 'Animal' | |
// main.cpp:19: note: because the following virtual functions are pure within 'Animal': | |
// main.cpp:38: note: virtual void Animal::SayHello() | |
Dog * beagley = new Dog("Beagley", 3); | |
Cat * sammy = new Cat("Sammy", 3, "Siamese"); | |
{ Cat barsik("Barsik", 5, "Siberian"); } | |
Dog bullik("Bullik", 5); | |
delete beagley; | |
delete sammy; | |
// [ANTIWARNING] unused parameters: 'argc', 'argv' | |
return 0; (void)argc; (void)argv; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment