Perform Basic Operations Using Class

Beginner

Introduction

In this lab, we will learn how to perform basic operations using class and its members in the C++ programming language. We will create a class 'operations' containing member variables and functions to implement basic operations such as addition, subtraction, multiplication, and division on two user-input numbers.

Create a new C++ file

Firstly, create a new C++ file named main.cpp in the ~/project directory using the following command in the terminal:

touch ~/project/main.cpp

Write the code

Copy and paste the following code into the main.cpp file using a text editor or an integrated development environment (IDE):

#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 functions or methods
 public:
     void input() {
         cout << "Enter two numbers to perform operations on: \n";
         cin >> num1 >> num2;
         cout << "\n";
     }

     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 C++ ===== \n\n";

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

     cout << "\n

: Call the division() function";

 op.division();

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

 return 0;

}

The code declares a class named 'operations' containing member variables and functions to perform basic operations on two user-input numbers. The main method is used to access the members of the class and call the required functions to perform basic operations on the user-input numbers.

Compile and run the code

To compile and run the code, use the following command in the terminal:

g++ ~/project/main.cpp -o main && ./main

This command will compile the main.cpp file and generate an executable named 'main'. The executable is then run, and the output will be displayed on the terminal.

You should see an output that looks like the following:

Welcome to Studytonight :-)


===== Program to perform basic operations using Class, in C++ =====

: Call the division() function

Division = 2

Exiting the main method

Summary

In this lab, we learned how to perform basic operations using class and its members in the C++ programming language. We created a class 'operations' containing member variables and functions to implement basic operations such as addition, subtraction, multiplication, and division on two user-input numbers. We also learned how to access the members of the class from outside the class and call the required functions to perform basic operations on the user-input numbers.

Other Tutorials you may like