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.
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 LabEx {
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 LabEx :-)\n\n\n";
cout << " ===== Program to demonstrate the concept of Class, in CPP ===== \n\n";
LabEx 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 LabEx 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 LabEx :-)
===== 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 LabEx {
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 LabEx :-)\n\n\n";
cout << " ===== Program to demonstrate the concept of Class, in CPP ===== \n\n";
LabEx 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;
}



