-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetters_setters.cpp
More file actions
41 lines (33 loc) · 914 Bytes
/
getters_setters.cpp
File metadata and controls
41 lines (33 loc) · 914 Bytes
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
#include <iostream>
using namespace std;
class Student {
private:
string name;
int age;
float marks;
public:
// Inline setter
void setName(const string& n) { name = n; }
// Inline getter (const because it does not modify anything)
string getName() const { return name; }
void setAge(int a) {
if (a > 0) age = a;
else cout << "Invalid age!" << endl;
}
int getAge() const { return age; }
void setMarks(float m) {
if (m >= 0 && m <= 100) marks = m;
else cout << "Invalid marks!" << endl;
}
float getMarks() const { return marks; }
};
int main() {
Student s1;
s1.setName("Aarav");
s1.setAge(14);
s1.setMarks(92.5);
cout << "Name: " << s1.getName() << endl;
cout << "Age: " << s1.getAge() << endl;
cout << "Marks: " << s1.getMarks() << endl;
return 0;
}