New Tutorials:   NUMPY    TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL    Matplotlib    C++ Programs
See the Tutorial List

C++ Performing basic Operations using Class

Hello Everyone!

In this tutorial, we will learn how to perform basic operations using Class and its members, in the C++ programming language.

To understand the concept of Class and its members, we will recommend you to visit here: C++ Class Concept, where we have explained it from scratch.

Code:

#include <iostream>

#include <vector>

using namespace std;

//defining the class operations to implement all the basic operations
class operations {
    //declaring member variables
    public:
        int num1, num2;

    //defining member function or methods
    public:
        void input() {
            cout << "Enter two numbers to perform operations on: \n";
            cin >> num1 >> num2;
            cout << "\n";
        }

    public:
        void addition() {
            cout << "Addition = " << num1 + num2;
            cout << "\n";
        }

    void subtraction() {
        cout << "Subtraction = " << num1 - num2;
        cout << "\n";
    }

    void multiplication() {
        cout << "Multiplication = " << num1 * num2;
        cout << "\n";
    }

    void division() {
        cout << "Division = " << (float) num1 / num2;
        cout << "\n";
    }
};

//Defining the main method to access the members of the class
int main() {
    cout << "\n\nWelcome to Studytonight :-)\n\n\n";
    cout << " =====  Program to perform basic operations using Class, in CPP  ===== \n\n";

    //Declaring class object to access class members from outside the class
    operations op;

    cout << "\nCalling the input() function from the main() method\n";
    op.input();

    cout << "\nCalling the addition() function from the main() method\n";
    op.addition();

    cout << "\nCalling the subtraction() function from the main() method\n";
    op.subtraction();

    cout << "\nCalling the multiplication() function from the main() method\n";
    op.multiplication();

    cout << "\nCalling the division() function from the main() method\n";
    op.division();

    cout << "\nExiting the main() method\n\n\n";

    return 0;
}

Output:

C++ Class operations

We hope that this post helped you develop a better understanding of the concept of Class in C++. For any query, feel free to reach out to us via the comments section down below.

Keep Learning : )