Classes & Objects
Encapsulation, this & static Members
Getters and setters with validation, the this pointer, const member functions, and static data and functions shared by every object.
Getters and setters
Encapsulation means keeping data private and exposing controlled access. A setter can validate input; a getter can return a value without exposing the variable itself.
#include <iostream>
#include <string>
using namespace std;
class Employee {
string name;
double salary = 0;
public:
void setName(const string &n) { if (!n.empty()) name = n; }
void setSalary(double s) {
if (s < 0) { cout << "Rejected negative salary " << s << endl; return; }
salary = s;
}
string getName() const { return name; }
double getSalary() const { return salary; }
};
int main() {
Employee e;
e.setName("Divya");
e.setSalary(65000);
e.setSalary(-500); // the class protects its own data
cout << e.getName() << " earns " << e.getSalary() << endl;
return 0;
}Rejected negative salary -500 Divya earns 65000
The this pointer
Inside a member function, this is a pointer to the object the function was called on. You use it to tell members apart from parameters with the same name, and to return the object itself (which allows method chaining):
#include <iostream>
#include <string>
using namespace std;
class Pizza {
string size = "medium";
int toppings = 0;
public:
Pizza &setSize(const string &size) { this->size = size; return *this; } // this->size is the member
Pizza &addTopping() { toppings++; return *this; }
void print() const { cout << size << " pizza with " << toppings << " toppings" << endl; }
};
int main() {
Pizza p;
p.setSize("large").addTopping().addTopping().addTopping(); // chaining
p.print();
return 0;
}large pizza with 3 toppings
const member functions
A member function marked const promises not to modify the object. Only const functions can be called on const objects and through const references — so mark every getter const.
class Account {
double balance;
public:
double getBalance() const { return balance; } // OK: only reads
void reset() const { balance = 0; } // error: modifies a member in a const function
};static data members
A static data member belongs to the class, not to any object: there is exactly one copy shared by all objects. It must be defined once outside the class (or declared inline static in C++17).
static member functions
A static member function can be called without any object, as ClassName::function(). It has no this pointer, so it can only use static members.
#include <iostream>
#include <string>
using namespace std;
class Student {
string name;
int id;
static int count; // one shared counter
public:
Student(const string &n) : name(n), id(++count) {}
~Student() { count--; }
void show() const { cout << "#" << id << " " << name << endl; }
static int total() { return count; } // no object needed
};
int Student::count = 0; // definition of the static member
int main() {
cout << "students: " << Student::total() << endl;
Student a("Asha"), b("Ravi");
{
Student c("Meera");
c.show();
cout << "students: " << Student::total() << endl;
}
a.show(); b.show();
cout << "students: " << Student::total() << endl;
return 0;
}students: 0 #3 Meera students: 3 #1 Asha #2 Ravi students: 2
| Regular member | static member | |
|---|---|---|
| Copies | one per object | one per class |
| Accessed via | an object: s.name |
the class: Student::count |
Function has this? |
yes | no |
| Can use non-static members? | yes | no |
Test yourself
4 questions · pick an answer to check it instantly
- 1
What is
thisinside a member function?thispoints at the current object, sothis->xis that object'sx. - 2
How many copies of a static data member exist if you create 50 objects?
Static members belong to the class and are shared. - 3
Which member functions can be called on a
constobject?Non-const functions might modify the object, so they are not allowed on const objects. - 4
Why can a static member function not access non-static members?
A static function is called on the class, not on an object.