Data Structures using C PlusPlus: Chapter 1: Data Abstraction and Overloading

Member Functions and Classes

Data Structures using C ++ Program

Question: Give the syntax and use of the reserved word inline with two examples.

Member Functions and Classes


1. Defining Member Functions Inside the Class

The class can contain the definition of the member functions. Usually these methods are defined as the public. Following program shows how to define the member functions inside the class.

#include<iostream>

using namespace std;

class Test

{

   private:

   int a,b;

   public:

// Member functions defined inside the class.

void get_data()

{

   cout<<"\n Enter the first number: ";

   cin>>a;

   cout<<"\n Enter the second number: ";

   cin>>b;

}

void sum()

{

   int c;

   c=a+b;

   cout<<"\n Sum of two numbers is: "<<c;

}

};

void main()

{

   Test obj;

   obj.get_data();

   obj.sum();

}

Output

Enter the first number: 10

Enter the second number: 20

Sum of two numbers is: 30

 

Note that the above defined functions are belonging to the class Test. Hence the data members of this class can be accessed by the methods defined within the Test class. That means the methods get_data and sum can access the variables a and b. If we define another class say Test1 then this class methods can not access variables a and b

Following program will raise error!!

#include<iostream>

using namespace std;

class Test

{

private:

   int a,b;

public:

void get_data()

{

   cout<<"\n Enter the first number: ";

   cin>>a;

   cout<<"\n Enter the second number: ";

   cin>>b;

}

void sum()

{

   int c;

   c=a+b;

   cout<<"\n Sum of two numbers is: "<<c;

}

};

class Test1

{

   int x;

   void mul()  // This method can not access the variables a and b of class Test

   {

      int c;

      c=x*a;

   }

};

void main()

{

   Test obj;

   Test1 obj1;

   obj.get_data();

   obj.sum();

   obj1,mul();

}

 

Example: 1

Implement ATM transaction in C++.

Solution :        

#include<iostream>

using namespace std;

class ATM

{

private:

   double balance,withdraw,deposit;

public:

void init()

{

   balance =1000; // Minimum required balance

}

void withdrawal()

{

   cout << "\n Enter the amount to be withdrawn ";

   cin>> withdraw;

   if (withdraw >= 1000)

      cout<<"\n Insufficient balance!!!";

   else

      balance = balance ‒ withdraw;

}

void deposition()

{

   cout << "\n Enter the amount to be deposited: ";

   cin >> deposit;

   balance = balance + deposit;

}

void display()

{

   cout<<"\n Balance Amount: "<<balance;

}

};

void main()

{

   char ans = 'y';

   int choice;

   ATM obj;

   obj.init();

   do

   {

      cout<<"\n Main Menu":

      cout << "\n 1. Withdraw \n 2. Deposit \n 3. Show Balance ";

      cout << "\n Enter your choice: ";

      cin >> choice:

      switch (choice)

      {

      case 1:obj.withdrawal();

      break;

      case 2:obj.deposition();

      break;

      case 3:obj.display();

      break;

   }

   cout<<"\n Do you want to continue?";

   cin >> ans;

} while (ans == 'y');

}

Output

Main Menu

1. Withdraw

2. Deposit

3. Show Balance

Enter your choice: 1

Enter the amount to be withdrawn 100

Do you want to continue? y

Main Menu

1. Withdraw

2. Deposit

3. Show Balance

Enter your choice: 3

Balance Amount: 900

Do you want to continue? y

Main Menu

1. Withdraw

2. Deposit

3. Show Balance

Enter your choice: 2

Enter the amount to be deposited: 500

Do you want to continue? y

Main Menu

1. Withdraw

2. Deposit

3. Show Balance

Enter your choice: 3

Balance Amount: 1400

Do you want to continue?n

 

2. Defining Member Functions Outside the Class

The members functions that needs to be defined outside the class are defined using the scope resolution operator. Following program illustrates it

/*********************************************************************

Program that uses a class where the member functions are defined outside a class

**********************************************************************/

#include<iostream>

using namespace std;

class Test

{

private:

   int a,b,c;

public:

   int Addition(int,int);

   void Display();

};

int Test::Addition(int a, int b)

{

   c=a+b;

   return c;

}

void Test::Display()

{ cout<"The sum is:" <c< "\n";}

void main()

{

   Test obj;

   obj.Addition(10,20);

   obj.Display();

}

Output

The sum is:30

 

Example: 2

Write a menu‒driven program to accept 2 integers and an operator (+,‒,*,%,/) and to perform the operation and print the result.

Solution:

#include<iostream>

using namespace std;

class Test

{

   int a,b,c

public:

   void get input();

   void addition();

   void multiplication();

   void division();

   void subtraction();

   void mod();

};

void Test::get_input()

{

   cout<<"\n Enter first number";

   cin>a;

   cout<<"\n Enter second number";

   cin>b;

}

void Test::addition()

{

   c=a+b;

   cout<<"\n The addition of two numbers is "<<c;

}

void Test::multiplication()

{

   c=a*b;

   cout<<"\n The multiplication of two numbers is "<<c;

}

void Test::subtraction()

{

   c=a‒b;

   cout<<"\n The subtraction of two numbers is "<<c;

}

void Test::division()

{

   c=a/b;

   cout<<"\n The division of two numbers is "<<c;

}

void Test::mod()

{

   c‒a%b;

   cout<<"\n The mod of two numbers is "<<c;

}

void main()

{

   Test obj;

   char opr

   char ans='y';

   do

   {

      cout<<"\n 1. + \n 2. ‒ \n 3. * \n 4. / \n 5. %";

      cout<<"\n Enter your choice":

      cin>>opr;

   switch(opr)

   {

   case '+':obj.get_input();

        obj.addition();

           break;

   case '*':obj.get_input();

           obj.multiplication();

           break;

   case '‒':obj.get_input();

           obj.subtraction();

           break;

   case '/':obj.get_input();

           obj.division();

           break;

   case '%':obj.get_input();

           obj.mod();

           break;

   }

   cout<<"\n Do you want to continue?";

   cin>>ans;

}while(ans=='y');

}

 

3. Inline Functions

When we define a function normally the compiler makes a copy of that definition in the memory. And when a call to that function is made the compiler jumps to those copied instructions and when the function returns, the execution resumes from the next line in the calling function. Hence if we make a call to that function for 5 times then each time the copied block of function in the memory will be referred by the compiler. This also means that there is only one copy of the function definition in the memory and on each call to that function the same copy is referred.

Now if there are many calls to that function and function contains very few lines of code then such a jumping to memory becomes a performance overhead for the compiler. It ultimately slows down the execution of the program. Hence the solution is to make the function inline.

Definition : The inline function is a function whose code is copied in place of each function call. The inline specifies that the compiler should insert the complete body of function in every context where the function is used.

For the inline function if the function is called for 5 times then for 5 times the code of the function is copied to the calling function and hence no jump is made to the memory. This improves the performance but increases the memory size because of the made on each call.

The fact is that if the function definition contains very few lines of code then making the function inline is always preferred otherwise it is not at all preferred to make the function inline because large code gets copied many times and large amount of space will be wasted by storage of the same code.

C++ Program

#include<iostream>

using namespace std;

inline int sum(int x,int y)

{

   return (x+y);

}

void main()

{

   int a,b;

   a=10;

   b=20;

   cout<<"The sum is "<<sum(a,b);

}

Output

The sum is 30

 

Difference between inline function and macro


Inline function

1. Inline function is a function whose code is copied in place of each function call.

2. The compilers handle the inline functions.

3. The inline functions perform type checking.

4. The inline functions can be used for debugging as they are handled during the compile time.

5. Inline functions may or may not be handled by the compiler. It is compiler's decision to handle the inline function.

6. It can be defined inside or outside the class.

Macro

1. Macro is a fragment of code which has been given a name. And whenever this name is used, it is replaced by the content of macro.

2. The preprocessors handle the macros.

3. Macros do not perform the type checking.

4. The macros can not be used for debugging as they are handled by preprocessor at pre compile time.

5. The macros need to be handled.

6. It can not be defined inside the class.

 

Review Question

1. Give the syntax and use of the reserved word inline with two examples.

 

Data Structures using C PlusPlus: Chapter 1: Data Abstraction and Overloading : Tag: Data Structure, C++, C Programing : Data Structures using C ++ Program - Member Functions and Classes


Data Structures using C PlusPlus: Chapter 1: Data Abstraction and Overloading



Under Subject


Data Structures using CPlusPlus

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Electron Devices

EC25C01 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Data Structures using CPlusPlus

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Circuits and Network Analysis

EC25C02 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Engineering Drawing - Laboratory

ME25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Data Structures using CPlusPlus - Laboratory

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Devices and Circuits Laboratory

EC25C03 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation