in Object Oriented Programming retagged by
846 views
0 votes
0 votes

Give the output 

#include<iostream>
using namespace std;
class Base
{
public:
    int x,y;
public:
    Base(int i, int j){x=i;y=j;}
};
class Derived:public Base
{
public:
    Derived(int i,int j):x(i),y(j){}
    void print(){cout<< x << ""<<y ;}
};
int main(void)
{
    Derived q(10,10);
    q.print();
    return 0;
}
  1. $10\:10$
  2. Compiler Error
  3. $0\:0$
  4. None of the option
in Object Oriented Programming retagged by
846 views

1 comment

reshown by
For assigning values to base class members or intializing the base class members in derived class we have to call base class constructor in derived class. Here intializer list concept is used but this will not work for base class. So correct code is

Derived(int i, int j ):Base(i,j) {}

 

So ans will be option b
0
0

2 Answers

1 vote
1 vote

Answer: (B)

Derived(int i,int j):x(i),y(j){}

The above line uses initializer list concept. The base class members cannot be directly assigned using initializer list. So, it gives us a compiler error.

We should call the base class constructor in order to initialize base class members.

Following is an error free program and prints “10 10”

#include<iostream> 
using namespace std; 
  
class Base 
{ 
    public : 
       int x, y; 
    public: 
      Base(int i, int j){ x = i; y = j; } 
}; 
  
class Derived : public Base 
{ 
    public: 
       Derived(int i, int j): Base(i, j) {} 
       void print() {cout << x <<" "<< y; } 
}; 
  
int main(void) 
{ 
    Derived q(10, 10); 
    q.print(); 
    return 0; 
} 

 

0 votes
0 votes
For assigning values to base class members or intializing the base class members in derived class we have to call base class constructor in derived class. Here intializer list concept is used but this will not work for base class. So correct code is

Derived(int i, int j ):Base(i,j) {}

 

So ans will be option b
by
Answer:

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true