C++ | Inheritance | Question 11
#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; } |
chevron_right
filter_none
(A) 10 10
(B) Compiler Error
(C) 0 0
Answer: (B)
Explanation: The base class members cannot be directly assigned using initializer list. We should call the base class constructor in order to initialize base class members.
Following is 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; } |
chevron_right
filter_none
Recommended Posts:
- C++ | Inheritance | Question 12
- C++ | Inheritance | Question 3
- C++ | Inheritance | Question 4
- C++ | Inheritance | Question 5
- C++ | Inheritance | Question 14
- C++ | Inheritance | Question 1
- C++ | Inheritance | Question 9
- C++ | Inheritance | Question 2
- C++ | Inheritance | Question 8
- C++ | Inheritance | Question 13
- C++ | Inheritance | Question 10
- C++ | Inheritance | Question 6
- C++ | Inheritance | Question 7
- Java | Inheritance | Question 9
- Java | Inheritance | Question 7



