OOP Classes & Objects

What is 'class' & object in C++?

* Object-Oriented Programming (OOP) is a paradigm that allows you to structure your code in terms of classes and objects.

Classes

A class is a user-defined data type in C++ that represents a blueprint for creating objects. It encapsulates data members (variables) and member functions (methods) that operate on the data. The general syntax for a class definition is as follows:

class ClassName {
  // Data members (variables)
  private:
    dataType member1;
    dataType member2;

  // Member functions (methods)
  public:
    returnType methodName(parameters);
};

Here's an example of a simple 'Rectangle' class:

class Rectangle {
  private:
    int length;
    int width;

  public:
    void setDimensions(int len, int wid) {
      length = len;
      width = wid;
    }

    int calculateArea() {
      return length * width;
    }
};

Objects

An object is an instance of a class. It is a real-world entity that can be identified by a unique set of characteristics. Objects are created from a class, and they represent a specific occurrence of the class. To create an object, you use the following syntax:

ClassName objectName;

Here's an example of creating objects from the 'Rectangle' class:

int main() {
  Rectangle rectangle1, rectangle2; // Creating objects

  rectangle1.setDimensions(5, 10); // Setting dimensions for rectangle1
  rectangle2.setDimensions(3, 7);   // Setting dimensions for rectangle2

// Calculating area for rectangle1
  int area1 = rectangle1.calculateArea();

// Calculating area for rectangle2
  int area2 = rectangle2.calculateArea(); 

  // Outputting results
  cout << "Area of rectangle1: " << area1 << endl;
  cout << "Area of rectangle2: " << area2 << endl;

  return 0;
}

Here's the full code for the 'Rectangle' class along with a simple program to demonstrate its usage:

cpp Copy Code
#include<iostream>

 //The Rectangle class
class Rectangle {
private:
    int length;
    int width;

public:
    void setDimensions(int len, int wid) {
        length = len;
        width = wid;
    }

    int calculateArea() {
        return length * width;
    }
};

int main() {
    Rectangle rectangle1, rectangle2; // Creating objects

    rectangle1.setDimensions(5, 10);
    rectangle2.setDimensions(3, 7); 

    int area1 = rectangle1.calculateArea(); 
    int area2 = rectangle2.calculateArea();

    // Outputting results
    std::cout << "Area of rectangle1: " << area1 << std::endl;
    std::cout << "Area of rectangle2: " << area2 << std::endl;

    return 0;
}

In this example, 'rectangle1' and 'rectangle2' are objects of the 'Rectangle' class. The 'setDimensions' method is used to set the dimensions for each rectangle, and the 'calculateArea' method is used to compute the area.

Output:
Area of rectangle1: 50
Area of rectangle2: 21

* This is a basic introduction to classes and objects in C++. They are fundamental concepts in object-oriented programming, providing a way to model and organize code in a more modular and reusable manner.

What is New?

We have just updated this website & still working on adding new content & updating existing features.