Sample C++ Project Code to Pass Parameters to Member Functions of C++ Class

Passing parameters to C++ Class make them reusable as displayed by the sample project in this post. A Basic C++ Class is created in this Project and then parameters are passed to the member function. This Class is the basic building block of passing in more types of parameters and data types. Apart from data type changes, there are lots of other useful stuff that can be done with parameters of member functions of a C++ class.

C++ Class with Parameter Passing Functions

C++ Class with Parameter Passing Functions

Have a close look at the above C++ class which has two functions in it. The functions of the class are created by creating an object of C++ Class and then calling the public function of the class. Apart from the class, this sample project uses the cout object provided by the iostream library in the  std namespace to write the text to the console window. The C++ Hello World object describes the steps in detail for creating a basic C++ project with Microsoft Visual Studio Express Edition.

// CFunctionsWithParameters.cpp : Defines the entry point for the console application.//TapKaa.com for More Tutorials#include "stdafx.h"#include using namespace std;class CFunctionsWithParameters{public:void DisplayValue(int cr_iValue){cout << "Value of the Parameter Passed in is " << cr_iValue << "n";}void AddAndDisplay(int cr_iNumber1, int cr_iNumber2){int vl_iSum=cr_iNumber1+cr_iNumber2;DisplayValue(vl_iSum);}};int _tmain(int argc, _TCHAR* argv[]){CFunctionsWithParameters vl_oClassObject;vl_oClassObject.DisplayValue(200);vl_oClassObject.AddAndDisplay(1234,456);return 0;}

You can use the above provided code to paste it in your C++ project to easily create the C++ project and learn C++ Member Functions accepting parameters. In case you have not guessed the output of this sample C++ project, do have a look at the given below screenshot which displays the output of the project in a console window.

Output of C++ Project to Pass Parameters to Member Functions

Output of C++ Project to Pass Parameters to Member Functions

Passing parameters to Member Functions of a class save you from writing the same code multiple times. Yes this is a really simple C++ Project and in actual programming , more complex scenarios will be there. The member Functions can accept parameters of multiple types, return values and much more. Once you get the basic idea of passing parameters to functions in C++, all other types of parameter passing will be really easy for you to learn and understand.