C++ program to demonstrate Class with array of Objects

//class Book with data members: book_name, ISBN, author and price
//and appropriate function members to read and display data
//read records of 3 books and display

#include <iostream>
#include <iomanip>
using namespace std;
class Book
{
    char book_name[20],ISBN[20],author[20];
    float price;
    public:
        void Read();
        void Display();
};
void Book::Read()
{
    cout<<"Enter book name, ISBN, author name and price: "<<endl;
    cin>>book_name>>ISBN>>author>>price;
    // cout<<"Enter book name: ";
    // cin.get(book_name,20,'\n');
    // cout<<"Enter ISBN: ";
    // cin>>ISBN;
    // cout<<"Enter name of author: ";
    // cin.get(author,20,'\n');
    // cout<<"Enter price: ";
    // cin>>price;
}
void Book::Display()
{
    cout<<book_name<<setw(10)<<ISBN<<setw(10)<<author<<setw(10)<<price<<endl;
}
int main()
{
    Book b[3];
    int i;
    for(i=0;i<3;i++)
    {
        cout<<"For book "<<(i+1)<<endl;
        b[i].Read();
    }
    cout<<"Displaying the details in tabular form:"<<endl;
    cout<<"Book Name"<<setw(10)<<"ISBN"<<setw(10)<<"Author"<<setw(10)<<"Price"<<endl;

    for(i=0;i<3;i++)
        b[i].Display();
    return 0;
}

No comments:

Post a Comment