Basic OOP Program Structure Using Class in C++ Programming Language.
In this C++ OOP Program, we will learn to define a class with necessary features like member variables, constructor and member functions. The class ‘ExampleClass’ and main() function will be in the sme cpp file. We will create object of ExampleClass in main() function and call the member functions.
Source Code of Basic C++ Object Oriented Program
#include <iostream>
using namespace std;
class ExampleClass {
private:
int i;
double d;
public:
// Constructor
ExampleClass() {
i = 0;
d = 0.0;
}
// Member function to set values
void setValues(int num, double val) {
i = num;
d = val;
}
// Member function to display values
void displayValues() {
cout << “Integer value: ” << i << endl;
cout << “Double value: ” << d << endl;
}
};
int main() {
// Create an instance of the ExampleClass
ExampleClass obj;
// Call member functions
obj.setValues(42, 3.14);
obj.displayValues();
return 0;
}
Output:
Integer value: 42
Double value: 3.14
Explanation of Source Code
Let’s go through the C++ program step by step and explain each part:
- Include Header Files: The program starts by including the necessary header file,
<iostream>
. This header provides functionality for input and output operations. - Using Namespace std: The
using namespace std;
statement allows us to use elements from thestd
namespace directly, without needing to prefix them withstd::
. - Class Definition: The program defines a class named
ExampleClass
. This class has three members:private
data membersint i
anddouble d
, andpublic
member functionssetValues
anddisplayValues
. - Constructor Definition: The constructor
ExampleClass()
is defined inside the class. The constructor initializes the data membersi
andd
to zero. - Member Function
setValues()
: The member functionsetValues(int num, double val)
is defined inside the class. It takes two arguments,num
of typeint
andval
of typedouble
. This function sets the values of the data membersi
andd
to the givennum
andval
respectively. - Member Function
displayValues()
: The member functiondisplayValues()
is defined inside the class. It doesn’t take any arguments. This function displays the values of the data membersi
andd
usingcout
from thestd
namespace. - Main Function: The
main()
function is the entry point of the program. It starts by creating an instance of theExampleClass
namedobj
. - Using Class Member Functions: After creating the instance
obj
, the program calls the member functions of theExampleClass
to work with the data members. First,obj.setValues(42, 3.14);
is called to set the values ofi
to 42 andd
to 3.14. - Displaying Values: Next, the program calls
obj.displayValues();
, which displays the values ofi
andd
usingcout
statements from thestd
namespace. - Return Statement: Finally, the
main()
function returns 0, indicating successful program execution.
When you run the program, it will create an instance of the ExampleClass
, set its member variables i
and d
, and then display these values on the console. In this example, the output will be:
Integer value: 42
Double value: 3.14