Construct object in an object array one by one : Instance « Class « C++






Construct object in an object array one by one

   
#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);
  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;
   cout << "In constructor." << endl;
 }

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

   int i;

   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("K", "J","P", 29.95);

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








Related examples in the same category

1.Declare the instance variable for a classDeclare the instance variable for a class
2.Loop through an object array and call its method
3.class array and object method call