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.