Loop through an object array and call its method : Instance « Class « C++






Loop through an object array and call its method

   
#include <iostream>
#include <iomanip>
#include <string.h>
using namespace std;
class Book 
{
  public: 
    void show_title(void) { cout << title << '\n'; };
    void show_book(void) 
    { 
      show_title();
      show_publisher();
    };
    Book(char *title, char *author, char *publisher, float price);
    ~Book(void) { cout << "Destroying the entry for " << title << endl; };
  private:
    char title[256];
    char author[64];
    float price;
    char publisher[256];
    void show_publisher(void) { cout << publisher << '\n'; };
};

Book::Book(char *title, char *author, char *publisher, float price)
{
   strcpy(Book::title, title);
   strcpy(Book::author, author);
   strcpy(Book::publisher, publisher);
   Book::price = price;
}

int main(void)
{
   Book *Library[4];
   int i = 0;

   Library[0] = new Book("A", "B","C", 49.95);
   Library[1] = new Book("D", "E", "F", 54.95);
   Library[2] = new Book("G", "H","I", 49.95);
   Library[3] = new Book("J", "K","P", 24.95);

   for (i = 0; i < 4; i++)
     Library[i]->show_book();
   for (i = 0; i < 4; i++)
     delete Library[i];
}
  
    
    
  








Related examples in the same category

1.Declare the instance variable for a classDeclare the instance variable for a class
2.Construct object in an object array one by one
3.class array and object method call