Classes and Objects in C++
A class is a user-defined data type that acts as a blueprint for creating objects. It bundles data (attributes) and functions (methods) that operate on that data into one unit. An object is a specific instance of a class — created from the blueprint.
Defining a Class
class ClassName {
public:
// data members (attributes)
// member functions (methods)
};
#include <iostream>
#include <string>
using namespace std;
class Dog {
public:
string name;
string breed;
int age;
void bark() {
cout << name << " says: Woof! Woof!" << endl;
}
};
Creating Objects
int main() {
Dog dog1;
dog1.name = "Bruno";
dog1.breed = "Labrador";
dog1.age = 3;
dog1.bark();
Dog dog2;
dog2.name = "Milo";
dog2.breed = "Poodle";
dog2.age = 2;
dog2.bark();
return 0;
}
Output:
Bruno says: Woof! Woof!
Milo says: Woof! Woof!Access Specifiers
Access specifiers define who can access the members of a class:
| Specifier | Access |
|---|---|
public | Accessible from anywhere — inside or outside the class |
private | Accessible only within the class itself |
protected | Accessible within the class and its derived (child) classes |
Private Members and Getter/Setter Functions
Making data private and providing controlled access through public functions is the essence of encapsulation:
class BankAccount {
private:
double balance;
public:
void setBalance(double amount) {
if (amount >= 0) balance = amount;
}
double getBalance() {
return balance;
}
};
int main() {
BankAccount acc;
acc.setBalance(5000);
cout << "Balance: " << acc.getBalance() << endl;
return 0;
}
Output:
Balance: 5000Member Functions Defined Outside the Class
Member functions can be defined outside the class using the scope resolution operator :::
class Rectangle {
public:
double length;
double width;
double area(); // declaration
};
double Rectangle::area() { // definition outside
return length * width;
}
int main() {
Rectangle r;
r.length = 6.0;
r.width = 4.0;
cout << "Area = " << r.area() << endl;
return 0;
}
Output:
Area = 24The this Pointer
Inside any member function, the this pointer refers to the current object. It is useful when parameter names conflict with member names:
class Circle {
private:
double radius;
public:
void setRadius(double radius) {
this->radius = radius; // 'this' resolves ambiguity
}
double getArea() {
return 3.14 * radius * radius;
}
};
Array of Objects
class Point {
public:
int x, y;
void show() {
cout << "(" << x << ", " << y << ")" << endl;
}
};
int main() {
Point pts[3] = {{1,2}, {3,4}, {5,6}};
for (int i = 0; i < 3; i++) {
pts[i].show();
}
return 0;
}
Output:
(1, 2)
(3, 4)
(5, 6)Objects as Function Arguments
class Box {
public:
int volume;
};
void displayVolume(Box b) {
cout << "Volume: " << b.volume << endl;
}
int main() {
Box b;
b.volume = 120;
displayVolume(b);
return 0;
}
Key Takeaways
- A class is a blueprint; objects are created from it.
- Class members can be
public,private, orprotected. - Private data members should be accessed via public getter and setter functions.
- The scope resolution operator
::is used to define member functions outside the class. - The
thispointer refers to the current object inside member functions.
