-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetter and setter
49 lines (43 loc) · 1.19 KB
/
getter and setter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
using namespace std;
class Percentage
{
private:
int obtained_marks;
int total_marks;
double result;
public:
// Public getter for the result
double getPercentage() const
{
return result;
}
// Constructor to initialize obtained_marks and total_marks
Percentage(int obtained, int total) : obtained_marks(obtained), total_marks(total)
{
calculatePercentage();
}
// Function to calculate percentage and store in result
void calculatePercentage()
{
// Ensure total_marks is not zero to avoid division by zero
if (total_marks == 0)
{
cout<< "Error: Total marks cannot be zero." << std::endl;
result = 0.0; // Set result to 0 for error handling
}
else
{
result = static_cast<double>(obtained_marks) / total_marks * 100.0;
}
}
};
int main() {
int obtained = 537;
int total = 600;
Percentage percentage(obtained, total);
// Access the percentage using the getter
double calculatedPercentage = percentage.getPercentage();
cout << "Percentage: " << calculatedPercentage << "%" << std::endl;
return 0;
}