Archive
C++ Program to implement Hierarchical Inheritance (Part 1) – Q20
Q20. Program to implement Hierarchical Inheritance (Part 1):
Imagine a publishing company that markets books as well as audicassette. Create a class publication that stores the title and price. From this class derive two classes as follows:
– Book, which adds a Pagecount, AuthurName, Edition
– Tape, which adds Playnigtime in minutes, SpeakerName, Date of Recording
Each of these classes should have a getdata( ) and putdat()
Functions to read and to display the data reapectively.
… from College notes (BCA/MCA assignments):
#include <iostream.h> #include <conio.h> class CPubs{ protected: char title[20]; float price; }; class CBooks : public CPubs{ private: char aname[20]; int pcount, edition; public: void getdata(){ cout<<"\n Enter Title of Book: "; cin>>title; cout<<"\n Enter Price: "; cin>>price; cout<<"\n Enter Author Name: "; cin>>aname; cout<<"\n Enter Page Count: "; cin>>pcount; cout<<"\n Enter Edition: "; cin>>edition; } void showdata(){ cout<<"\n\t Book Title: "<<title; cout<<"\n\t Price: "<<price; cout<<"\n\t Author Name: "<<aname; cout<<"\n\t Page Count: "<<pcount; cout<<"\n\t Edition no.: "<<edition; } }; class CTapes : public CPubs{ private: char sname[20]; int ptime; int dd, mm, yy; public: void getdata(){ cout<<"\n Enter Title of Cassette: "; cin>>title; cout<<"\n Enter Price: "; cin>>price; cout<<"\n Enter Speaker Name: "; cin>>sname; cout<<"\n Enter Play Time: "; cin>>ptime; cout<<"\n Enter Date of Recording (dd mm yy): "; cin>>dd>>mm>>yy; } void showdata(){ cout<<"\n\t Cassette Title: "<<title; cout<<"\n\t Price: "<<price; cout<<"\n\t Speaker Name: "<<sname; cout<<"\n\t Play Time: "<<ptime; cout<<"\n\t Date of Recording: " <<dd<<":"<<mm<<":"<<yy; } }; void main(){ CBooks Obook; CTapes Otape; clrscr(); cout<<"\n Publishing Company"; cout<<"\n ~~~~~~~~~~~~~~~~~~"; cout<<"\n Enter the Information of BOOKS:-"<<endl; Obook.getdata(); cout<<"\n Enter the Information of TAPES:-"<<endl; Otape.getdata(); clrscr(); cout<<"\n Information of BOOKS & TAPES : -"; cout<<"\n\n BOOKS : -"<<endl; Obook.showdata(); cout<<"\n\n TAPES : -"<<endl; Otape.showdata(); getch(); }
Output:
Publishing Company
Enter the Information of BOOKS:-
Enter Title of Book: cplusplus
Enter Price: 250
Enter Author Name: stroustrup
Enter Page Count: 700
Enter Edition: 4
Enter the Information of TAPES:-
Enter Title of Cassette: bacardiblast
Enter Price: 125
Enter Speaker Name: ricky
Enter Play Time: 90
Enter Date of Recording (dd mm yy): 10 03 2000
Information of BOOKS & TAPES : –
BOOKS : –
Book Title: cplusplus
Price: 250
Author Name: stroustrup
Page Count: 700
Edition no.: 4
TAPES : –
Cassette Title: bacardiblast
Price: 125
Speaker Name: ricky
Play Time: 90
Date of Recording: 10:3:2000