Basic OOP Program Structure Using Class

By | July 29, 2023

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:

  1. Include Header Files: The program starts by including the necessary header file, <iostream>. This header provides functionality for input and output operations.
  2. Using Namespace std: The using namespace std; statement allows us to use elements from the std namespace directly, without needing to prefix them with std::.
  3. Class Definition: The program defines a class named ExampleClass. This class has three members: private data members int i and double d, and public member functions setValues and displayValues.
  4. Constructor Definition: The constructor ExampleClass() is defined inside the class. The constructor initializes the data members i and d to zero.
  5. Member Function setValues(): The member function setValues(int num, double val) is defined inside the class. It takes two arguments, num of type int and val of type double. This function sets the values of the data members i and d to the given num and val respectively.
  6. Member Function displayValues(): The member function displayValues() is defined inside the class. It doesn’t take any arguments. This function displays the values of the data members i and d using cout from the std namespace.
  7. Main Function: The main() function is the entry point of the program. It starts by creating an instance of the ExampleClass named obj.
  8. Using Class Member Functions: After creating the instance obj, the program calls the member functions of the ExampleClass to work with the data members. First, obj.setValues(42, 3.14); is called to set the values of i to 42 and d to 3.14.
  9. Displaying Values: Next, the program calls obj.displayValues();, which displays the values of i and d using cout statements from the std namespace.
  10. 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

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *