#2-Function overloading(with examples) in C++-Program to find Area of Triangle using function overloading

c++ tutorials for beginners. c++ for beginners, c++ for computer engineers, c++ tutorials best, c++ expert tutorials
c++ tutorials for beginners. c++ for beginners, c++ for computer engineers, c++ tutorials best, c++ expert tutorials

Q) Write a program to calculate area of triangle

i) when sides are given

ii) when base and height are given

using the concept of function overloading

Its output will come like this.

But, I recommend you to type this program once in your codeblocks. You may think what happens just by typing; and you are right to some extent; but; as soon as you feel like you have understood something about the program, it is essential to type it so that you remember what was the flow of the program. You can ask the questions on comment if you get errors.

c++ programs,c++ tutorial, c++ tutorials for beginners, c++ ioe notes
c++ programs,c++ tutorial, c++ tutorials for beginners,

Now, I will explain what the program was actually was? And, what did we actually did in this program?

We are required to calculate area of triangle

  1. with sides
  2. with base and height

Plus, we are also required to use the concept of function overloading.

Then what is Function Overloading?

Function overloading is one of the important feature of C++, which shows compile-time polymorphism. (Polymorphism=many forms! I will discuss all C++ theory later! As soon as possible)

Same function names having several definitions that differ either in number of arguments or type of arguments; or both is called function overloading in C++.

So, there are 3 different ways to write a program using the function overloading and they are as follows-:

  1. According to the number of arguments passed
  2. According to the type of arguments passed
  3. According to the type and number of arguments passed
  1. According to the number of arguments passed

2) According to the type of arguments passed

3) According to the type and number of arguments passed

So, I hope till now, you have learnt how to write a program using the concept of function overloading.

Now, let’s go to the initial question.

Firstly, we  created a function with void return type(means no value will be returned back); where we passed a,b,c. As, we are also asked to do default arguments, we initialized the value of c=7.5. Alright!

Then, let’s go inside the function; we want to find area.

We know, area =sqrt. of s*(s-a)*(s-b)*(s-c)

SO, we need s; what is s? s=semi-perimeter=(a+b+c)/2;

so, we found s, then calculated area and finally displayed area by using cout with << operator.

Remember-: in c++ cout=printf in c.

Then, we again made another function named area. It is because we are asked to do function overloading by the question. And we are doing a function overloading by using same name and different parameters; i.e in above, we used (float, float) and in below function; we are using (int, int). So, this is a function overloading of which type?- Comment fast!!!

Inside that area function with (int,int); we calculated area as area=1/2*b*h; if you don’t know, this is the formula for calculating area.

Did you noticed that we didn’t used area inside the function? It is because we are using the function name “area”, so we can’t use the variable name, same as the function name.

I think so! But, do you agree with me? Change ar to area and comment me what happens? Does the program runs on doing that also?

Now, let’s go inside of the main function. Here we are going to call all the functions to calculate areas.

So, firstly, we called the area function to calculate area from sides.

We called it like this-:

area(side1,side2,side3).

After that, we called the area function which will calculate area when base and height are given.

Note the number of parameters being different in two different functions with same name. Thus, we can say that we succeeded in using function overloading in this program.

Here’s a video lecture in this topic.

i have used the bucky’s video, if i have violated any copyrights by doing this, please message me, and i will remove this.

Be the first to comment

Leave a Reply

Your email address will not be published.


*