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

Lab 8

OOP Practice Docx

Uploaded by

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

Lab 8

OOP Practice Docx

Uploaded by

muhammadtahasahi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

School Of Electrical Engineering

Course Name
Object Oriented Programming

Subject Instructor Dr. SADIQ AMIN


Lab Instructor SYED ZAIN UL HASSAN

Group Members
Muhammad Taha (455060)
Muhammad Hassan (466063)

EXPERIMENT NO 8
Multiple Inheritance
INTRODUCTION
The following lab aims at building a good rear concept on inheritance, especially the
diamond problem that is encountered. This problem will truly enhance the concept
of inheritance.
Multiple inheritance is the main key concept allocated within the package of
inheritance. While there is also multilevel and hierarchical which do not need to be
introduced.

Objectives
To learn what is Multiple Inheritance
Understand and solve the diamond problem due to Multiple
Inheritance

Equipment required:
Visual Studio/ Dev C++

Description

Inheritance is the process of inheriting properties of objects of one class by objects of


another class. The class which inherits the properties of another class is called
Derived or Child or Sub class and the class whose properties are inherited is called
Base or Parent or Super class. When a class is derived from two or more base classes,
such inheritance is called Multiple Inheritance. It allow us to combine the features of
several existing classes into a single class.

For example,
• Petrol is derived from both liquid and fuel.

• A child has character of both his/her father and mother, etc

Syntax of Multiple Inheritance

class base_class1

properties;

methods;

};

class base_class2

properties;

methods;

};

... ... ...

... ... ...


class base_classN

properties; methods;

};

class derived_classname : visibility_mode base_class1, visibility_mode base_class2,...


,visibility_mode base_classN

properties;

methods;

};
The Output should be similar as shown:

TASK A
CODE
#include <iostream>
using namespace std;

// Base class A
class A {

public:
int data = 100;

void displayA() {
cout << "Normal Access Base1 class Data = " << data << endl;
}
};

// Base class B
class B {

public:
int data = 200;

void displayB() {
cout << "Normal Access Base2 class Data = " << data << endl;
}
};

// Derived class C inheriting from both A and B


class C : public A, public B {

public:

int data = 600;


void displayC() {
cout << "Normal Access Derived1 class Data = " << data << endl;
}
};

int main() {
A obj1;
B obj2;
C obj3;
obj1.displayA(); // Accessing function from class A
obj2.displayB(); // Accessing function from class B
obj3.displayC(); // Accessing function from class C

cout << endl <<"Extracting base1 data via derived class: " << endl;
obj3.displayA();

cout << endl <<"Extracting base2 data via derived class: " << endl;
obj3.displayB();

return 0;
}
OUTPUT SNIPET

TASK B
CODE
#include <iostream>
using namespace std;

//
OUTPUT SNIPPET
class teraBaap {
public:
int sata = 10;
void DisplayDaddy()
{
cout<<"Tera Baap hun mein "<<sata<<endl;
}

};
// Base class A
class A : virtual public teraBaap{
public:
int data = 100;
void displayA() {
cout << "Normal Access Base1 class Data = " << data << endl;
}
};

// Base class B
class B : virtual public teraBaap{
public:
int data = 200;
void displayB() {
cout << "Normal Access Base2 class Data = " << data << endl;
}
};

// Derived class C inheriting from both A and B


class C : public A, public B {
CODE
#include <iostream>
#include <string>
using namespace std;
class Individual {
public:
string fullName = "Abdullah";
int personAge = 20;
};

class Learner : virtual public Individual {


public:
int studentID = 4454542;
};

class Instructor : virtual public Individual {


public:
int facultyID = 51;
};

class TeachingAssistant : public Learner, public Instructor {


public:
int assistantID = 12340;
};

int main() {
TeachingAssistant assistantAli;
cout << "Age is: " << assistantAli.personAge << endl;
cout << "Student ID is: " << assistantAli.studentID << endl;
cout << "Faculty ID is: " << assistantAli.facultyID << endl;
cout << "Assistant ID is: " << assistantAli.assistantID << endl;

return 0;
}
OUTPUT SNIPPET

CONCLUSION
In this lab, we examined the concept of multiple inheritance in C++, which
allows a class to derive properties and behaviors from more than one base
class. This technique offers a versatile approach to integrating functionalities
from diverse sources, promoting flexibility and modular design in software
development.

A significant aspect of our exploration was the diamond problem, a common


issue in multiple inheritance where ambiguity arises when a class inherits from
the same base class through multiple paths. To resolve this, we utilized virtual
inheritance, a feature in C++ that ensures the base class is represented only
once in the inheritance hierarchy, eliminating redundancy and preventing
confusion.

Overall, the lab was designed to enhance our understanding of multiple


inheritance while refining our coding skills and problem-solving techniques.

You might also like