Skip to content

Instantly share code, notes, and snippets.

@kirilltobola
Last active December 2, 2021 16:41
Show Gist options
  • Save kirilltobola/5f8a0ea146df08c6929eb75a200dd251 to your computer and use it in GitHub Desktop.
Save kirilltobola/5f8a0ea146df08c6929eb75a200dd251 to your computer and use it in GitHub Desktop.
OOP lab8
#include <iostream>
using namespace std;
class rational {
private:
long long nom;
unsigned long long denom;
public:
rational() {
nom = 0;
denom = 1;
}
rational(long long x) {
nom = x;
denom = 1;
}
rational(long long _nom, unsigned long long _denom) {
nom = _nom;
denom = _denom;
}
string to_string() {
return std::to_string(nom) + " " + std::to_string(denom);
}
};
int main() {
rational r = 3;
rational r2(-2, 3);
cout << r.to_string();
cout << endl << r2.to_string();
return 0;
}
// Online C++ compiler to run C++ program online
#include <iostream>
using namespace std;
class foo {
private:
int x;
public:
int y;
virtual string to_string() {
return "foo implemetnation";
}
virtual double to_float() {
return (double) (x + y);
}
int getX() {
return x;
}
void setX(int _x) {
x = _x;
}
int getY() {
return y;
}
void setY(int _y) {
y = _y;
}
};
class bar : public foo {
private:
int z;
public:
string to_string() {
return "bar implemetnation";
}
double to_float() {
return 2.0;
}
};
int dynamic_example(foo **x) {
int count = 0;
for (int i = 0; i < 3; i++) {
bar *b = dynamic_cast<bar *>(x[i]);
if (b != nullptr) {
count++;
}
}
return count;
}
int main() {
foo **x = new foo*[3];
x[0] = new foo;
x[1] = new bar;
x[2] = new foo;
int res = dynamic_example(x);
cout << res;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
class Progression {
public:
virtual long long first() = 0;
virtual long long next() = 0;
};
class ArithmeticProgression: public Progression {
private:
long long _start;
long long _step;
long long _current;
public:
ArithmeticProgression(long long start, long long step) {
_start = start;
_current = start;
_step = step;
}
long long first() {
_current = _start;
return _current;
}
long long next() {
_current += _step;
return _current;
}
};
class GeometricProgression: public Progression {
private:
long long _start;
long long _current;
long long _step;
public:
GeometricProgression(long long start, long long step) {
_start = start;
_step = step;
}
long long first() {
_current = _start;
return _current;
}
long long next() {
_current *= _step;
return _current;
}
};
class FibonacciProgression: public Progression {
private:
long long _first;
long long _second;
long long _next;
public:
FibonacciProgression(long long first, long long second) {
_first = first;
_second = second;
}
long long first() {
}
long long next() {
}
};
int main() {
Progression *p[2];
p[0] = new ArithmeticProgression(1, 2);
p[1] = new GeometricProgression(1, 2);
p[2] = new FibonacciProgression(1, 1);
for (int i = 0; i < 3; i++) {
for (auto x: p) {
cout << x->first();
for (int j = 0; j < 10; j++) {
cout << ' ' << x->next();
}
cout << endl;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment