What are Classes and Objects?
Definition: A class is a user-defined data type that serves as a blueprint for creating objects. It encapsulates data (attributes) and methods (behaviors) related to a specific entity. An object, on the other hand, is an instance of a class, representing a unique entity with its own state and behavior.
Defining Classes in C++
Syntax: In C++, classes are defined using the class keyword followed by the class name and a pair of curly braces enclosing the class definition.
Example:
class Rectangle {
private:
double length;
double width;
public:
// Member functions declaration
double calculateArea();
double calculatePerimeter();
};
Creating Objects
Instantiation: Objects of a class are created using the class name followed by parentheses, similar to calling a function.
Example:
Rectangle myRectangle; // Creating an object of the Rectangle class
Difference between Rectangle myRectangle
and Rectangle myRectangle = new Rectangle;
1 Rectangle myRectanlge;
- This syntax creates an object of the Rectangle class using stack memory allocation.
- The object is created directly on the stack, and its memory is automatically managed by the program.
- No dynamic memory allocation is involved, and the object's lifetime is tied to the scope in which it is declared.
- This syntax is suitable for creating objects with automatic storage duration, meaning they are automatically destroyed when they go out of scope.
Rectangle myRectangle; // Object created on the stack
2 Rectangle *myRectangle = new Rectangle;
- This syntax creates an object of the Rectangle class using heap memory allocation.
- This line declares a pointer variable named
myRectangle
of typeRectangle*
, which is a pointer to an object of typeRectangle
. - The new keyword is used to allocate memory dynamically on the heap, and it returns a pointer to the allocated memory.
- The object is created on the heap, and its memory must be manually managed by the programmer using delete to deallocate it when it's no longer needed.
- Objects created with dynamic memory allocation have a potentially longer lifetime and can exist beyond the scope in which they are created.
Rectangle *myRectangle = new Rectangle; // Object created on the heap
Accessing Members
Data Members: The data members (attributes) of an object can be accessed and modified using the dot (.) operator.
Member Functions: Similarly, member functions (methods) of an object can be invoked using the dot (.) operator.
Example:
myRectangle.length = 5.0;
myRectangle.width = 3.0;
double area = myRectangle.calculateArea();
Constructor and Destructor
Constructor: Constructors are special member functions that initialize the object's state when it is created. They have the same name as the class and no return type.
Destructor: Destructors are special member functions that are called when an object is destroyed. They clean up resources allocated by the object.
Example:
class Rectangle {
public:
// Constructor
Rectangle(double len, double wid) {
length = len;
width = wid;
}
// Destructor
~Rectangle() {
// Cleanup code
}
};
Encapsulation and Access Specifiers
Encapsulation: Encapsulation is the bundling of data and methods within a class, hiding the internal implementation details from the outside world.
Access Specifiers: C++ provides three access specifiers (public, private, and protected) to control the access to class members.
Example:
class Rectangle {
private:
double length;
double width;
public:
// Member functions declaration
double calculateArea();
double calculatePerimeter();
};