Object-Oriented Programming in C++

C++ programming language supports both procedural-oriented and object-oriented programming. The above example is based on the procedural-oriented programming paradigm. So let’s take another example to discuss Object Oriented Programming in C++.

1. Class

A class is a template of an object. For example, the animal is a class & dog is the object of the animal class. It is a user-defined data type. A class has its own attributes (data members) and behavior (member functions). The first letter of the class name is always capitalized & use the class keyword for creating the class.

In line #3, we have declared a class named Calculate and its body expands from line #3 to line #7.

Syntax:

class class_name{

// class body

};

To know more about classes in C++, please refer to this article.

2. Data Members & Member Functions

The attributes or data in the class are defined by the data members & the functions that work on these data members are called the member functions.

Example:

class Calculate{

public:
int num1 = 50; // data member
int num2 = 30; // data member

// member function
int addition() {
int result = num1 + num2;
cout << result << endl;
}
};

In the above example, num1 and num2 are the data member & addition() is a member function that is working on these two data members. There is a keyword here public that is access modifiers. The access modifier decides who has access to these data members & member functions. public access modifier means these data members & member functions can get access by anyone.

3. Object

The object is an instance of a class. The class itself is just a template that is not allocated any memory. To use the data and methods defined in the class, we have to create an object of that class.

They are declared in the similar way we declare variables in C++.

Syntax:

class_name object_name;

We use dot operator ( . ) for accessing data and methods of an object.

To know more about objects in C++, please refer to the article – C++ Classes and Objects.

Now, let’s execute our code to see the output of the program.

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

class Calculate{
    
      // Access Modifiers
    public:
          // data member
        int num1 = 50;
        int num2 = 30;
        
          // member function
        int addition() {
            int result = num1 + num2;
            cout << result << endl;
        }
};

int main() {
    
    // object declaration
    Calculate add;
      // member function calling
    add.addition();

    return 0;
}

Output
80


C++ Basic Syntax

C++ is a general-purpose, object-oriented programming language. It was developed in 1979 by Bjarne Stroustrup at AT & T Bell Laboratory. It is a high-level programming language & advanced version of C programming language. As Compared to other programming languages like Java, and Python, it is the fastest programming language.

Similar Reads

What is Syntax?

Syntax refers to the rules and regulations for writing statements in a programming language. They can also be viewed as the grammatical rules defining the structure of a programming language....

Basic Syntax of a C++ Program

We can learn about basic C++ Syntax using the following program....

Object-Oriented Programming in C++

C++ programming language supports both procedural-oriented and object-oriented programming. The above example is based on the procedural-oriented programming paradigm. So let’s take another example to discuss Object Oriented Programming in C++....