C++ Program to Demonstrate Class and Object

In C++, classes and objects form the foundation of Object-Oriented Programming (OOP). A class is a blueprint for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods). An object is an instance of a class. This article will guide you through the concept and implementation of classes and objects in C++ with detailed explanations and examples.

Prerequisites

Before diving into the implementation, ensure you have a basic understanding of the following concepts:

  1. Basic C++ Syntax: Familiarity with C++ syntax, including variables, functions, and basic control structures (if-else, loops).
  2. Object-Oriented Programming (OOP): Basic understanding of OOP principles such as encapsulation, abstraction, and inheritance.

1. Introduction to Classes and Objects

1.1 Definition

  • Class: A user-defined data type that contains data members (variables) and member functions (methods) that operate on the data. It defines properties and behaviors for the objects.
  • Object: An instance of a class. When a class is defined, no memory is allocated until an object of that class is created.

1.2 Syntax

The syntax for defining a class is as follows:

C++
class ClassName {
public:
    // Data members
    int data;

    // Member functions
    void display() {
        // Function definition
    }
};

2. Implementation of Classes and Objects in C++

2.1 Example 1: Basic Class and Object

Let’s start with a basic example to demonstrate the use of a class and object in C++.

C++
#include <iostream>
using namespace std;

class MyClass {
public:
    int data;

    void display() {
        cout << "Data: " << data << endl;
    }
};

int main() {
    MyClass obj;  // Creating an object of MyClass
    obj.data = 42;  // Assigning value to data member
    obj.display();  // Calling member function

    return 0;
}

Output:

C++
Data: 42

Explanation:

  • A class MyClass is defined with a data member data and a member function display().
  • An object obj of MyClass is created.
  • The data member data is assigned a value, and the member function display() is called to print the value.

2.2 Example 2: Parameterized Constructor

This example demonstrates the use of a parameterized constructor to initialize data members of an object.

C++
#include <iostream>
using namespace std;

class MyClass {
private:
    int data;
public:
    MyClass(int d) : data(d) {}  // Parameterized constructor

    void display() {
        cout << "Data: " << data << endl;
    }
};

int main() {
    MyClass obj(42);  // Creating an object with initial value
    obj.display();  // Calling member function

    return 0;
}

Output:

C++
Data: 42

Explanation:

  • A parameterized constructor MyClass(int d) is defined to initialize the data member data.
  • An object obj is created with the initial value 42, and the member function display() is called to print the value.

2.3 Example 3: Class with Multiple Member Functions

This example shows a class with multiple member functions demonstrating different operations.

C++
#include <iostream>
using namespace std;

class Rectangle {
private:
    int width, height;
public:
    void setDimensions(int w, int h) {
        width = w;
        height = h;
    }

    int area() {
        return width * height;
    }

    int perimeter() {
        return 2 * (width + height);
    }
};

int main() {
    Rectangle rect;
    rect.setDimensions(10, 5);
    cout << "Area: " << rect.area() << endl;
    cout << "Perimeter: " << rect.perimeter() << endl;

    return 0;
}

Output:

C++
Area: 50
Perimeter: 30

Explanation:

  • A class Rectangle is defined with private data members width and height.
  • Public member functions setDimensions(), area(), and perimeter() are defined to set dimensions, calculate area, and calculate perimeter, respectively.
  • An object rect of Rectangle is created, dimensions are set, and the area and perimeter are calculated and printed.

2.4 Example 4: Class with Dynamic Memory Allocation

This example demonstrates a class with dynamic memory allocation and a destructor to release resources.

C++
#include <iostream>
using namespace std;

class MyClass {
private:
    int* data;
public:
    MyClass(int size) {
        data = new int[size];
        cout << "Constructor: Memory allocated for array of size " << size << endl;
    }

    ~MyClass() {
        delete[] data;
        cout << "Destructor: Memory deallocated." << endl;
    }
};

int main() {
    MyClass obj(10);
    return 0;
}

Output:

C++
Constructor: Memory allocated for array of size 10
Destructor: Memory deallocated.

Explanation:

  • The constructor allocates memory for an array of integers.
  • The destructor deallocates the memory when the object obj is destroyed, ensuring there are no memory leaks.

Conclusion

Classes and objects are fundamental concepts in C++ and Object-Oriented Programming. They provide a way to encapsulate data and functions that operate on that data. This article provided a comprehensive guide to the use of classes and objects in C++, demonstrating their application through examples of basic classes, parameterized constructors, multiple member functions, and dynamic memory allocation.