English
Tamil Nadu Board of Secondary EducationHSC Science Class 11

Discuss the benefits of constructor overloading? - Computer Science

Advertisements
Advertisements

Question

Discuss the benefits of constructor overloading?

Short Note

Solution

Function overloading can be applied for constructors, as constructors are special functions of classes. A class can have more than one constructor with a different signature. Constructor overloading provides the flexibility in creating multiple types of objects for a class.

  1. Memory is allocated for the objects.
  2. Initialization for the objects.
shaalaa.com
Overloaded Constructors
  Is there an error in this question or solution?
Chapter 15: Polymorphism - Evaluation - Section - C [Page 257]

APPEARS IN

Samacheer Kalvi Computer Science [English] Class 11 TN Board
Chapter 15 Polymorphism
Evaluation - Section - C | Q 4. | Page 257

RELATED QUESTIONS

Which function(s) out of the following can be considered as an overloaded function(s) in the same program? Also, write the reason for not considering the other(s) as an overloaded function(s)

void Execute(char A,int B);    // Function 1

void Execute(int A,char B);    // Function 2

void Execute(int P=10) ;       // Function 3

void Execute();                    // Function 4

int Execute(int A);               // Function 5

void Execute(int &K);          // Function 6


class add{int x; public: add(int)}; Write an outline definition for the constructor.


class sale ( int cost, discount; public: sale(sale &); Write a non-inline definition for constructor specified;


Answer the question after going through the following class.

class Book {
int BookCode ; char Bookname[20];float fees;
public:
Book( )   //Function 1
{ fees=1000;
BookCode=1;
strcpy(Bookname,"C++"); }
void display(float C) //Function 2
{ cout<<BookCode<<":"<<Bookname<<":"<<fees<<endl; }
~Book( )         //Function 3
{ cout<<"End of Book Object"<<endl; }
Book (intSC,char S[ ],float F) ; //Function 4
};

In the above program, what are Function 1 and Function 4 combined together referred to as?


Answer the question based on the following program.

#include<iostream>
#include<string.h>
using namespace std;
class comp {
public:
char s[10];
void getstring(char str[10])
{ strcpy(s,str); }
void operator==(comp);
};
void comp::operator==(comp ob)
{ if(strcmp(s,ob.s)==0)
cout<<"\nStrings are Equal";
else
cout<<"\nStrings are not Equal"; }
int main()
{ comp ob, ob1;
char string1[10], string2[10];
cout<<"Enter First String:";
cin>>string1;
ob.getstring(string1);
cout<<"\nEnter Second String:";
cin>>string2;
ob1.getstring(string2);
ob==ob1;
return 0; }

Name the object which gets destroyed in between the program.


Answer the question based on the following program.

#include<iostream>
#include<string.h>
using namespace std;
class comp {
public:
char s[10];
void getstring(char str[10])
{ strcpy(s,str); }
void operator==(comp);
};
void comp::operator==(comp ob)
{ if(strcmp(s,ob.s)==0)
cout<<"\nStrings are Equal";
else
cout<<"\nStrings are not Equal"; }
int main()
{ comp ob, ob1;
char string1[10], string2[10];
cout<<"Enter First String:";
cin>>string1;
ob.getstring(string1);
cout<<"\nEnter Second String:";
cin>>string2;
ob1.getstring(string2);
ob==ob1;
return 0; }

Which constructor will get executed in the above program? Write the output of the program?


Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×