C++ 22: Member Function Overloading
The process of defining multiple member functions in a class with SAME NAME by DIFFERENT NUMBER OF ARGUMENTS or DIFFERENT DATATYPE OF ARGUMENTS is called as member function overloading.
An appropriate function will be called as per the values passed while calling the function.
Example:
Design a class rectangle with data members L,B. Define member function setdata(), area(), and perimeter(). Overload the unction setradius so that it can be call by passing 2 or 1 arguments. The function area() and perimeter() must return result.
#include <iostream>
using namespace std;class Rectangle{private: int L, B;
public: void setdata(int l) { L = l; B = l; } void setdata(int l, int b) { L = l; B = b; } int area() { return (L * B); } int perimeter() { return (2 * (L + B)); }};
main(){ int aa, ap; int ba, bp;
Rectangle a; a.setdata(5); aa = a.area(); ap = a.perimeter(); cout << "Area of Rectangle is " << aa << endl; cout << "Perimeter of Rectangle is " << ap << endl;
Rectangle b; b.setdata(20, 10); ba = b.area(); bp = b.perimeter(); cout << "Area of Rectangle is " << ba << endl; cout << "Perimeter of Rectangle is " << bp << endl;}Output:
Perimeter of Rectangle is 20
Area of Rectangle is 200
Perimeter of Rectangle is 602Design a class Circle with data member Define memeber functions setradius(),area()
and circumference. Overload the function setradius() so that it can be call by passing
double or int value.
#include <iostream>
using namespace std;class Circle{
double R;
public: void setradius(int r) { R = r; } void setradius(double r) { R = r; }
void area() { double a = 3.14 * R * R; cout << "Area of circle is: " << a << endl; }
void circumference() { double c = 2 * 3.14 * R; cout << "Circumference is: " << c << endl; }};
int main(){
Circle a; a.setradius(5); a.area(); a.circumference();
Circle b; b.setradius(7.5); b.area(); b.area(); return 0;}Output:
Circumference is: 31.4
Area of circle is: 176.625
Area of circle is: 176.625Member function with default arguments or optional arguments:
- If required we can specify default value to the arguments of a function.
- Specifying the default value makes the argument optional.
- If a function contain default arguments then such a function can be called by passing different number of arguments.
- The default values must be specified in right to left direction.
Example: o
Design a class Distance with data members Feet and inches. Define member function setdata()
showdata(), nd convert(). The function setdata() must be called by passing 2 or 1 arguments. The convert
function will return data of Distance object into inches. (1 feet = 12 inches).
#include <iostream>
using namespace std;class Distance{
private: int F; int I;
public: void setdata(int f, int i = 0) { F = f; I = i; } void showdata() { cout << "Data of Object is " << F << " feet and " << I << " inches." << endl; }
double convert() { return (F / 12.0 + I); }};main(){ double ac, bc; Distance a; a.setdata(67); a.showdata();
ac = a.convert();
cout << "The converted value is :" << ac << " inches." << endl;
Distance b; b.setdata(24, 9); b.showdata();
bc = b.convert();
cout << "The converted value is :" << bc << " inches." << endl;}Output:
The converted value is :5.58333 inches.
Data of Object is 24 feet and 9 inches.
The converted value is :11 inches
Comments
Post a Comment