Skip to content

Commit 5761868

Browse files
committed
add: cpp-programming-questions 13_virtual_abstract 08
1 parent e0cc0ed commit 5761868

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"files.associations": {
3+
"iostream": "cpp"
4+
}
5+
}

13_virtual_function_and_abstract_class/00_questions.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
07. Create a base class called Matrix. Use this class to store a matrix of 2 x 2 of int type values that could be used to calculate determinants and create matrices. Create class CalculateDeterminant which will calculate the determinant of a matrix. Using these classes, calculate the determinant of the matrix.
2020

21-
08. Create a base class called proof. Use this class to store two 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
21+
08. 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
2222
display the result. (Summary: Prove that triangle is a right angled triangle using pythagoras theorem).
2323

2424
09. Create a base class called volume. Use this class to store two double type values
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
}
Binary file not shown.

0 commit comments

Comments
 (0)