Article by: Manish Methani
Last Updated: October 18, 2021 at 2:04pm IST
Member functions are the functions which are applied to the dataMembers of the class. Member functions can be declared inside a class or outside a class.
#include using namespace std; class Game { public: int i; //data variable void display(string str) { cout<
Save file as Game.cpp Open terminal Access to the directory where Game.cpp is stored. In terminal , then write this g++ Game.cpp Once the file is compiled successfully, write this in terminal ./a.out Snack Game Pacman Game
Firstly main() function is called . Then it creates first instance of Game class named sankeObject. The display Method is called where it prints the passed the string using parameter str. Then it creates second object of Game class named pacmanObject. Then it calls a display function again with different string "Pacman Game."
You can define the member functions outside the class using scope resolution operator written as two colons ::
#include using namespace std; class Game { public: int i; // data member void display(string str) ; // Member Function Declaration }; // Class ends here //Member Function Defination void Game :: display(string str) { cout<