You must Sign In to post a response.
  • Explain constructor in derived class with suitable example.


    Preparing for diploma exams for the computer technology stream? Searching for detailed solution pertaining to constructor in derived class? Check out this page where the experts have provided detailed solution for your queries.

    Following is a question that was asked in the question paper of Diploma in Computer Technology(Summer 2019)conducted by Maharashtra State Board of Technical Examination, Mumbai for subject Object Oriented Programming(17432)(G Scheme), Semester-IV

    This question is asked for 8 marks and hence along with program some explanation is also expected.

    Q) Explain constructor in derived class with suitable example [8 Marks]
  • Answers

    1 Answers found.
  • Constructor in derived class is a concept which is used in inheritance where derived class must have constructor when base class is having constructor. Normally while using inheritance, object of derived class is created and not the object of base class. So when situation is like that base class has constructor and derived class is based on base class, then to supply arguments to base class constructor as well to supply arguments to derived class itself the concept which is known as "Constructor in derived class" is implemented. Here the responsibility of derived class constructor is to supply arguments to base class constructor and to supply arguments to its own constructor. For achieving this task there is a special mechanism. The syntax of constructor in derived class is as follows
    derived class constructor(arg1, arg2, ARG3, ..., argn, arg D): base1(arg1,..), base2(arg3,..)
    {
    arg d:
    }
    Below is the program which demonstrate the use of constructor in derived class. Please include and header files before declaration of class.
    //PROGRAM TO IMPLEMENT CONSTRUCTOR IN DERIVED CLASS.
    #include
    #include
    class base
    {
    protected:
    int t1,t2;
    public:
    base(int x, int y)
    {
    t1=x;
    t2=y;
    cout<<"\nmarks in first test = "< cout<<"\nmarks in second test = "< }

    };
    class derived:public base
    {
    protected:
    float avg;
    int total;
    public:
    derived(int a, int b):base(a,b)
    {
    total=t1+t2;
    avg=float(total)/2;
    cout<<"Total marks in two tests = "< cout<<"\nAverage marks in two tests = "< }
    };
    void main(void)
    {

    int m1,m2;
    clrscr();
    cout<<"\nEnter marks in two tests\n";
    cin>>m1>>m2;
    class derived d(m1,m2);
    getch();
    }


  • Sign In to post your comments