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

cout<<timeObject.hour;

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

#include<iostream>
using std::cout;
using std::endl;
#include<iomanip>
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;
dinnerTime.hour=18;
dinnerTime.minute=30; 
dinnerTime.second=0;
cout<<"Dinner will be held at";
printUniversal(dinnerTime);
cout<<"universal time, \nwhich is";
printStandard(dinnerTime);
cout<<"standard time.\n"; 
dinnerTime.hour=29;
dinnerTime.minute=73;
cout<<"\nTime with invalid values:";
printUniversal(dinnerTime);
cout<<endl;
return 0;
}
void printUniversal(const Time &t)
{
cout<<setfill('0')<<setw(2)<<t.hour<<":"
    <<setw(2)<<t.minute<<":"
    <<setw(2)<<t.second;
}
void printStandard(const Time &t)
{
cout<<((t.hour==0||t.hour==12)?12:t.hour%12)<<":"<<setfill('0')
    <<setw(2)<<t.minute<<":"   
    <<setw(2)<<t.second
    <<(t.hour<12?"AM":"PM");
}

OUTPUT:

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 { 
private:
public:
}; 

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(p1.name, 50);
    cout << "Enter age: ";
    cin >> p1.age;
    cout << "Enter salary: ";
    cin >> p1.salary;
    cout << "\nDisplaying Information." << endl;
    cout << "Name: " << p1.name << endl;
    cout <<"Age: " << p1.age << endl;
    cout << "Salary: " << p1.salary;
    return 0;
}

OUTPUT:

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:

StructureUnion
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 »