C++ Passing arguments to functions

The Class is the fundamental building block of the object-oriented programming language C++. A class serves as a kind of blueprint for building objects with functions and variables. Since the objects are instances of a Class, they share the same attributes and functions as the Class.

Syntax:

Syntax:

class ClassName {
   access_specifier:
      // data members
      // member functions
};

Example:

class Rectangle {
   public:
      double length;
      double width;
      
      double area() {
         return length * width;
      }
};

The Class “Rectangle” in the example above has the two data members “length” and “width” as well as the member function “area().” The access specifier “public” establishes the visibility of the data members and member functions.

Creating Objects:

Once a Class is defined, we can create objects of that Class using the following syntax:

ClassName objectName;

Accessing Data Members and Member Functions:

We can access the data members and member functions of an object using the dot (.) operator.

Example:

Rectangle rect1;
rect1.length = 5.0;
rect1.width = 3.0;

double area = rect1.area();
cout << "Area of Rectangle is: " << area << endl;

The object “rect1” of the Class “Rectangle” was formed in the example above, and its data members “length” and “width” were set to 5.0 and 3.0, respectively. Finally, we used the object “rect1” to execute the member function “area()” and set the return value of that method to the variable “area”. The value of “area” was then printed on the console.