Практика 12
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 <memory> | |
class Comparable { | |
public: | |
virtual int compareTo(const Comparable& other) const = 0; | |
}; | |
class Int : public Comparable { | |
int value; | |
public: | |
Int(int x) | |
: value(x) | |
{} | |
int get() const { | |
return value; | |
} | |
void set(int x) { | |
value = x; | |
} | |
int compareTo(const Comparable& other) const override { | |
const Int *otherPtr = dynamic_cast<const Int*>(&other); | |
return get() - otherPtr->get(); | |
} | |
}; | |
int main() { | |
std::cout << Int(179).compareTo(Int(30)) << std::endl; | |
} |
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 <memory> | |
class Base { | |
int x; | |
public: | |
Base() {} | |
virtual void f() { | |
std::cout << "Base" << std::endl; | |
} | |
}; | |
class Derived : public Base { | |
public: | |
Derived() {} | |
virtual void f() override { | |
std::cout << "Derived" << std::endl; | |
} | |
}; | |
int main() { | |
std::shared_ptr<Base> b = std::make_shared<Derived>(); | |
b->f(); | |
return 0; | |
} |
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> | |
class Engine { | |
public: | |
virtual void start() = 0; | |
virtual std::string name() = 0; | |
}; | |
using EnginePtr = std::shared_ptr<Engine>; | |
class V8Engine : public Engine { | |
public: | |
void start() override { | |
std::cout << "WROOOOOM!!!!" << std::endl; | |
} | |
std::string name() override { | |
return "V8"; | |
} | |
}; | |
class Car { | |
public: | |
Car(EnginePtr engine) | |
: engine_(engine) | |
{} | |
virtual ~Car() = default; | |
EnginePtr getEngine() { | |
return engine_; | |
} | |
virtual std::string name() { return ""; } | |
private: | |
EnginePtr engine_; | |
}; | |
class BatmanCar : public Car { | |
public: | |
BatmanCar() | |
: Car(std::make_shared<V8Engine>()) | |
{} | |
~BatmanCar() override final = default; | |
std::string name() override final { | |
return "Batman car"; | |
} | |
}; | |
void print(Engine& e) { | |
std::cout << "Engine name is " << e.name() << std::endl; | |
} | |
int main() { | |
BatmanCar b; | |
b.getEngine()->start(); | |
return 0; | |
} |
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 <memory> | |
#include <string> | |
std::string y; | |
void g(std::string&& x) { | |
y = x; | |
} | |
void f(std::string&& x) { | |
x.push_back('a'); | |
g(std::forward<std::string>(x)); | |
} | |
int main() { | |
std::string s = "d"; | |
f(std::move(s)); | |
std::cout << y << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment