Structure in C++

Page Contents:

Syntax of Structure in C++:
Accessing Structure Members:
The Keywords class and struct:
Comparison between Structure and Union

Structures are aggregate data types that are, they can be built using elements of other types including other structs.

Syntax of Structure in C++:

 struct structure_name 
data_type variable_name;
data_type variable name;

An Example can be given as:

struct employee
int id,password;
char name;
floot salary;

So the Keyword struct introduces the structure definition. the identifier employee is the structure tag that names the structure definition and is used to declare variables of the structure type. The names declared in the braces of the structure definition are the structure’s members. Members of the same structure must have unique names, but two different structures may contain members of the same name without conflict. Each structure definition must end with a semicolon.

Accessing Structure Members:

Members of a structure or a class are accessed using the member access operators the dot operator(.) and the arrow operator(->). The dot operator accesses a structure or class member by the variable name for the object or by a reference to the object. For example, to print member hour of timeObject, use the statement


Implementing User-defined Type Time with a C-like struct

using std::cout;
using std::endl;
using std::setfill;
using std::setw;
struct Time
int hour;
int minute;
int second;
void printUniversal(const Time &);
void printStandard(const Time &);
int main()
Time dinnerTime;
cout<<"Dinner will be held at";
cout<<"universal time, \nwhich is";
cout<<"standard time.\n"; 
cout<<"\nTime with invalid values:";
return 0;
void printUniversal(const Time &t)
void printStandard(const Time &t)


Dinner will be held at18:30:00universal time,                                      which is6:30:00PMstandard time.
Time with invalid values:29:73:00 

By default, structures are passed by value.To avoid the overhead of copying a structure, pass the structure by reference

The Keywords class and struct:

You can also use the keyword struct to define a class, such as the class Student.

struct Student { 

The keywords class and struct only vary with respect to data encapsulation; the default for access to members of a class defined as a struct is public. In contrast to a class defined using the class keyword, all the class members are public unless a private label is used. This allows the programmer to retain C compatibility

1.Examples for Structure:

#include <iostream>
using namespace std;
struct Person
    char name[50];
    int age;
    float salary;

int main()
    Person p1;
    cout << "Enter Full name: ";
    cin.get(, 50);
    cout << "Enter age: ";
    cin >> p1.age;
    cout << "Enter salary: ";
    cin >> p1.salary;
    cout << "\nDisplaying Information." << endl;
    cout << "Name: " << << endl;
    cout <<"Age: " << p1.age << endl;
    cout << "Salary: " << p1.salary;
    return 0;


Enter Full name: Abhishek verma 
Enter age: 18 
Enter salary: 50245.6 
Displaying Information. 
Name: Abhishek verma 
Age: 18 
Salary: 50245.6

Comparison between Structure and Union:

Memory allotted for a structure is equal to the space require collectively by all the members of that structure Memory allotted for a union is equal to the space required by the largest member of that union.
Data is more secure in structures.Data can be corrupted in a union.
Structure provide ease of programmingUnions are comparatively difficult for programming.
Structure must be used when information of all the member elements of a structure are to be stored.Unions must be used when only one of the member elements of the union is to be stored.

Translate »