getter and seeter method in c++
Fri Oct 04 2024 23:22:27 GMT+0000 (Coordinated Universal Time)
Saved by
@E23CSEU1151
#include <iostream>
using namespace std;
class SAM {
// properties
public:
int health;
char level;
int getHealth() {
return health;
}
char getLevel() {
return level;
}
void setHealth(int h) {
health = h;
}
void setLevel(char l) {
level = l;
}
};
int main() {
// Creating an object
SAM hero;
hero.setHealth(30);
hero.setLevel('A'); // Set level to a single character
cout << "His health is: " << hero.getHealth() << endl;
cout << "His level is: " << hero.getLevel() << endl;
return 0;
}
content_copyCOPY
Comments