1. Write a program in C++ to create abstract class Figure having abstract method area(), and data members length, breadth. Derive classes Triangle and Rectangle from Figure having member function area(). Find area of triangle and rectangle by function overriding concept? [Note: sub classes does not contain any data members].
2. Write a program in C++ to justify the control flow and use of templates by mentioning overloaded function as well.
SOLUTION CODE
//Function overloading by use of templates
#include <iostream>
using namespace std;
//function called square_of_a_number taking int as a parameter
void square_of_a_number(int my_number)
{
cout << "Square of " << my_number<< " is " << my_number * my_number<< endl;
}
//function called square_of_a_number taking double as a parameter
void square_of_a_number(double my_number)
{
cout << "Square of " << my_number<< " is " << my_number * my_number<< endl;
}
// Driver Code
int main()
{
// Function Call for integer value
square_of_a_number(12);
// Function Call for double value
square_of_a_number(5.5);
return 0;
}
Comments
Leave a comment