Show the book Information using Structure and Function in C++


The program defines a struct named Books that has four members: title, author, subject, and book_id. It also defines a function printBook that takes a struct Books argument and prints the details of the book. In main(), two struct Books objects, Book1 and Book2, are defined and their member variables are initialized with some values using strcpy() and direct assignment.

Then, printBook() is called twice, passing Book1 and Book2 as arguments respectively. Inside the printBook() function, the details of the book are printed to the console using cout statements. The program then returns 0 to indicate successful execution.

Source Code

#include<iostream>
#include<cstring>
using namespace std;
void printBook(struct Books book);
struct Books
{
  char  title[50];
  char  author[50];
  char  subject[100];
  int   book_id;
};
int main()
{
  struct Books Book1;
  struct Books Book2;
  strcpy(Book1.title,"Mathematics for IITJEE");
  strcpy(Book1.author,"RD Sharma");
  strcpy(Book1.subject,"JEE Mathematics");
  Book1.book_id = 6495407;
  strcpy( Book2.title,"Physics");
  strcpy( Book2.author,"IE Irodov");
  strcpy( Book2.subject,"JEE Mechanical Physics");
  Book2.book_id=6495700;
  printBook(Book1);
  printBook(Book2);
  return 0;
}
void printBook(struct Books book)
{
  cout<<"Book title : "<<book.title<<endl;
  cout<<"Book author : "<<book.author<<endl;
  cout<<"Book subject : "<<book.subject<<endl;
  cout<<"Book id : "<<book.book_id<<endl;
}
To download raw file Click Here

Output

Book title : Mathematics for IITJEE
Book author : RD Sharma
Book subject : JEE Mathematics
Book id : 6495407
Book title : Physics
Book author : IE Irodov
Book subject : JEE Mechanical Physics
Book id : 6495700


Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples