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

OOP Inheritance and Factorial in C++

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)
3 views9 pages

OOP Inheritance and Factorial in C++

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

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;
[Link]();
[Link]();
[Link]();
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;
[Link]();
cout << "\nEnter tape details:" << endl;
[Link]();

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


[Link]();

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


[Link]();

return 0;
}

Common questions

Powered by AI

In a C++ program with multiple inheritance, the constructors of the base classes are executed first, followed by the constructor of the derived class. The sequence ensures that the base classes are properly initialized before the derived class operates on them .

Multiple inheritance involves a class that is derived from more than one base class, allowing it to inherit features from all the base classes. An example would be Class C being derived from both base Class A and base Class B. In contrast, multilevel inheritance is a hierarchy where a class is derived from another derived class. For example, Class C is derived from Class B, which in turn is derived from Class A .

The inheritance hierarchy allows the system to leverage common attributes and methods in the Publication base class, such as title and price, while extending specific features for Books and Tapes. For example, the Book class adds page count, while the Tape class includes playing time. This design supports scalability and maintainability by minimizing redundancy and offering clear paths for further extensions or modifications .

A developer might choose to use inheritance to promote code reuse, enhance scalability, and maintain organization in the codebase. Inheritance captures common traits in a base class, allowing for extensions and specific enhancements in derived classes, as seen in the examples with the Publication hierarchy and factorial calculation system. This approach also supports the open-closed principle, where systems can be extended with new functionality without modifying existing code .

Using `cin` for inputting data can cause issues such as leading to failures in handling spaces within strings. It can also result in input mismatch errors when non-numeric data is entered into numerical fields. These issues can be mitigated by using `cin.ignore()` to clear the input buffer, and `getline(cin, string)` for strings, to handle full lines including spaces. Input validation can also prevent incorrect data types from causing runtime errors .

To modify the Publication class constructor to include parameters, you would define a parameterized constructor. It could look like this: `Publication(string t, float p) : title(t), price(p) {}`. This allows the title and price to be set at the point of object creation, enhancing object initialization flexibility and ensuring that these properties are not left unset .

The program contains a logical error in the `displayResult` method, where `fact` is used instead of `f` to display the factorial result. Correct this error by referencing `f`, the correctly calculated factorial variable in the Factorial class. Change `cout << "Factorial of " << num << " is " << fact << endl;` to `cout << "Factorial of " << num << " is " << f << endl;` .

Polymorphism can be applied in the Publication system to allow base class pointers or references to access derived class objects. This involves using virtual functions for the methods `getdata` and `putdata`, allowing the correct method to be invoked based on the actual object type at runtime, facilitating dynamic method binding. Such an approach enhances flexibility and interoperability in managing books and audiocassettes without code duplication .

In multilevel inheritance, the Base class prompts the user to input a number. The Factorial class, which is derived from Base, includes the logic to calculate the factorial, starting with a factorial value of 1 and multiplying it by numbers up to the input number. The Display class, which is derived from Factorial, outputs the factorial result. The process involves the execution of each class's member functions in order of inheritance .

The design choice to separate the book and audiocassette attributes into distinct classes allows for clear distinction and encapsulation of properties specific to each media type, such as page count for books and playing time for tapes. This separation enhances code readability and reuse. However, it can lead to increased code complexity and potential redundancy if many subclasses become very similar, especially if only minor differences exist between them .

You might also like