Calculating an area of triangle using the inline function C++ program example

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Calculating an area of triangle using the inline function C++ program example

To show: How to use the inline function in C++ programming to calculate the area of a triangle

// calculate an area of triangle using the inline function

#include <iostream>

using namespace std;

// inline function, no need prototype

inline float triangle_area(float base, float height)

{

float area;

area = (0.5 * base * height);

return area;

}

int main(void)

{

float b, h, a;

b = 4;

h = 6;

// compiler will substitute the inline function code here.

a = triangle_area(b, h);

cout<<"Area = (0.5*base*height)"<<endl;

cout<<"where, base = 4, height = 6"<<endl;

cout<<"\nArea = "<<a<<endl;

return 0;

}

Output example:

Area = (0.5*base*height)

where, base = 4, height = 6

Area = 12

Press any key to continue . . .