Welcome to The Coding College! In this tutorial, we’ll explore classes and objects in C++, the fundamental building blocks of Object-Oriented Programming (OOP).
What Are Classes and Objects?
Class
A class is a blueprint or template for creating objects. It defines the properties (data) and behaviors (functions) that the objects created from the class will have.
Object
An object is an instance of a class. It represents a specific entity that possesses the attributes and behaviors defined by the class.
Defining a Class
To define a class in C++, use the class
keyword:
class ClassName {
public: // Access specifier
// Data members (attributes)
// Member functions (methods)
};
- Access Specifiers:
public
: Members are accessible from outside the class.private
: Members are accessible only within the class.protected
: Members are accessible within the class and derived classes.
Example: Creating a Class
#include <iostream>
using namespace std;
class Car { // Define a class
public:
string brand; // Attribute
int year; // Attribute
void displayInfo() { // Method
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};
Here, Car
is the class that defines the attributes brand
and year
and the method displayInfo()
.
Creating Objects
Once a class is defined, you can create objects (instances) of that class.
Example:
int main() {
Car car1; // Create an object of Car
car1.brand = "Tesla";
car1.year = 2023;
car1.displayInfo(); // Call the method
return 0;
}
Output:
Brand: Tesla, Year: 2023
Access Specifiers
By default, the members of a class are private. To allow access, you must use the public
access specifier.
Example:
#include <iostream>
using namespace std;
class Student {
private:
string name; // Private attribute
public:
void setName(string n) { // Setter method
name = n;
}
string getName() { // Getter method
return name;
}
};
int main() {
Student student1;
student1.setName("Alice"); // Access private member via public method
cout << "Student Name: " << student1.getName() << endl;
return 0;
}
Output:
Student Name: Alice
Constructors
A constructor is a special method that is automatically called when an object is created. It initializes the object’s attributes.
Example:
#include <iostream>
using namespace std;
class Car {
public:
string brand;
int year;
// Constructor
Car(string b, int y) {
brand = b;
year = y;
}
void displayInfo() {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};
int main() {
Car car1("BMW", 2020); // Create object and initialize using constructor
car1.displayInfo();
Car car2("Audi", 2022);
car2.displayInfo();
return 0;
}
Output:
Brand: BMW, Year: 2020
Brand: Audi, Year: 2022
Destructor
A destructor is a special method that is called automatically when an object is destroyed. It is defined using the ~
symbol before the class name.
Example:
#include <iostream>
using namespace std;
class Car {
public:
Car() {
cout << "Car object created!" << endl;
}
~Car() {
cout << "Car object destroyed!" << endl;
}
};
int main() {
Car car1; // Constructor is called
return 0; // Destructor is called
}
Output:
Car object created!
Car object destroyed!
Member Functions
Member functions define the behavior of a class. They can be declared inside the class or outside using the ::
scope resolution operator.
Example: Defining Member Functions Outside the Class
#include <iostream>
using namespace std;
class Car {
public:
string brand;
int year;
void displayInfo(); // Function declaration
};
// Function definition outside the class
void Car::displayInfo() {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
int main() {
Car car1;
car1.brand = "Ford";
car1.year = 2018;
car1.displayInfo();
return 0;
}
Practical Example: Student Management
#include <iostream>
using namespace std;
class Student {
private:
string name;
int age;
string grade;
public:
Student(string n, int a, string g) { // Constructor
name = n;
age = a;
grade = g;
}
void displayStudentInfo() {
cout << "Name: " << name << ", Age: " << age << ", Grade: " << grade << endl;
}
};
int main() {
Student student1("Alice", 20, "A");
Student student2("Bob", 22, "B");
student1.displayStudentInfo();
student2.displayStudentInfo();
return 0;
}
Output:
Name: Alice, Age: 20, Grade: A
Name: Bob, Age: 22, Grade: B
Advantages of Classes and Objects
- Modularity: Classes keep related data and methods together.
- Reusability: Classes can be reused to create multiple objects.
- Encapsulation: Data is hidden using private access specifiers.
- Easy Maintenance: Code is organized and easy to debug.
Explore More at The Coding College
To master C++ and learn advanced concepts, visit The Coding College.