4.2) Classes and Objects in C++
Classes and Objects are fundamental concepts in object-oriented programming (OOP) that allow you to create structured and modular code by modeling real-world entities as classes and creating instances of those classes as objects. Let’s delve into the details with code examples and explanations:
Table of Contents
1. Class Definition
A class is a blueprint that defines the structure and behavior of objects. It contains attributes (data members) and methods (functions) that describe the characteristics and actions of the objects.
#include <iostream>
#include <string>
// Class definition
class Person {
public:
// Attributes
std::string name;
int age;
// Method to display information
void displayInfo() {
std::cout << "Name: " << name << ", Age: " << age << std::endl;
}
};
2. Creating Objects
An object is an instance of a class. It encapsulates data (attributes) and can perform actions (methods) defined in the class.
int main() {
// Creating objects of the class
Person person1;
Person person2;
// Assigning values to attributes
person1.name = "Alice";
person1.age = 25;
person2.name = "Bob";
person2.age = 30;
// Calling methods on objects
person1.displayInfo();
person2.displayInfo();
return 0;
}
Output:
Name: Alice, Age: 25
Name: Bob, Age: 30
3. Access Control
C++ supports three access control modifiers: public, private, and protected.
By default, members are private.
class Student {
public:
std::string name; // Public attribute
int age; // Public attribute
private:
int rollNumber; // Private attribute
protected:
double gpa; // Protected attribute
};
4. Constructors and Destructors
Constructors are special methods that initialize object attributes when an object is created. Destructors are used to clean up resources when an object is destroyed.
class Book {
public:
std::string title;
std::string author;
// Constructor
Book(std::string t, std::string a) : title(t), author(a) {
std::cout << "Book created: " << title << " by " << author << std::endl;
}
// Destructor
~Book() {
std::cout << "Book destroyed: " << title << std::endl;
}
};
5. Using Constructors
Constructors can have parameters to initialize attributes during object creation.
int main() {
Book book1("The Great Gatsby", "F. Scott Fitzgerald");
Book book2("To Kill a Mockingbird", "Harper Lee");
return 0;
}
Output:
Book created: The Great Gatsby by F. Scott Fitzgerald
Book created: To Kill a Mockingbird by Harper Lee
Book destroyed: To Kill a Mockingbird
Book destroyed: The Great Gatsby
6. Member Functions
Member functions (methods) are functions defined within a class. They operate on the attributes of the class and can perform various actions.
class Calculator {
public:
// Member function to add two numbers
int add(int a, int b) {
return a + b;
}
};
Using Member Functions:
You can call member functions on objects to perform actions.
int main() {
Calculator calculator;
int result = calculator.add(5, 3);
std::cout << "Result: " << result << std::endl;
return 0;
}
Output:
Result: 8
Classes and objects provide a structured way to model and manipulate data in your programs. They promote encapsulation and allow you to design more organized and maintainable code. By defining classes, you create a blueprint for creating objects with well-defined attributes and behaviors.