Introduction to Classes and Objects in C++
1. What are Classes and Objects?
In C++, classes and objects are fundamental concepts of object-oriented programming (OOP). A class is a blueprint for creating objects, and an object is an instance of a class. Classes encapsulate data for the object and methods to manipulate that data.
2. Defining a Class
To define a class in C++, you use the keyword class followed by the class name and a pair of curly braces. Here is the syntax:
class ClassName { // Access specifier public: // Data members int data; // Member functions void functionName() { // function body } };
Let's break it down:
- Access specifier: Determines the access level of class members. Common specifiers are public, private, and protected.
- Data members: Variables that hold data specific to the object.
- Member functions: Functions that define the behavior of the objects.
3. Creating Objects
An object is created from a class. Here is how you can create an object:
ClassName objectName;
Here is a complete example:
#include <iostream> class Car { public: std::string brand; std::string model; int year; void displayInfo() { std::cout << "Brand: " << brand << std::endl; std::cout << "Model: " << model << std::endl; std::cout << "Year: " << year << std::endl; } }; int main() { Car car1; car1.brand = "Toyota"; car1.model = "Corolla"; car1.year = 2020; car1.displayInfo(); return 0; }
In this example:
- We define a class Car with data members brand, model, and year.
- We define a member function displayInfo() to display the car's information.
- In the main() function, we create an object car1 of the Car class and set its data members.
- We call car1.displayInfo() to display the car's details.
4. Access Specifiers
Access specifiers define the accessibility of members of a class. The commonly used access specifiers are:
- public: Members are accessible from outside the class.
- private: Members are accessible only within the class.
- protected: Members are accessible within the class and by derived class(es).
Example:
class Example { public: int publicData; private: int privateData; protected: int protectedData; };
5. Member Functions
Member functions are functions that operate on the data members of the class. They are defined inside the class definition. Example:
class Rectangle { public: int length; int breadth; int area() { return length * breadth; } };
In this example, area() is a member function that calculates the area of the rectangle.
6. Constructors and Destructors
Constructors are special member functions that are called when an object is created. They initialize the object's data members. Example:
class Rectangle { public: int length; int breadth; // Constructor Rectangle(int l, int b) { length = l; breadth = b; } int area() { return length * breadth; } };
Destructors are special member functions that are called when an object is destroyed. They clean up resources allocated to the object. Example:
class Example { public: // Constructor Example() { std::cout << "Constructor called" << std::endl; } // Destructor ~Example() { std::cout << "Destructor called" << std::endl; } };
7. Example Program
Here is a complete example program that demonstrates the concepts of classes and objects:
#include <iostream> class Rectangle { public: int length; int breadth; // Constructor Rectangle(int l, int b) { length = l; breadth = b; } int area() { return length * breadth; } }; int main() { Rectangle rect1(10, 5); std::cout << "Area of rectangle: " << rect1.area() << std::endl; return 0; }
Output: