Inheritance
Inheritance
functions getnum() to input rollno and putnum() to display rollno. A class Test is
derived from class Student with data member marks and member functions getmarks to input
marks and putmarks() to display marks. Class Sports is also derived from class Student
with data member score and member functions getscore() to input score and
putscore() to display score. The class Result is inherited from two base classes, class Test
and class Sports with data member total and a member function display() to display
rollno, marks, score and the total(marks + score).
#include <iostream>
#include <string>
void input_teacher_details() {
cout << "Enter Teacher Name: ";
cin >> teacher_name;
cout << "Enter Teacher Age: ";
cin >> age;
cout << "Enter Teacher Address: ";
cin.ignore(); // Ignore newline character left in the buffer
getline(cin, address);
}
void display_teacher_details() {
cout << "Teacher Details:" << endl;
cout << "Name: " << teacher_name << endl;
cout << "Age: " << age << endl;
cout << "Address: " << address << endl;
}
};
void input_author_details() {
cout << "Enter Author Name: ";
cin >> author_name;
cout << "Enter Author Address: ";
cin.ignore();
getline(cin, address);
cout << "Enter Number of Books Written: ";
cin >> num_books_written;
}
void display_author_details() {
cout << "Author Details:" << endl;
cout << "Name: " << author_name << endl;
cout << "Address: " << address << endl;
cout << "Number of Books Written: " << num_books_written << endl;
}
};
int main() {
// Create an object of the derived class Scholar
Scholar scholarObj;
// Input details for both Teacher and Author using the inherited functions
scholarObj.input_teacher_details();
scholarObj.input_author_details();
return 0;
}
Write a class LocalPhone that contains an attribute phone to store a local telephone
number. The class contains member functions to input and display phone number. Write
a child class NatPhone for national phone numbers that inherits LocPhone class. It
additionally contains an attribute to store city code. It also contains member functions to
input and show the city code. Write another class IntPhone for international phone
numbers that inherit NatPhone class. It additionally contains an attribute to store country
code. It also contains member functions to input and show the country code. Test these
classes from main() by creating objects of derived classes and testing functions in a way
that clear concept of multi-level Inheritance.
#include <iostream>
#include <string>
void display_phone_number() {
cout << "Local Phone Number: " << phone << endl;
}
};
// Derived class NatPhone for national phone numbers inheriting from LocalPhone
class NatPhone : public LocalPhone {
public:
string city_code;
void input_city_code() {
cout << "Enter City Code: ";
cin >> city_code;
}
void display_city_code() {
cout << "City Code: " << city_code << endl;
}
};
// Derived class IntPhone for international phone numbers inheriting from
NatPhone
class IntPhone : public NatPhone {
public:
string country_code;
void input_country_code() {
cout << "Enter Country Code: ";
cin >> country_code;
}
void display_country_code() {
cout << "Country Code: " << country_code << endl;
}
};
int main() {
// Create an object of the derived class IntPhone
IntPhone intPhoneObj;
// Input details for local, national, and international phone numbers using
inherited functions
intPhoneObj.input_phone_number();
intPhoneObj.input_city_code();
intPhoneObj.input_country_code();
return 0;
}
Start with the publication, book and tape classes. Add base class sales that holds an array
of three floats so that it can record the dollar sales of a particular publication for
55
the last three months. Include a getdata() function to get three sale amount from the
user and a putdata() function to display the sales figure.
Alter the book and tape classes, so they are derived from both publication and
sales.
An object of book or tape should should input and output ans sales data along
with
other data.
Write a main function to create a book and tape object and exercise their input/output
capabilitie
#include <iostream>
#include <string>
public:
void getdata() {
cout << "Enter sales data for the last three months:" << endl;
for (int i = 0; i < 3; ++i) {
cout << "Month " << i + 1 << ": ";
cin >> salesData[i];
}
}
public:
void getdata() {
cout << "Enter publication title: ";
cin.ignore(); // Ignore newline character left in the buffer
getline(cin, title);
cout << "Enter publication price: $";
cin >> price;
}
return 0;
}
Suppose you are designing a system for a zoo. You have a base
class called Animal with properties like name and age. Now, you
need to create specific class for different type of animal such as
Lion, which is inheriting from the Animal class. Discuss how you
would structure the classes and what additional attributes or
methods subclass might have.
#include <iostream>
#include <string>
class Animal {
protected:
std::string name;
int age;
public:
Animal(const std::string& animalName, int animalAge) : name(animalName),
age(animalAge) {}
public:
Lion(const std::string& lionName, int lionAge, const std::string& lionManeColor)
: Animal(lionName, lionAge), maneColor(lionManeColor) {}
int main() {
// Example usage
Lion lionObj("Simba", 5, "Golden");
// Display Lion information
std::cout << "Lion Information:" << std::endl;
lionObj.displayLionInfo();
return 0;
}