Home | | Computer Science 11th std | C++: Structure Assignments

# C++: Structure Assignments

Structures can be assigned directly instead of assigning the values of elements individually.

Structure Assignments

Structures can be assigned directly instead of assigning the values of elements individually.

## Example

If Mahesh and Praveen are same age and same height and weight then the values of Mahesh can be copied to Praveen

Structure assignment is possible only if both structure variables/ objects are same type.

struct Student

{

int age;

float height, weight;

}mahesh;

The age of Mahesh is 17 and the height and weights are 164.5 and 52.5 respectively.The following statement will perform the assignment.

mahesh = {17, 164.5, 52.5};

praveen =mahesh;

will assign the same age, height and weight to Praveen.

## The following C++ program reads student information through keyboard and displays the same

#include <iostream>

using namespace std;

struct Student

{

int age;

float height, weight;

} mahesh; void main( )

{

cout<< “ Enter the age:”<<endl;

cin>>mahesh.age;

cout<< “Enter the height:”<<endl;

cin>>mahesh.height;

cout<< “Enter the weight:”<<endl;

cin>>mahesh.weight;

cout<< “The values entered for Age, height and weight are”<<endl;

cout<<mahesh.age<< “\t”<<mahesh.height<< “\t”<<Mahesh. weight;

}

### Output:

Enter the age:

18

Enter the height:

160.5

Enter the weight:

46.5

The values entered for Age, height and weight are

18  160.5    46.5

## The following C++ Program assigns data to members of a structure variable and displays the contents

#include<iostream>

using namespace std;

struct Employee

{

char name[50];

int age;

float salary;

};

int main()

{

Employee e1;

cout<< "Enter Full name: ";

cin>>e1.name;

cout<<endl<<"Enter age: ";

cin>>e1.age;

cout<<endl<< "Enter salary: ";

cin>>e1.salary;

cout<< "\nDisplaying Information." <<endl;

cout<< "Name: " <<e1.name <<endl;

cout<<"Age: " <<e1.age <<endl;

cout<< "Salary: " <<e1.salary;

return 0;

}

### Output:

Enter Full name:

Ezhil

Enter age:

27

Enter salary:

40000.00

Displaying Information.

Name: Ezhil

Age: 27

Salary: 40000.00

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 12 : Arrays and Structures : C++: Structure Assignments |