What's new

C++ program

niel32

Eternal Poster
Established
Joined
Feb 4, 2018
Posts
1,382
Reaction
316
Points
352
Age
24
C++
Imagine a publishing company that markets both book and audiocassette
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 at the keyboard, and a putdata() function to display
its data.
Write a main() program to test the book and tape classes by creating instances of
them, asking the user to fill in data with getdata(), and then displaying the data
with putdata().
 
Here is one possible implementation of the classes publication, book, and tape, along with a main() function to test them:

C++:
#include <iostream>
#include <string>
using namespace std;

class publication {
protected:
    string title;
    float price;
public:
    void getdata() {
        cout << "Enter title: ";
        getline(cin, title);
        cout << "Enter price: ";
        cin >> price;
        cin.ignore(); // ignore newline character
    }
    void putdata() const {
        cout << "Title: " << title << endl;
        cout << "Price: " << price << endl;
    }
};

class book : public publication {
private:
    int pagecount;
public:
    void getdata() {
        publication::getdata(); // call base class getdata() function
        cout << "Enter page count: ";
        cin >> pagecount;
        cin.ignore(); // ignore newline character
    }
    void putdata() const {
        publication::putdata(); // call base class putdata() function
        cout << "Page count: " << pagecount << endl;
    }
};

class tape : public publication {
private:
    float playingtime;
public:
    void getdata() {
        publication::getdata(); // call base class getdata() function
        cout << "Enter playing time (in minutes): ";
        cin >> playingtime;
        cin.ignore(); // ignore newline character
    }
    void putdata() const {
        publication::putdata(); // call base class putdata() function
        cout << "Playing time (in minutes): " << playingtime << endl;
    }
};

int main() {
    book b;
    tape t;

    cout << "Enter data for book:\n";
    b.getdata();

    cout << "\nEnter data for tape:\n";
    t.getdata();

    cout << "\nBook data:\n";
    b.putdata();

    cout << "\nTape data:\n";
    t.putdata();

    return 0;
}

In this implementation, the base class publication has two data members (title and price) and two member functions (getdata() and putdata()), which are used by the derived classes book and tape to get and display their respective data. The derived class book adds a pagecount data member and overrides the getdata() and putdata() functions to include it, while the derived class tape adds a playingtime data member and overrides the getdata() and putdata() functions to include it.

In the main() function, we create instances of the book and tape classes, ask the user to input data for each of them using their respective getdata() functions, and then display the data using their respective putdata() functions.
 
SAMPLE 1

#include <iostream>
#include <string>

class Publication {
protected:
std::string title;
float price;
public:
void getdata() {
std::cout << "Enter the title: ";
std::getline(std::cin, title);
std::cout << "Enter the price: ";
std::cin >> price;
std::cin.ignore(); // to consume the newline character after price input
}
void putdata() const {
std::cout << "Title: " << title << std::endl;
std::cout << "Price: " << price << std::endl;
}
};

class Book : public Publication {
private:
int page_count;
public:
void getdata() {
Publication::getdata(); // call the base class function to get title and price
std::cout << "Enter the page count: ";
std::cin >> page_count;
std::cin.ignore(); // to consume the newline character after page count input
}
void putdata() const {
Publication::putdata(); // call the base class function to display title and price
std::cout << "Page count: " << page_count << std::endl;
}
};

class Tape : public Publication {
private:
float playing_time;
public:
void getdata() {
Publication::getdata(); // call the base class function to get title and price
std::cout << "Enter the playing time in minutes: ";
std::cin >> playing_time;
std::cin.ignore(); // to consume the newline character after playing time input
}
void putdata() const {
Publication::putdata(); // call the base class function to display title and price
std::cout << "Playing time: " << playing_time << " minutes" << std::endl;
}
};

int main() {
Book book;
Tape tape;

std::cout << "Enter the details for a book:\n";
book.getdata();
std::cout << "\nEnter the details for a tape:\n";
tape.getdata();

std::cout << "\nThe details of the book are:\n";
book.putdata();
std::cout << "\nThe details of the tape are:\n";
tape.putdata();

return 0;
}




Explaination!

In this implementation, Publication is the base class for Book and Tape, and it contains the common attributes title and price. The getdata() and putdata() functions in Publication are declared as virtual, which allows them to be overridden by the corresponding functions in the derived classes.

Book and Tape inherit from Publication using the public access specifier. This means that the public members of Publication become public members of Book and Tape, and the protected members of Publication become protected members of Book and Tape.

Book adds the attribute page_count and overrides the getdata() and putdata() functions to handle it. Similarly, Tape adds the attribute playing_time and overrides the getdata() and putdata() functions to handle it.

In the main() function, we create instances of Book and Tape, and call their getdata() functions to get input from the user. Then we call their putdata() functions to display the input. Note that we use the Publication functions `




SAMPLE 2

#include <iostream>
#include <string>
using namespace std;

class publication {
string title;
float price;
public:
void getdata() {
cout << "Enter the title: ";
getline(cin, title);
cout << "Enter the price: ";
cin >> price;
cin.ignore(); // ignore the newline character left in the input buffer
}
void putdata() const {
cout << "Title: " << title << endl;
cout << "Price: $" << price << endl;
}
};

class book : public publication {
int page_count;
public:
void getdata() {
publication::getdata();
cout << "Enter the page count: ";
cin >> page_count;
cin.ignore(); // ignore the newline character left in the input buffer
}
void putdata() const {
publication::putdata();
cout << "Page count: " << page_count << endl;
}
};

class tape : public publication {
float playing_time;
public:
void getdata() {
publication::getdata();
cout << "Enter the playing time (in minutes): ";
cin >> playing_time;
cin.ignore(); // ignore the newline character left in the input buffer
}
void putdata() const {
publication::putdata();
cout << "Playing time: " << playing_time << " minutes" << endl;
}
};

int main() {
book b;
tape t;

cout << "Enter the details for the book:" << endl;
b.getdata();
cout << "\nEnter the details for the tape:" << endl;
t.getdata();

cout << "\nThe details of the book are:" << endl;
b.putdata();
cout << "\nThe details of the tape are:" << endl;
t.putdata();

return 0;
}

In this program, we define the publication class as the base class, and the book and tape classes as derived classes. The getdata() function is used to get data from the user at the keyboard, and the putdata() function is used to display the data.

In the main() function, we create instances of the book and tape classes, and then call the getdata() and putdata() functions to get input from the user and display the output.
 
Last edited:

Similar threads

Back
Top