Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Практика 12
#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;
}
#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;
}
#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;
}
#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
You can’t perform that action at this time.