Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <vector>
- #include <iostream>
- using namespace std;
- class Mammal {
- private:
- string name;
- public:
- virtual string voice() const {
- return "Not implemented yet\n";
- }
- };
- class Cat : public Mammal {
- public:
- string murmur() const {
- return "murmur\n";
- }
- string voice() const override {
- return "meow\n";
- }
- };
- class Dog : public Mammal {
- public:
- string voice() const override {
- return "Waff!\n";
- }
- };
- int main() {
- Cat c;
- Dog d;
- vector<Mammal> zoo;
- zoo.push_back(c);
- zoo.push_back(d);
- cout << zoo[0].voice() << endl;
- vector<Mammal*> zoo2;
- zoo2.push_back(&c);
- zoo2.push_back(&d);
- cout << zoo2[0]->voice() << endl;
- }
- ======================================================
- #include <iostream>
- #include <string>
- using namespace std;
- class Old {
- protected:
- int x = 3;
- };
- class New: public Old {
- protected:
- int x = 4;
- public:
- New() {
- cout << x << " " << Old::x << endl;
- }
- };
- class Newest: New {
- protected:
- string x = "5";
- public:
- Newest() {
- cout << this->x << " " << New::x << " " << Old::x << endl;
- }
- };
- int main() {
- Old();
- New();
- Newest();
- }
- =============================================
- #include <iostream>
- #include <string>
- using namespace std;
- enum {
- FOO = 1, BAR = 2, BUZZ = 4
- };
- enum Colors {
- RED, BLUE
- };
- enum Numbers {
- // можно переопределять начал значения
- ONE = 1, TWO, SEVEN = 7, EIGHT
- };
- enum class TheColors {
- THE_RED, THE_BLUE, THE_VIOLET
- };
- enum class TheColors2 : int {
- THE_RED = 1, THE_BLUE = 4, THE_VIOLET = 5
- };
- int main() {
- cout << sizeof(Colors) << " " << sizeof(RED) << " " << Colors::BLUE << endl;
- // это константы, которые экспортируют свои имена в пространство имён
- int x = 2 + RED;
- Colors y = RED; // y++; хотя они и защищены явно от арифметики
- y = static_cast<Colors>(
- static_cast<int>(y) + 1
- );
- cout << (y == BLUE ? "DA": "NET") << endl;
- cout << ONE << TWO << SEVEN << EIGHT << endl;
- // часто использовались в C как множества (настроек, ключей, ...)
- auto some = FOO | BUZZ;
- if ((some & BUZZ) == BUZZ) {
- cout << "We have BUZZ" << endl;
- }
- //===========================================
- TheColors color = TheColors::THE_RED; // просто THE_RED не сработает
- TheColors2 color2 = static_cast<TheColors2>(
- static_cast<int>(TheColors2::THE_RED) |
- static_cast<int>(TheColors2::THE_BLUE)
- );
- if (color2 == TheColors2::THE_VIOLET) {
- cout << "WOW" << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement