Overloading Member Functions Assignment Help

Assignment Help: >> Polymorphism - Overloading Member Functions

Overloading Member Functions

Aside from performing the special role of initialization, constructor functions are no different from other types of functions. This involves overloading. Actually, it is extremely common to search overloaded constructor functions. For instance, let consider the following program that creates a class known as date which contains a calendar date. Remember that the constructor is overloaded two ways.

#include<iostream.h>

#include<stdio.h>

class date

{

int day.month,year;

public:

date(char *d);

date(int m,int d,int y);

void show_date();

};

date::date(char *d)

{

scanf(d,"%d%*c%d%*c%d",&month,&day,&year); // For print the output

}

date::date(int m,int d,int y)

{

day=d; month=m; year=y;

}

void date::show_date()

{

cout << month <<"/" <<day;

cout <<"/" << year <<"\n";

}

main()

{

date ob1(12,4,96),ob2("10/22/97");

ob1.show_date();

ob2,show_date();

return 0;

}

in this program, you could initialize an object of type date, either through specifying the date by using three digits to represent the month, day and year or by using a string which holds the date in this coomon form:

mm/dd/yy

the most general reason to overload a constructor is to permit an object to be created through  using  the  most  suitable  and  natural  means  for  every  circumstance.  For instance, in the given main (), the user is prompted for the date, that is input to arrays. This string could then be used directly to create. There is no requirement for it to be converted to any other form. Moreover, if date () were not overloaded to accept the string form you would manually convert it into three integers every time you created an object.

main()

{

char s[80];

cout <<"Enter new date :";

cin >> s;

date d(s);

d.show_date();

return 0;

}

in another condition, initializing an object of type date by using three integers might be more convenient. For instance, if the date is produced by a few sort of computational technique, then creating a date object using date(int,int,int) is the most natural and suitable constructor to employ. The point here is which by overloading dates and ease of use are especially important if you are creating class libraries which will be used through other programmers.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd