Create the Volume
class
First, create a new C++ file in the ~/project
directory with the name main.cpp
, and add the following code to define the Volume
class:
#include <iostream>
using namespace std;
//Class Volume to demonstrate the concept of Friend Function in CPP
class Volume {
//Member variables are declared as private and hence cannot be simply accessed from outside the class
private:
int liter;
//Initializing the value of variable liter to 2 using the default constructor
public:
Volume(): liter(2) {}
};
We define a Volume
class with a private member variable liter
which cannot be directly accessed from outside the class.