0% found this document useful (0 votes)
9 views

Lab 8

Questions on Oops

Uploaded by

Suhani Garg
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lab 8

Questions on Oops

Uploaded by

Suhani Garg
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

LAB-8

Q1. Visibility of Inherited Members in C++


a) Visibility Table

Consider the following access specifiers in C++: public, protected, and private. Fill in the
visibility table below to indicate whether the members of the base class are accessible in
the derived class based on the type of inheritance.

Derived class visibility


Base class visibility
Public derivation Private derivation Protected derivation

Private

Protected

Public

b) Write a C++ program that demonstrates the visibility of inherited members.

Requirements:

• In each derived class, attempt to access and call each of the base class's members
(public, protected, and private).

• Create instances of your derived classes in the main function to test member
visibility from outside the derived classes.

• Provide comments in your code explaining the expected visibility results for each
member type and any errors encountered.

Q2. Understanding of Virtual class


Implement class hierarchies to explore inheritance and virtual inheritance in C++.

1. Classes A (Without Virtual Inheritance), B, C (Without Virtual Inheritance), and


D (Without Virtual Inheritance)

o Create a base class A with an integer member data and a method display().

o Implement classes B and C inheriting from A.


o Implement class D inheriting from both B and C. Handle any ambiguity in
accessing data.

2. Classes A (Without Virtual Inheritance), B, C (With Virtual Inheritance), and D


(Without Virtual Inheritance)

o Same as above, but make class C inherit from A with virtual inheritance.

o Implement class D and manage access to data.

3. Classes A (Without Virtual Inheritance), B (With Virtual Inheritance), C, and D


(Without Virtual Inheritance)

o Modify class B to use virtual inheritance.

o Implement class D inheriting from B and C, handling ambiguities.

4. Classes A (Without Virtual Inheritance), B (With Virtual Inheritance), C (With


Virtual Inheritance), and D (Without Virtual Inheritance)

o Make both classes B and C use virtual inheritance.

o Implement class D and ensure proper access to data.

Q3. Pointer to derived class


Consider the following scenario:

You have a base class Shape that has a member function to calculate the area. You also have
two derived classes: Circle and Rectangle. Each derived class implements its own method to
calculate the area specific to its shape.

a) Write a program to demonstrate how you can access the member functions of the
Shape class using a base class pointer.

b) Show that you cannot access the specific member functions of the Circle and
Rectangle classes using the base class pointer.

c) Demonstrate how to access these specific member functions using derived class
pointers.

Q4. Function Overloading vs. Function Overriding


a) Function Overloading: Implement a class Calculator with overloaded add()
functions. Additionally, introduce an ambiguous overload scenario where two
overloaded functions could potentially match the same call. You will also demonstrate
how type promotion affects which overloaded function is called.
b) Function Overriding: Implement a base class Animal and derived classes Dog
and Cat. Each derived class will override a method speak(), but you'll also implement
an additional method in the derived classes that is not present in the base class. You
will then demonstrate how to access the derived class's specific methods using base
class pointers.

Include scenarios where:


• A base class pointer is used to call the overridden function and the non-overridden
function.
• You test which function gets called in an ambiguous overload situation .
Syntax for Function Overriding

class Base {
public:
// Virtual function in the base class
virtual ReturnType FunctionName(ParameterType1 param1, ParameterType2
param2) {
// Base class implementation
}
};

class Derived : public Base {


public:
// Overriding the base class function
void FunctionName(ParameterType1 param1, ParameterType2 param2) override
{
// Derived class implementation
}
};

You might also like