Structure in C++

struct Books{

char title[50];
char author[50];
char subject[100];
int book_id;

}book;

#include<iostream>
#include<cstring>
using namespace std;



struct Books{

char title[50];
char author[50];
char subject[100];
int book_id;
};

int main(){

    struct Books Book1;
    struct Books Book2;
//Book1 specifications
strcpy(Book1.title,"Learn");
strcpy(Book1.author,"Chand");
strcpy(Book1.subject,"C++");
Book1.book_id=553;

//Book2 specifications
strcpy(Book2.title,"Trel");
strcpy(Book2.author,"Trel");
strcpy(Book2.subject,"Trel");
Book2.book_id=4343;

//Print Book1 Info

cout<<"Book1 Title:"<<Book1.title<<endl;
cout<<"Book1 Author:"<<Book1.author<<endl;
cout<<"Book1 subject:"<<Book1.subject<<endl;
cout<<"Book1 Id:"<<Book1.book_id<<endl;

//Print Book2 Info

cout<<"Book1 Title:"<<Book2.title<<endl;
cout<<"Book1 Author:"<<Book2.author<<endl;
cout<<"Book1 subject:"<<Book2.subject<<endl;
cout<<"Book1 Id:"<<Book1.book_id<<endl;


return 0;
}

Structure as Function arguments

 

#include<iostream>
#include<cstring>
using namespace std;

void printBook(struct Books boook);

struct Books{

char title[50];
char author[50];
char subject[100];
int book_id;
};

int main(){

    struct Books Book1;
    struct Books Book2;
//Book1 specifications
strcpy(Book1.title,"Learn");
strcpy(Book1.author,"Chand");
strcpy(Book1.subject,"C++");
Book1.book_id=553;

//Book2 specifications
strcpy(Book2.title,"Trel");
strcpy(Book2.author,"Trel");
strcpy(Book2.subject,"Trel");
Book2.book_id=4343;


printBook(Book1);

printBook(Book2);

return 0;
}



//Print Book Info
void printBook(struct Books boook)

{

cout<<"Book Title:"<<boook.title<<endl;
cout<<"Book Author:"<<boook.author<<endl;
cout<<"Book subject:"<<boook.subject<<endl;
cout<<"Book Id:"<<boook.book_id<<endl;

}

Pointers to structures:

struct Books *struct_pointer;

struct_pointer=&Book1;

To access the members of the structure  I have to use -> operator as follows

struct_pointer->title;

#include<iostream>
#include<cstring>
using namespace std;

void printBook(struct Books *boook);

struct Books{

char title[50];
char author[50];
char subject[100];
int book_id;
};

int main(){

    struct Books Book1;
    struct Books Book2;
//Book1 specifications
strcpy(Book1.title,"Learn");
strcpy(Book1.author,"Chand");
strcpy(Book1.subject,"C++");
Book1.book_id=553;

//Book2 specifications
strcpy(Book2.title,"Trel");
strcpy(Book2.author,"Trel");
strcpy(Book2.subject,"Trel");
Book2.book_id=4343;


printBook(&Book1);

printBook(&Book2);

return 0;
}



//Print Book Info
void printBook(struct Books *boook)

{

cout<<"Book Title:"<<boook->title<<endl;
cout<<"Book Author:"<<boook->author<<endl;
cout<<"Book subject:"<<boook->subject<<endl;
cout<<"Book Id:"<<boook->book_id<<endl;

}

With typedef:

There are some arrors in teh code  I will fix thsi later..

#include<iostream>
#include<cstring>
using namespace std;

typedef void printBook(Books *boook);

typedef struct
{

char title[50];
char author[50];
char subject[100];
int book_id;
}Books;

int main(){

    Books Book1;
    Books Book2;
//Book1 specifications
strcpy(Book1.title,"Learn");
strcpy(Book1.author,"Chand");
strcpy(Book1.subject,"C++");
Book1.book_id=553;

//Book2 specifications
strcpy(Book2.title,"Trel");
strcpy(Book2.author,"Trel");
strcpy(Book2.subject,"Trel");
Book2.book_id=4343;


printBook(&Book1);

printBook(&Book2);

return 0;
}



//Print Book Info
typedef void printBook(Books *boook)

{

cout<<"Book Title:"<<boook->title<<endl;
cout<<"Book Author:"<<boook->author<<endl;
cout<<"Book subject:"<<boook->subject<<endl;
cout<<"Book Id:"<<boook->book_id<<endl;

}

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published.