0% found this document useful (0 votes)
7 views9 pages

Assignment Oop

The document discusses concepts of multiple and multilevel inheritance in C++, providing definitions and examples for each. It includes a C++ program that calculates the factorial using multilevel inheritance and another program for a publishing company that markets books and audiocassettes. Additionally, it explains the order of constructor execution in multiple inheritance scenarios.

Uploaded by

zunayrajunaid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views9 pages

Assignment Oop

The document discusses concepts of multiple and multilevel inheritance in C++, providing definitions and examples for each. It includes a C++ program that calculates the factorial using multilevel inheritance and another program for a publishing company that markets books and audiocassettes. Additionally, it explains the order of constructor execution in multiple inheritance scenarios.

Uploaded by

zunayrajunaid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

OOP ASSIGNMENT

ABDUL MOIZ IMRAN


FALL 23-BSCS-030
SECTION A
Q1: Differentiate between multiple and
multilevel inheritance:

1. Multiple Inheritance:

It is the type of inheritance in which multiple


classes are involved, serving as base class
and derived class is derived from both of the
base classes.

E.g: Class C is derived from two base


classes, A and B.

2. Multi-level Inheritance:

It is the type of inheritance in which one


class is derived from the previous derived
class.
E.g: Class C is derived from B and B from A.
Q2: Order of execution of constructors in
inheritance where multiple classes are being
used:

In multiple inheritance, the constructor of base


class is executed first followed by derived class.

Q3: C++ program to find factorial using


multilevel inheritance:
#include<iostream>
using namespace std;

class Base {
public:
int num;
void getData() {
cout << "Enter a number: ";
cin >> num;
}
};
class Factorial : public Base {
public:
int f;
void calculateFactorial() {
f = 1;
for (int i = 1; i <= num; i++) {
f*= i;
}
}
};

class Display : public Factorial {


public:
void displayResult() {
cout << "Factorial of " << num << " is " <<
fact << endl;
}
};
int main() {
Display obj;
obj.getData();
obj.calculateFactorial();
obj.displayResult();
return 0;
}

Q4:Publishing company that markets both book


and audiocassette versions of its works:

#include <iostream>
#include <string>
using namespace std;

class Publication {
protected:
string title;
float price;
public:
Publication() : title(""), price(0.0) {}

void getdata() {
cout << "Enter title: ";
cin >> title;
cout << "Enter price: ";
cin >> price;
}

void putdata() const {


cout << "Title: " << title << endl;
cout << "Price: " << price << endl;
}
};

class Book : public Publication {


int page_count;
public:
Book() : page_count(0) {}

void getdata() {
Publication::getdata();
cout << "Enter page count: ";
cin >> page_count;
}

void putdata() const {


Publication::putdata();
cout << "Page count: " << page_count <<
endl;
}
};

class Tape : public Publication {


float playing_time;
public:
Tape() : playing_time(0.0) {}

void getdata() {
Publication::getdata();
cout << "Enter playing time (in minutes): ";
cin >> playing_time;
}

void putdata() const {


Publication::putdata();
cout << "Playing time: " << playing_time << "
minutes" << endl;
}
};

int main() {
Book b;
Tape t;
cout << "Enter book details:" << endl;
b.getdata();
cout << "\nEnter tape details:" << endl;
t.getdata();

cout << "\nBook Details:" << endl;


b.putdata();

cout << "\nTape Details:" << endl;


t.putdata();

return 0;
}

You might also like