![]() |
How To Use User define Data Types Structure in Cpp With Example |
How To Use User define Data Types Structure in Cpp With Example
Structures are used to represent a record. Like you want to keep record your books in a library.
The structure can be used Keywords struct.
Book ID
Title
Subject
Author
Definition of Structure: A structure defines you must use the struct keyword in the statement.
Structure Syntax
struct [structure_Name]
{
data_type variable 1;
data_type variable 2;
data_type variable 3;
};
struct [structure_Name]
{
data_type variable 1;
data_type variable 2;
data_type variable 3;
};
Structure Syntax Example
struct Book
{
int book_id;
char book_title[100];
char book_name[100];
char book_author[100];
};
struct Book
{
int book_id;
char book_title[100];
char book_name[100];
char book_author[100];
};
How To Use Structure in C++ Language?
#include<iostream>
using namespace std;
#include<string.h>
struct Book
{
int book_id;
char book_title[100];
char book_name[100];
char book_author[100];
};
int main( )
{
struct Book B;
B.book_id = 667407;
strcpy( B.book_title, "C++ Programming Language");
strcpy( B.book_name, "Cpp Language");
strcpy( B.book_author ",Bala Swami");
cout<<"Book ID:"<<B.book_id<<"\n";
cout<<"Book Title:"<<B.book_title<<"\n";
cout<<"Book Name:"<<B.book_name<<"\n";
cout<<"Book Author:"<<B.book_author<<"\n";
}
*****OUTPUT*****
Book ID: 667407
Book Title: C++ Programming Language
Book Name: Cpp Language Book
Author: Bala Swami
#include<iostream>
using namespace std;
#include<string.h>
struct Book
{
int book_id;
char book_title[100];
char book_name[100];
char book_author[100];
};
int main( )
{
struct Book B;
B.book_id = 667407;
strcpy( B.book_title, "C++ Programming Language");
strcpy( B.book_name, "Cpp Language");
strcpy( B.book_author ",Bala Swami");
cout<<"Book ID:"<<B.book_id<<"\n";
cout<<"Book Title:"<<B.book_title<<"\n";
cout<<"Book Name:"<<B.book_name<<"\n";
cout<<"Book Author:"<<B.book_author<<"\n";
}
*****OUTPUT*****
Book ID: 667407
Book Title: C++ Programming Language
Book Name: Cpp Language Book
Author: Bala Swami
Structures Function Arguments
A Structure-function argument can be used in the same way as we can pass any other variable.How To Use Structure Function in C++ Language?
#include<iostream>
using namespace std;
#include<string.h>
struct student
{
int s_id;
char sname[50];
char sfname[50];
char smname[100];
};
void printdetails( struct student s);
int main( )
{
struct student s1;
struct student s2;
s1.s_id=63633;
strcpy(s1.sname, "Jitendra");
strcpy(s1.sfname, "Sohan");
strcpy(s1.smname,"Geeta");
s2.s_id=6367833;
strcpy(s2.sname, "Rohan");
strcpy(s2.sfname, "Sohan");
strcpy(s2.smname,"Rekha");
printdetails(s1);
printdetails(s2);
return 0;
}
void print details( struct student s )
{
cout<<"Student Id :"<<s.s_id<<"\n";
cout<<"Student Name :"<<s.sname <<"\n";
cout<<"Student Father Name :"<<s.sfname <<"\n";
cout<<"Student Mother Name:"<<s.smname <<"\n";
}
*****OUTPUT*****
Student Id: 63633
Student Name : Jitendra
Student Father Name: Sohan
Student Mother Name: Geeta
Student Id: 6367833
Student Name: Rohan
Student Father Name: Sohan
Student Mother Name: Rekha
#include<iostream>
using namespace std;
*****OUTPUT*****
Student Id: 63633
Student Name : Jitendra
Student Father Name: Sohan
Student Mother Name: Geeta
Student Id: 6367833
Student Name: Rohan
Student Father Name: Sohan
Student Mother Name: Rekha
How do we write a C++ program to print patient's records using Structure?
#include<iostream>
using namespace std;
#include<string.h>
struct parents
{
char sname[100];
char fname[100];
char mname[100];
char faddress[200];
int faddhar_num;
int fmobile_no;
int mmobil_no;
};
int main()
{
struct parents p;
strcpy(p.sname,"Shubham");
strcpy(p.fname,"Ramesh");
strcpy(p.mname,"Radhika");
strcpy(p.faddress,"Delhi");
p.faddhar_num=67574357;
p.fmobile_no=96905657;
p.mmobil_no=9787877;
cout<<"Student Name:"<<p.sname <<"\n";
cout<<"Student Father Name:"<<p.fname<<"\n";
cout<<"Student Mother Name:"<<p.mname <<"\n";
cout<<"Student Father Addresd:"<<p.faddress <<"\n";
cout<<"Student Father Aadhar Number:"<<p.faddhar_num <<"\n";
cout<<"Student Father Mobile Number:"<<p.fmobile_no <<"\n";
cout<<"Student Mother Mobile Number:"<<p.mmobil_no <<"\n";
}
*****OUTPUT*****
Student Name: Shubham
Student Father Name: Ramesh
Student Mother Name: Radhika
Student Father Address: Delhi
Student Father Aadhar Number:67574357
Student Father Mobile Number:96905657
Student Mother Mobile Number:9787877
#include<iostream>
using namespace std;
*****OUTPUT*****
Student Name: Shubham
Student Father Name: Ramesh
Student Mother Name: Radhika
Student Father Address: Delhi
Student Father Aadhar Number:67574357
Student Father Mobile Number:96905657
Student Mother Mobile Number:9787877