Skip to main content

ProwessApps

Learn · Practice · Excel

WAP using inline function to find out the area of rectangle, circle, triangle & Square

C++ Code Example — Miscellaneous Programs

ADVERTISEMENT

WAP using inline function to find out the area of rectangle, circle, triangle & Square

Objective

Write a C++ program to calculate the area of shapes using Inline Functions.

Algorithm / Approach

  1. Create a class Area.
  2. Prefix the class member functions with the inline keyword.
  3. Write the short area calculation code directly inside the functions.
  4. Call the functions from main().
main.cpp
#include<iostream>
using namespace std;
class Area {
 double ar;
 public :
 inline void areaCircle(int a) {
  ar = 3.14*a*a;
  cout<<"Area of Circle = "<< ar;
  cout<< endl;
 }
 inline void areaRec(int a, int b) {
  ar = a*b;
  cout<<"Area of Rectangle "<< ar;
  cout<< endl;
 }
 inline void areaTri(int a, int b) {
  ar = a*b/2;
  cout<<"Area of Triangle "<< ar;
  cout<< endl;
 }
 inline void areaSquare(int a) {
  ar = a*a;
  cout<<"Area of Square "<< ar;
  cout<< endl;
 }
};
int main() {
 Area a;
 int r,l,b,s,s1,s2;
 cout<<"Enter Radius : ";
 cin>>r;
 cout<<"Enter Length , breadth : ";
 cin>>l>>b;
 cout<<"Enter Base and Height : ";
 cin>>s1>>s2;
 cout<<"Enter Side of Square : ";
 cin>>s;
 a.areaCircle(r);
 a.areaRec(l,b);
 a.areaTri(s1,s2);
 a.areaSquare(s);
 return 0;
}

Expected Output

Enter Radius : 7
Enter Length , breadth : 4 3
Enter Base and Height : 4 6
Enter Side of Square : 5
Area of Circle = 153.86
Area of Rectangle = 12
Area of Triangle = 12
Area of Square = 25

Explanation of the Program

  • Normally, when a program encounters a function call, it has to jump to a different part of memory, execute the code, and jump back. This jumping takes a tiny bit of time.
  • The inline keyword suggests to the compiler that it should take the actual code inside the function and physically paste it directly into main() wherever it was called, entirely eliminating the jump time. It is highly efficient for very short functions.

Complexity

Time Complexity O(1)
Space Complexity O(1)
ADVERTISEMENT