|
| 1 | +// // Create a base class called proof. Use this class to store three int type values that could be used to prove that triangle is a right angled triangle. Create a class compute which will determine whether a triangle is a right angled triangle. Using these classes, design a program that will accept dimensions of a triangle, and display the result. (Summary: Prove that triangle is a right angled triangle using pythagoras theorem). |
| 2 | + |
| 3 | +// // Header files |
| 4 | +#include <iostream> |
| 5 | +#include <conio.h> |
| 6 | +#include <algorithm> |
| 7 | + |
| 8 | +// // use namespace |
| 9 | +using namespace std; |
| 10 | + |
| 11 | +// // define class Proof |
| 12 | +class Proof |
| 13 | +{ |
| 14 | +protected: |
| 15 | + // // instance member variables |
| 16 | + int side1, side2, side3; |
| 17 | + |
| 18 | +public: |
| 19 | + // // constructor |
| 20 | + Proof(int a, int b, int c) : side1(a), side2(b), side3(c) {} |
| 21 | + |
| 22 | + // // display sides |
| 23 | + void displaySides() const |
| 24 | + { |
| 25 | + cout << "\nSide 1 => " << side1; |
| 26 | + cout << "\nSide 2 => " << side2; |
| 27 | + cout << "\nSide 3 => " << side3; |
| 28 | + } |
| 29 | +}; |
| 30 | + |
| 31 | +// // define class Compute by inheriting class Proof |
| 32 | +class Compute : public Proof |
| 33 | +{ |
| 34 | +public: |
| 35 | + // // inheriting the constructor of the base class |
| 36 | + using Proof::Proof; |
| 37 | + |
| 38 | + // // function to check if the triangle is right-angled using Pythagorean theorem |
| 39 | + bool isRightAngled() const |
| 40 | + { |
| 41 | + // sort sides in ascending order |
| 42 | + int sides[3] = {side1, side2, side3}; |
| 43 | + std::sort(sides, sides + 3); |
| 44 | + |
| 45 | + // Check Pythagorean theorem |
| 46 | + return (sides[0] * sides[0] + sides[1] * sides[1] == sides[2] * sides[2]); |
| 47 | + } |
| 48 | +}; |
| 49 | + |
| 50 | +int main() |
| 51 | +{ |
| 52 | + int sideA, sideB, sideC; |
| 53 | + |
| 54 | + // accept dimensions of the triangle |
| 55 | + cout << "\nEnter the Dimensions of A Triangle => "; |
| 56 | + cin >> sideA >> sideB >> sideC; |
| 57 | + |
| 58 | + // // create an instance of Compute |
| 59 | + Compute triangleInstance(sideA, sideB, sideC); |
| 60 | + |
| 61 | + // display the sides |
| 62 | + triangleInstance.displaySides(); |
| 63 | + |
| 64 | + // check and display if the triangle is right-angled |
| 65 | + if (triangleInstance.isRightAngled()) |
| 66 | + { |
| 67 | + cout << "\nThe triangle is a Right-Angled Triangle (Satisfies the Pythagorean Theorem)...\n"; |
| 68 | + } |
| 69 | + else |
| 70 | + { |
| 71 | + cout << "\nThe Triangle is Not a Right-Angled Triangle.\n"; |
| 72 | + } |
| 73 | + |
| 74 | + return 0; |
| 75 | +} |
0 commit comments