C/C++, Featured
C++ Inheritance: Questions and Solutions
These seven C++ inheritance problems cover scope resolution, access control, direct and indirect base classes, and multi-level class networks. Each includes a working solution with output.
Need help with a C++ assignment? C++ Programming Assignment Help covers the full range of OOP topics.
Scope Resolution in Single Inheritance
Q1. Write a program to show how ambiguity is avoided in single inheritance using the scope resolution operator.
#include <iostream>
using namespace std;
class A {
public:
void display() {
cout << "A\n";
}
};
class B : public A {
public:
void display() {
cout << "B\n";
}
};
int main() {
B b;
b.display(); // calls display() in B
b.A::display(); // calls display() in A
b.B::display(); // calls display() in B
return 0;
}
Output:
B
A
B
The scope resolution operator (::) lets you call a specific class version of display() when both the base and derived class define it.
Private Members and Public Inheritance
Q2. Write a program to show that private data members of a base class are not accessible in the derived class, even with public inheritance.
#include <iostream>
using namespace std;
class B {
int a; // private; not inherited
public:
int b;
void get_ab() { a = 5; b = 10; }
int get_a() { return a; }
void show_a() { cout << "a=" << a << "\n"; }
};
class D : public B {
int c;
public:
void mul() { c = b * get_a(); }
void display() {
cout << "a=" << get_a() << "\n";
cout << "b=" << b << "\n";
cout << "c=" << c << "\n";
}
};
int main() {
D d;
d.get_ab();
d.mul();
d.show_a();
d.display();
d.b = 20;
d.mul();
d.display();
return 0;
}
a is private in B. Class D accesses it only through the public accessor get_a(). Attempting c = b * a inside D::mul() would not compile.
Direct and Indirect Base Classes
Q3. What is inheritance in OOP? Show how a direct and indirect base class is declared in C++.
Inheritance lets you derive a new class from an existing one. The original class is the base class; the derived class extends it. When a chain of three or more classes forms, the topmost class is the indirect base of the lowest.
class M { // direct base class
protected:
int m;
public:
void get_m(int);
};
class N { // indirect base class
protected:
int n;
public:
void get_n(int);
};
class B : public M, public N {
public:
void display();
};
void M::get_m(int x) { m = x; }
void N::get_n(int y) { n = y; }
void B::display() {
cout << "m=" << m << "\n";
cout << "n=" << n << "\n";
cout << "m*n=" << m * n << "\n";
}
int main() {
B p;
p.get_m(10);
p.get_n(20);
p.display();
return 0;
}
Output:
m=10
n=20
m*n=200
Class Hierarchy Terminology
Q4. Explain the relationship among: super class, sub class, base class, and derived class.
The terms map to the same two roles:
- Base class = super class (the parent; also called the grand parent class when a chain extends further)
- Derived class = sub class (the child)
class A { }; // super class (grand parent)
class B : public A { }; // base class (parent)
class C : public B { }; // derived class / sub class (child)
C inherits from B directly; it inherits from A indirectly.
Reading Employee Data with Inheritance
Q5. Design an OOP program that reads employee information using a base class and inherits it into a derived class.
#include <iostream>
#include <string>
using namespace std;
class Employee {
private:
string name;
string designation;
int age, code, yrs;
public:
void getdata() {
cout << "Enter name: "; cin >> name;
cout << "Enter designation: "; cin >> designation;
cout << "Enter age: "; cin >> age;
cout << "Enter code: "; cin >> code;
cout << "Enter years of experience: "; cin >> yrs;
}
void display() {
cout << "Name: " << name << "\n";
cout << "Designation: " << designation << "\n";
cout << "Age: " << age << "\n";
cout << "Code: " << code << "\n";
cout << "Years of experience: " << yrs << "\n";
}
};
class Manager : public Employee { };
int main() {
Manager m;
m.getdata();
m.display();
return 0;
}
Sample output:
Name: Rajiv
Designation: Manager
Age: 22
Code: 123
Years of experience: 12
Manager inherits getdata() and display() directly from Employee. No method duplication needed.
Multi-Level Class Network Design
Q6. Design the following class network in C++:
PERSON(name, code)is a base for bothACCOUNT(pay)andADMIN(experience).ACCOUNTandADMINtogether feed into a final classMANAGER(name, code, experience, pay)using multiple inheritance.
#include <iostream>
#include <string>
using namespace std;
class Person {
protected:
string name;
int code;
public:
void get_person() {
cout << "Enter name: "; cin >> name;
cout << "Enter code: "; cin >> code;
}
};
class Account : virtual public Person {
protected:
double pay;
public:
void get_account() {
cout << "Enter pay: "; cin >> pay;
}
};
class Admin : virtual public Person {
protected:
int experience;
public:
void get_admin() {
cout << "Enter experience (years): "; cin >> experience;
}
};
class Manager : public Account, public Admin {
public:
void get_all() {
get_person();
get_account();
get_admin();
}
void display() {
cout << "Name: " << name << "\n";
cout << "Code: " << code << "\n";
cout << "Pay: " << pay << "\n";
cout << "Experience: " << experience << "\n";
}
};
int main() {
Manager mgr;
mgr.get_all();
mgr.display();
return 0;
}
virtual inheritance on Person prevents the "diamond problem": Manager gets exactly one copy of name and code even though it inherits from both Account and Admin.
Debugging Access Violations in Inheritance
Q7. Find and explain the errors in the following class hierarchy.
#include <iostream>
using namespace std;
class X {
private:
int x1; // private; not accessible in derived classes
protected:
int x2; // accessible in derived classes
public:
int x3; // accessible everywhere
};
class Y : public X {
public:
void f() {
int y1, y2, y3;
// y1 = x1; // ERROR: x1 is private in X
y2 = x2; // OK: x2 is protected
y3 = x3; // OK: x3 is public
}
};
class Z : private X { // private inheritance
public:
void f() {
int z1, z2, z3;
// z1 = x1; // ERROR: x1 is private in X
z2 = x2; // OK inside Z::f
z3 = x3; // OK inside Z::f
}
};
int main() {
Y y;
// m = y.x1; // ERROR: x1 is private
// n = y.x2; // ERROR: x2 is protected (not accessible outside class)
int p = y.x3; // OK: x3 is public
Z z;
// m = z.x1; // ERROR: x1 is private in X
// n = z.x2; // ERROR: private inheritance hides x2 outside Z
// p = z.x3; // ERROR: private inheritance hides x3 outside Z
(void)p;
return 0;
}
Three error sources:
x1is private inX. No derived class or external caller can read it.x2is protected. Only code insideXor a derived class can use it. Themain()linesy.x2andz.x2fail.- Private inheritance (
class Z : private X) makes all inherited members private insideZ. OutsideZ, neitherx2norx3is reachable.
For more on managing class hierarchies and OOP design in assignments, see OOP in C++ Explained: Classes to Polymorphism and C++ Best Practices for Clean Code.
C++ Programming Assignment Help is available if you need a developer to write, debug, or explain an inheritance-heavy assignment.
Related articles
- C/C++
Min Heap and Max Heap in C++
Build min heaps and max heaps in C++ with std::priority_queue and the STL heap algorithms, plus array math, heapify, heap sort, and worked examples.
Sep 19, 2023
- C/C++
Vectors in C++: A Complete Guide
Master std::vector in C++ with declaration, push_back and emplace_back, iterators, size vs capacity, 2D vectors, custom types, and complexity, with code that compiles.
Aug 7, 2023
- C/C++
C++ Programming: A Complete Introduction
Learn C++ from the ground up: how it compiles, its core data types, functions, and the four pillars of object-oriented programming, with runnable code.
Mar 30, 2023


