Last active
July 12, 2025 09:45
-
-
Save sunmeat/0454756ee7345784a2ddd1034f92e71c to your computer and use it in GitHub Desktop.
C++ diamond problem (rhombus inheritance)
This file contains hidden or 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> | |
using namespace std; | |
#define PI 3.14159265358979323846 | |
// точка | |
class Point | |
{ | |
protected: | |
double x; | |
double y; | |
public: | |
Point() | |
{ | |
cout << "Point default c-tor!\n"; | |
x = y = 5; | |
} | |
Point(int x, int y) | |
{ | |
cout << "Point param c-tor!\n"; | |
this->x = x; | |
this->y = y; | |
} | |
void Print() const | |
{ | |
cout << "Point - X: " << x << ", Y: " << y << "\n"; | |
} | |
~Point() | |
{ | |
cout << "Point destructor!\n"; | |
} | |
}; | |
// коло | |
class Circle : public Point | |
{ | |
protected: | |
double radius; | |
double area; | |
public: | |
Circle() | |
{ | |
cout << "Circle default c-tor!\n"; | |
radius = 5; | |
area = PI * pow(radius, 2); | |
} | |
Circle(int center_x, int center_y, int radius) : Point(center_x, center_y) | |
{ | |
cout << "Circle param c-tor!\n"; | |
this->radius = radius; | |
} | |
void Print() const | |
{ | |
cout << x << " " << y << " " << radius << "\n"; | |
} | |
~Circle() | |
{ | |
cout << "Circle destructor!\n"; | |
} | |
}; | |
// квадрат | |
class Square : public Point | |
{ | |
protected: | |
double x2; | |
double y2; | |
double area; | |
public: | |
Square() | |
{ | |
cout << "Square c-tor!\n"; | |
x2 = y2 = 10; | |
area = abs(x2 - x) * abs(y2 - y); | |
} | |
void Print() const | |
{ | |
cout << x << " " << y << " " << x2 << " " << y2 << "\n"; | |
} | |
~Square() | |
{ | |
cout << "Square destructor!\n"; | |
} | |
}; | |
// коло, вписане в квадрат | |
class SquareCircle : public Circle, public Square | |
{ | |
double exclusive_area; | |
public: | |
SquareCircle() | |
{ | |
cout << "SquareCircle c-tor!\n"; | |
// exclusive_area = area_ - area_; // ??? OOPS! | |
} | |
void Print() const | |
{ | |
// cout << x << " " << y << " " << x2 << " " << y2 << "\n"; // ??? OOPS # 2 | |
cout << "SquareCircle::Print - OOPS!\n"; | |
} | |
~SquareCircle() | |
{ | |
cout << "SquareCircle destructor!\n"; | |
} | |
}; | |
/* у сучасній методології об'єктно-орієнтованого | |
програмування існує три способи розв'язання цієї проблеми. | |
1) об'єднувати такі поля в одне поле у похідному класі - потрібен віртуальний базовий клас | |
// https://ravesli.com/urok-169-virtualnyj-bazovyj-klass/ | |
2) розглядати такий конфлікт як помилку - тобто, взагалі намагатися не застосовувати множинне успадкування | |
3) використовувати операцію дозволу контексту для доступу до таких полів у похідному класі */ | |
int main() { | |
Point point; | |
point.Print(); | |
cout << "\n==============================\n\n"; | |
Circle circle; | |
circle.Print(); | |
cout << "\n==============================\n\n"; | |
Square square; | |
square.Print(); | |
cout << "\n==============================\n\n"; | |
SquareCircle sc; | |
sc.Print(); | |
cout << "\n==============================\n\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment