Structures In C++
Structures in C++ are user defined data types which are used to store
group of items of non-similar data types. The ‘struct’ keyword is used to
create a structure.
Structures in C++ can contain two
types of members:
Fig: structure in c++ |
· Data Member:
These
members are normal C++ variables. We can create a structure with variables of different data types in C++.
· Member Functions:
These
members are normal C++ functions. Along with variables, we can also include
functions inside a structure declaration.
Syntax:
struct structure name
{
structure memeber1;
structure memeber2;
structure memeber3;
};<structure variable>;
Example:
struct MCA
{
int age;
char name[20];
}p;
Program:
#include<iostream.h>
#include<conio.h>
struct MCA
{
char name [30];
char place[50];
int pincode;
}p;
void main()
{
cout<<”input name”;
cin>>p.name;
cout<<”input place”;
cin>>p.place;
cout<<”input pincode”;
cin>>p.pincode;
cout<<p.name;
cout<<p.place;
cout<<p.pincode;
getch();
}
Output:
Input name Pramod
Input place Delhi
Input pinocde 201301
Pramod
Delhi
201301
No comments:
Post a Comment