Classes and Objects in C++
![Picture of the author](/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Fauthor-profile-picture.8832aca3.jpg&w=1120&q=90)
- Published on
![Classes and Objects in C++](/_next/image?url=%2Fphotos%2Fblog-prototype.jpg&w=1120&q=90)
Introduction
Classes and Objects are the fundamental building blocks of Object-Oriented Programming (OOP) in C++. A class serves as a blueprint for objects, while an object is an instance of a class. This paradigm enables developers to model real-world entities, encapsulating their attributes and behaviors within classes.
Defining a Class
A class is defined using the class
keyword, followed by the class name and the class body enclosed in curly braces {}
.
class Car {
public:
string brand;
string model;
int year;
};
Creating Objects
Objects are instances of classes and can be created by declaring the class name followed by the object name.
Car car1;
car1.brand = "Toyota";
car1.model = "Corolla";
car1.year = 2020;
Utilizing Class Members
Member Functions
Member functions are functions defined within a class and are used to access object data.
class Calculator {
public:
int add(int a, int b) {
return a + b;
}
};
Calculator calc;
std::cout << calc.add(5, 10) << std::endl; // Output: 15
Constructors
Constructors are special member functions that are called when objects of a class are created. They usually initialize object members.
class Circle {
public:
double radius;
Circle(double r) {
radius = r;
}
double area() {
return 3.14159 * radius * radius;
}
};
Circle circle1(5.0); // Constructor is called here
std::cout << "Area: " << circle1.area() << std::endl; // Output: Area: 78.5398
Access Specifiers
- Public: Members are accessible from outside the class.
- Private: Members cannot be accessed (or viewed) from outside the class.
- Protected: Members cannot be accessed from outside the class, but they can be accessed in inherited classes.
Conclusion
Understanding classes and objects is pivotal to mastering C++ and OOP principles. By encapsulating data and behaviors within classes and creating objects to interact with those classes, developers can build modular, organized, and scalable applications.