Object-oriented programming#
In procedural C, data (structs) and the functions that use it are separate. Object-oriented programming (OOP) puts them together into objects. A bank account object holds its balance and knows how to deposit and withdraw.
The four pillars of OOP:
| Pillar | Idea | C++ feature |
|---|---|---|
| Encapsulation | keep data and its functions together; hide the internals | class, private, public |
| Abstraction | show what an object does, hide how | public interface, abstract classes |
| Inheritance | build new classes from existing ones | class Car : public Vehicle |
| Polymorphism | one interface, many forms | overloading, virtual functions |
Class एक नक्शा (blueprint) है - जैसे मकान का नक्शा। Object उस नक्शे से बना असली मकान है। एक class से कितने भी objects बना सकते हैं, और हर object का अपना data होता है (हर मकान का अपना रंग, अपने लोग)।
Defining a class#
class Student {
private: // only member functions can touch these
string name;
int marks;
public: // anyone can call these
void setData(string n, int m);
void show();
};
Kavya scored 92 (grade A) Harsh scored 0 (grade F)
Because marks is private, the only way to set it is through setData, which validates it. That is encapsulation: the class protects its own data.
Access specifiers#
| Specifier | Accessible from |
|---|---|
public |
anywhere |
private |
only inside the class (and friends) |
protected |
inside the class and its derived (child) classes |
class and struct are almost identical in C++. The only difference: members are private by default in a class and public by default in a struct. Use struct for plain data, class for objects with behaviour.
Getters and setters, and const member functions#
36.6 C = 97.88 F Clamped to -273.15
A const after the parameter list promises the function does not change the object - and lets it be called on const objects.
Defining member functions outside the class#
In bigger programs the class declaration goes in a .h file and function bodies in a .cpp file, using the scope resolution operator :::
Area: 12.5664
The this pointer#
Inside a member function, this points to the object the function was called on. It is handy when a parameter has the same name as a member, and for method chaining:
5 x Notebook
Static members#
A static data member belongs to the class, not to any one object - all objects share it. A static member function can be called with ClassName::function():
Ticket #1001 - Asha Ticket #1002 - Imran Ticket #1003 - Priya Tickets issued: 3
Friend functions#
A friend function is not a member, but is allowed to access private members. Use it sparingly - for example when a function needs the internals of two different classes:
First box is wider
(Box(double w) : width(w) {} is a constructor - covered in the next lesson.)
Summary#
- A class bundles data members and member functions; objects are instances of a class.
privatedata +publicmethods = encapsulation; validate data inside setters.- Mark read-only methods
const; define methods outside withClassName::. thispoints to the current object;staticmembers are shared; friends can see private members.