You are on page 1of 14

Write a program having a base class Student with data member rollno and member

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>

using namespace std;

// Base class Teacher


class Teacher {
public:
string teacher_name;
int age;
string address;

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;
}
};

// Base class Author


class Author {
public:
string author_name;
string address;
int num_books_written;

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;
}
};

// Derived class Scholar inheriting from Teacher and Author


class Scholar : public Teacher, public Author {
public:
// No need to define any additional members or functions, as Scholar inherits
from both Teacher and Author
};

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();

// Display details using the inherited functions


scholarObj.display_teacher_details();
scholarObj.display_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>

using namespace std;

// Base class LocalPhone


class LocalPhone {
public:
string phone;
void input_phone_number() {
cout << "Enter Local Phone Number: ";
cin >> phone;
}

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();

// Display details using inherited functions


intPhoneObj.display_phone_number();
intPhoneObj.display_city_code();
intPhoneObj.display_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>

using namespace std;


// Base class Sales
class Sales {
protected:
float salesData[3];

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];
}
}

void putdata() const {


cout << "Sales Data for the last three months:" << endl;
for (int i = 0; i < 3; ++i) {
cout << "Month " << i + 1 << ": $" << salesData[i] << endl;
}
}
};

// Base class Publication


class Publication {
protected:
string title;
float price;

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;
}

void putdata() const {


cout << "Publication Details:" << endl;
cout << "Title: " << title << endl;
cout << "Price: $" << price << endl;
}
};

// Derived class Book from Publication and Sales


class Book : public Publication, public Sales {
public:
void getdata() {
Publication::getdata();
Sales::getdata();
}

void putdata() const {


Publication::putdata();
Sales::putdata();
}
};

// Derived class Tape from Publication and Sales


class Tape : public Publication, public Sales {
public:
void getdata() {
Publication::getdata();
Sales::getdata();
}

void putdata() const {


Publication::putdata();
Sales::putdata();
}
};
int main() {
// Create objects of Book and Tape classes
Book bookObj;
Tape tapeObj;

// Input data for Book and Tape objects


cout << "Enter details for the Book:" << endl;
bookObj.getdata();

cout << "\nEnter details for the Tape:" << endl;


tapeObj.getdata();

// Display data for Book and Tape objects


cout << "\nDisplaying details for the Book:" << endl;
bookObj.putdata();

cout << "\nDisplaying details for the Tape:" << endl;


tapeObj.putdata();

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) {}

virtual void makeSound() const = 0; // Virtual function to represent the concept


of making a sound

void displayInfo() const {


std::cout << "Name: " << name << std::endl;
std::cout << "Age: " << age << " years" << std::endl;
}
};

class Lion : public Animal {


private:
std::string maneColor;

public:
Lion(const std::string& lionName, int lionAge, const std::string& lionManeColor)
: Animal(lionName, lionAge), maneColor(lionManeColor) {}

void makeSound() const override {


std::cout << "Roar! Roar!" << std::endl;
}

void displayLionInfo() const {


displayInfo();
std::cout << "Mane Color: " << maneColor << std::endl;
}
};

int main() {
// Example usage
Lion lionObj("Simba", 5, "Golden");
// Display Lion information
std::cout << "Lion Information:" << std::endl;
lionObj.displayLionInfo();

// Make Lion sound


std::cout << "Lion Sound:" << std::endl;
lionObj.makeSound();

return 0;
}

You might also like