You must Sign In to post a response.
  • Category: Programming

    C++ program for implementation of overloaded constructor.


    Want a solution for a C program asked in MSBTE exam? Searching for the answer here? On this Ask Expert page you can scroll through the solution and prepare well for the final exam.

    Following is the question asked in MSBTE(Maharashtra State Board of Technical Education, Mumbai) Diploma in Computer Engineering question paper for Winter 2017 examination.
    This question is asked for 4 marks and hence requires some explanation along with program code. Please let me know the solution.
    Q) Write a program which implement the concept of overloaded constructor. [4 Marks]
  • Answers

    2 Answers found.
  • Constructor overloading in C++:

    A class can have more than one constructor with the same name. As more than one constructor is defined in a class it is called constructor overloading in C++. The overloaded constructor will have the same name and a different number of arguments. The constructor will be called based on the number of argument and number passed. While creating the object, we must pass the arguments to let know the compiler, which constructor should be called.

    Here is an example for constructor overloading in C++:
    //
    #include
    using namespace std;

    class construct
    {

    public:
    float area;

    // Constructor with no parameters
    construct()
    {
    area = 0;
    }
    // Constructor with two parameters
    construct(int a, int b)
    {
    area = a * b;
    }

    void disp()
    {
    cout<< area<< endl;
    }
    };
    int main()
    {
    // Constructor Overloading
    // with two different constructors of class name
    construct o;
    construct o2( 10, 30);

    o.disp();
    o2.disp();
    return 1;
    } //

    Output:
    0
    300

    Regards,
    Ramya

  • Constructor is a special member function of the class. The task of constructor function is to initialize the members of an object of the class, whenever object of the class is declared. So this is one kind of dynamic initialization of the object. The name of the constructor function is same as that of the class name.
    Overloaded constructor means if a class has more that two constructors in the same class then according to arguments, their sequence the appropriate constructor function get executed.
    Following is the program which has overloaded constructor.
    Please include iostream.h and conio.h header files before class declaration.

    //PROGRAM TO IMPLEMENT OVERLOADED CONSTRUCTOR
    #include
    #include
    #include
    class studentinfo
    {
    private:
    char name[20];
    int t1_m, t2_m,total;
    public:
    studentinfo(){}
    studentinfo(char *s, int m)
    {
    strcpy(name,s);
    t1_m=t2_m=m;
    cout<<"\nStudent name is "< cout<<"\nMarks in first test are "< cout<<"\nMarks in second test are "< total=t1_m+t2_m;
    cout<<"\nTotal marks of both tests are "< }
    studentinfo(char *s, int m1, int m2)
    {
    strcpy(name,s);
    t1_m=m1;
    t2_m=m2;
    cout<<"\nStudent name is "< cout<<"\nMarks in first test are "< cout<<"\nMarks in second test are "< total=t1_m+t2_m;
    cout<<"\nTotal marks of both tests are "< }

    };
    void main(void)
    {
    char * str;
    int t1,t2;
    clrscr();
    class studentinfo obj1;
    cout<<"\nEnter student name\n";
    cin>>str;
    cout<<"\nEnter marks in tests(out of 25)\n";
    cin>>t1;
    obj1=studentinfo(str,t1);
    cout<<"\n\n**********************************************\n";
    cout<<"\nEnter student name\n";
    cin>>str;
    cout<<"\nEnter marks obtained in first test(out of 25)\n";
    cin>>t1;
    cout<<"\nEnter marks obtained in second test(out of 25)\n";
    cin>>t2;
    class studentinfo obj2(str,t1,t2);
    getch();
    }

    In above program three constructors are used. First form of constructor is default constructor that is constructor without any argument. Second for is parameterised constructor which requires one character pointer(which holds the student name) and one integer(which are the marks in two tests). Third form of constructor is also a parameterised constructor with three arguments out of one is character pointer and two integers as test marks. When only object of class is created the first form of constructor get executed. This object is used to call second form of constructor forcefully using only two parameters. When another object is created and supplied with three parameters, it invokes third form of constructor.
    So in this program though class contains three constructors, still it executes only appropriate constructor accordingly. This is the beauty of OOP.


  • Sign In to post your comments