C++ Class with Multiple Functions

A C++ Class can have multiple member functions of various types. Each Sample C++ Projects on this website explain specific functionality of C++ and can be a quick reference to use whenever you are struck with something specific and find answer here along with code snipplet. The Sample Code presented in this post displays how Visual Studio helps you in writing code when multiple functions of a C++ class have same name. This Sample C++ Project also displays how to return value from a function and also displays how to use data stored in member variables.

Sample C++ Code with Class Containing Multiple Functions

Sample C++ Code with Class Containing Multiple Functions

The above screenshot displays a sample C++ Class which has a private member variable, public constructor used to initialize the member variable, and 3 other member functions. Note that two member functions of the class have the same name but different parameters and the third member function is returning an integer value and using the ++ operator.

Sample C++ Code to Call Multiple Functions of the C++ Class

Sample C++ Code to Call Multiple Functions of the C++ Class

The above screenshot displays the main function of the console application which is calling the public member functions of the class created above. Note that the object is not trying to read the value of the private member variable and instead member functions are reading the value of member variable and modifying the value of the member variable.

Visual Studio Integrated Development Environment Helping Coder to Write Code

Visual Studio Integrated Development Environment Helping Coder to Write Code

As the class used in the sample has multiple functions with same name, the Visual Studio IDE is helping to write the code which can be of great help when multiple functions in the same class have different types of parameters. The post Pass Parameters to Member Functions of a C++ Class described how to pass parameters to the member functions of a C++ class and that information is used in this project as well.

Now the final thing remaining is the output of this sample C++ project. The given below console screenshot displays the output of this sample c++ project. In case you do not understand the logic and want to delve deep into the working of this sample C++ project, read on the C++ Debugging post and know what’s happening inside this sample C++ project.

Console Output of Sample C++ Project with Class Containing Multiple Functions

Console Output of Sample C++ Project with Class Containing Multiple Functions

Full Source Code of this Sample C++ Project is provided below. Feel free to do changes to the source code in order to fully understand and learn how you can easily create multiple functions in a C++ class with same name and different input parameters. Also remember that C++ Compiler is there to help you learn C++ as fast as possible and treat error as your help , interpret them appropriately and proceed ahead.

// CFunctionTypes.cpp : Defines the entry point for the console application.//Visit TapKaa.com for More C++ Sample Code and Tutorials#include "stdafx.h"#include <iostream>using namespace std;class CFunctionTypes{int m_iMemberVariable;public:CFunctionTypes(){m_iMemberVariable=0;}void DisplayValue(){cout <<"Value of the Member Variable is " << m_iMemberVariable<< "n";}void DisplayValue(int cr_iNewValue){m_iMemberVariable=cr_iNewValue;DisplayValue();}int GetIncrementedValue(){return ++m_iMemberVariable;//Return Incremented Value of m_iMemberVariable//return m_iMemberVariable++;//Return Value of m_iMemberVariable and then Increment it}};int _tmain(int argc, _TCHAR* argv[]){CFunctionTypes vl_oClassObject;int vl_iNewValue;vl_oClassObject.DisplayValue();vl_iNewValue=vl_oClassObject.GetIncrementedValue();vl_oClassObject.DisplayValue(vl_iNewValue);return 0;}