get and set in c++ code example
Example 1: set and get in c++
class Employee {
private:
int salary;
public:
void setSalary(int s) {
salary = s;
}
int getSalary() {
return salary;
}
};
int main() {
Employee myObj;
myObj.setSalary(50000);
cout << myObj.getSalary();
return 0;
}
Example 2: set in cpp
#include<iostream>
#include<set>
using namespace std;
int main(){
set<int, greater<int>> s1 = {6, 10, 5, 1};
s1.insert(12);
s1.insert(20);
s1.insert(3);
set<int> :: iterator it;
for(it=s1.begin(); it != s1.end();it++)
cout<<*it<<" ";
cout<<endl;
}
Example 3: set in cpp
#include<iostream>
#include<set>
using namespace std;
int main(){
set<int, greater<int>> s1 = {6, 10, 5, 1};
set<int> :: iterator it;
for(it=s1.begin(); it != s1.end();it++)
cout<<*it<<" ";
cout<<endl;
}