Class and Object
Programming
Classes are user-defined types that encapsulate data (member variables) and functions (member functions). Objects are instances of classes. This is the foundation of object-oriented programming in C++.
Basic Example
#include <iostream>
class Rectangle {
public:
int length, width;
int area() {
return length * width;
}
};
int main() {
Rectangle rect;
rect.length = 5;
rect.width = 3;
std::cout << "Area of rectangle: " << rect.area() << std::endl;
return 0;
} Access Specifiers
C++ provides three access levels for class members:
- public: Accessible from anywhere
- private: Accessible only within the class (default for classes)
- protected: Accessible within the class and derived classes
Encapsulation Example
A better design uses private members with public accessors:
#include <iostream>
class Rectangle {
private:
int length;
int width;
public:
// Constructor
Rectangle(int l, int w) : length(l), width(w) {}
// Getters
int getLength() const { return length; }
int getWidth() const { return width; }
// Setters with validation
void setLength(int l) {
if (l > 0) length = l;
}
void setWidth(int w) {
if (w > 0) width = w;
}
// Member functions
int area() const {
return length * width;
}
int perimeter() const {
return 2 * (length + width);
}
};
int main() {
Rectangle rect(5, 3);
std::cout << "Area: " << rect.area() << std::endl;
std::cout << "Perimeter: " << rect.perimeter() << std::endl;
return 0;
} Constructors and Destructors
#include <iostream>
#include <string>
class Person {
private:
std::string name;
int age;
public:
// Default constructor
Person() : name("Unknown"), age(0) {
std::cout << "Default constructor called" << std::endl;
}
// Parameterized constructor
Person(const std::string& n, int a) : name(n), age(a) {
std::cout << "Parameterized constructor called" << std::endl;
}
// Destructor
~Person() {
std::cout << "Destructor called for " << name << std::endl;
}
void display() const {
std::cout << "Name: " << name << ", Age: " << age << std::endl;
}
};
int main() {
Person p1; // Default constructor
Person p2("Alice", 25); // Parameterized constructor
p2.display();
// Destructors called automatically when objects go out of scope
return 0;
} Key Concepts
- Class: A blueprint for creating objects
- Object: An instance of a class
- Encapsulation: Bundling data and methods together, hiding implementation details
- Constructor: Special member function called when an object is created
- Destructor: Special member function called when an object is destroyed
- Member Access: Use dot operator (
.) for objects, arrow operator (->) for pointers
Best Practices
- Keep member variables private and provide public accessors if needed
- Use constructors to initialize all member variables
- Mark member functions that don't modify state as
const - Follow the Rule of Three/Five/Zero for resource management
- Prefer initialization lists in constructors
- Use meaningful names for classes (typically PascalCase)
Common Pitfalls
- Forgetting to initialize member variables (use constructors)
- Exposing implementation details through public member variables
- Not marking const member functions as
const - Shallow copying when deep copy is needed (copy constructor/assignment)
- Memory leaks in destructors (always pair
newwithdelete)
Output
Area of rectangle: 15