C++ Practical
Class Design Snippet-1
Question 1: Imagine a publishing company that markets both book and audio-cassette versions of its works. Create a class publication that stores the title (a string) and price (type float) of a publication. From this class derive two classes: book, which adds a page count (type int) and tape, which adds a playing time in minutes (type float). Each of these three classes should have a getdata() function to get its data from the user and a putdata() function to display its data. Write a main() program to test the book and tape classes by creating instances of then displaying the data with putdata().
Answer:
#include<iostream.h> #include<string.h> #include <conio.h> #include <stdio.h> #define max 100 void flushkb(); class publication { protected: char title[max]; float price; public: publication( ) { strcpy(title,""); price=0.0; } publication(char t[],float pr) { strcpy(title,t); price=pr; } void getdata( ) { cout<<"Enter title:"; cin.get(title,max); cout << "Enter price:"; cin>>price; //cin.ignore(1, '\n'); } void putdata( ) { cout<<"Title:"<< title<<"\tPrice:"<<price<<"\t"; } }; class Book : public publication { private: int pgcount; public: Book( ): publication( ) { pgcount=0; } Book(char t[],float pr,int pgc): publication(t,pr) { pgcount=pgc; } void getdata( ) { publication::getdata( ); cout<<"Enter pagecount:"; cin>>pgcount; } void putdata() { publication:: putdata(); cout << "Page Count:" << pgcount << "\n"; } }; class tape: public publication { private: float min; public: tape() { min =0.0; } void getdata( ) { cin.ignore(3, '\n'); publication::getdata( ); cout<<"Enter minutes:"; cin>>min; } void putdata( ) { publication::putdata( ); cout<<"minutes:" << min<<"\n"; } }; void main( ) { clrscr(); Book B; B.getdata( ); B.putdata( ); tape t; t.getdata( ); t.putdata(); getch(); } void flushkb() { char ch; while ((ch =getchar()) != '\n' && ch != EOF) continue; }
No comments:
Post a Comment
We love to hear your thoughts about this post!
Note: only a member of this blog may post a comment.