Declare Class instance and use them : Class Basics « Class « C++






Declare Class instance and use them

Declare Class instance and use them
#include <iostream>
#include <cstring>
using namespace std;

class Book {
  char title[80];             // book title
  char author[40];            // author
  int number;                 // number in library
public:
  void store(char *t, char *name, int num);
  void show();
};

void Book::store(char *t, char *name, int num)
{
  strcpy(title, t);
  strcpy(author, name);
  number = num;
}

void Book::show()
{
  cout << "Title: " << title << endl;
  cout << "Author: " << author << endl;
  cout << "Number on hand: " << number << endl;
}

int main()
{
  Book book1, book2, book3;

  book1.store("D", "t", 2);
  book2.store("T", "v", 2);
  book3.store("T", "e", 1);

  book1.show();
  book2.show();
  book3.show();

  return 0;
}



           
       








Related examples in the same category

1.Class forward declarationClass forward declaration
2.Simplest class definitionSimplest class definition
3.A simple class with member variable, constructor, destructorA simple class with member variable, constructor, destructor
4.Address class: class definition and implementationAddress class: class definition and implementation
5.Declare class instanceDeclare class instance
6.Constructor: different parameter typeConstructor: different parameter type
7.Assign object1 to object2Assign object1 to object2
8.Init Object arrayInit Object array