Bytebook Compiler

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.

4 min read3 runnable examples4-question quiz

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.

C++
#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;
}
Output
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):

C++
#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;
}
Output
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.

C++
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.

C++
#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;
}
Output
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. 1

    What is this inside a member function?

  2. 2

    How many copies of a static data member exist if you create 50 objects?

  3. 3

    Which member functions can be called on a const object?

  4. 4

    Why can a static member function not access non-static members?