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 thestdnamespace directly, without needing to prefix them withstd::. - Class Definition: The program defines a class named
ExampleClass. This class has three members:privatedata membersint ianddouble d, andpublicmember functionssetValuesanddisplayValues. - Constructor Definition: The constructor
ExampleClass()is defined inside the class. The constructor initializes the data membersianddto zero. - Member Function
setValues(): The member functionsetValues(int num, double val)is defined inside the class. It takes two arguments,numof typeintandvalof typedouble. This function sets the values of the data membersianddto the givennumandvalrespectively. - 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 membersianddusingcoutfrom thestdnamespace. - Main Function: The
main()function is the entry point of the program. It starts by creating an instance of theExampleClassnamedobj. - Using Class Member Functions: After creating the instance
obj, the program calls the member functions of theExampleClassto work with the data members. First,obj.setValues(42, 3.14);is called to set the values ofito 42 anddto 3.14. - Displaying Values: Next, the program calls
obj.displayValues();, which displays the values ofianddusingcoutstatements from thestdnamespace. - 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