C++ Class Implementation

C++C++Beginner
Practice Now

Introduction

This lab will guide you through implementing the concept of Class and its members in C++ programming. The class is a user-defined data type that works as a blueprint for objects. Members of the class can be variable or functions, and they can be defined as public, private, or protected.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/OOPGroup(["`OOP`"]) cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/BasicsGroup -.-> cpp/data_types("`Data Types`") cpp/BasicsGroup -.-> cpp/operators("`Operators`") cpp/OOPGroup -.-> cpp/classes_objects("`Classes/Objects`") cpp/OOPGroup -.-> cpp/class_methods("`Class Methods`") cpp/OOPGroup -.-> cpp/access_specifiers("`Access Specifiers`") subgraph Lab Skills cpp/variables -.-> lab-96219{{"`C++ Class Implementation`"}} cpp/data_types -.-> lab-96219{{"`C++ Class Implementation`"}} cpp/operators -.-> lab-96219{{"`C++ Class Implementation`"}} cpp/classes_objects -.-> lab-96219{{"`C++ Class Implementation`"}} cpp/class_methods -.-> lab-96219{{"`C++ Class Implementation`"}} cpp/access_specifiers -.-> lab-96219{{"`C++ Class Implementation`"}} end

Create and Edit the Main Source File

First, we need to create and edit the main source file. Open the terminal and navigate to the project directory using the cd command:

cd ~/project

Create and open the main source file:

touch main.cpp

Add the following code to the file:

#include <iostream>
using namespace std;

class studyTonight {

    private:
        int value;

    public:
        void input() {
            cout << "Entering the input() function\n";
            cout << "Enter an integer you want to display: ";
            cin >> value;
            cout << "Exiting the input() function\n\n";
        }

        void display() {
            cout << "\nEntering the display() function\n";
            cout << "The value entered is: " << value << endl;
            cout << "Exiting the display() function\n\n";
        }
};

int main() {
    cout << "\n\nWelcome to Studytonight :-)\n\n\n";
    cout << " =====  Program to demonstrate the concept of Class, in CPP  ===== \n\n";

    studyTonight object;

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

    cout << "\nCalling the display() function from the main() method\n\n\n";
    object.display();

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

    return 0;
}

The code defines a studyTonight class with two member functions input() and display(). The input() function accepts input from the user and stores it in value, while the display() function outputs the stored value on the screen.

Compile and Run the Program

Compile the program by running the following command in the terminal:

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

Upon successful compilation and execution, you should see the following output:

Welcome to Studytonight :-)


 =====  Program to demonstrate the concept of Class, in CPP  =====


Calling the input() function from the main() method


Entering the input() function
Enter an integer you want to display: 5
Exiting the input() function


Calling the display() function from the main() method


Entering the display() function
The value entered is: 5
Exiting the display() function


Exiting the main() method

Summary

In this lab, you have learned how to define a class and its members, how to declare and initialize objects of a class, and how to access the class members using a constructor.

You can now use the concept of C++ class and its members to write advanced programs that require user-defined data types. This concept can also help you to achieve better code organization and improved readability by applying object-oriented programming (OOP) techniques.

Full code:

Don't forget to modify the path and filename according to your implementation.

#include <iostream>
using namespace std;

class studyTonight {

    private:
        int value;

    public:
        void input() {
            cout << "Entering the input() function\n";
            cout << "Enter an integer you want to display: ";
            cin >> value;
            cout << "Exiting the input() function\n\n";
        }

        void display() {
            cout << "\nEntering the display() function\n";
            cout << "The value entered is: " << value << endl;
            cout << "Exiting the display() function\n\n";
        }
};

int main() {
    cout << "\n\nWelcome to Studytonight :-)\n\n\n";
    cout << " =====  Program to demonstrate the concept of Class, in CPP  ===== \n\n";

    studyTonight object;

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

    cout << "\nCalling the display() function from the main() method\n\n\n";
    object.display();

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

    return 0;
}

Other C++ Tutorials you may like