Example: C++ Structure
// Program to assign data to members of a structure variable
1. #include <iostream>
using namespace std;
struct Person
string first_name;
string last_name;
int age;
float salary;
};
int main()
Person p1;
cout << "Enter first name: ";
cin >> p1.first_name;
cout << "Enter last name: ";
cin >> p1.last_name;
cout << "Enter age: ";
cin >> p1.age;
cout << "Enter salary: ";
cin >> p1.salary;
cout << "\nDisplaying Information." << endl;
cout << "First Name: " << p1.first_name << endl;
cout << "Last Name: " << p1.last_name << endl;
cout << "Age: " << p1.age << endl;
cout << "Salary: " << p1.salary;
return 0;
Run Code
Output
Enter first name: Jane
Enter last name: Smith
Enter age: 27
Enter salary: 10000
Displaying Information.
First Name: Jane
Last Name: Smith
Age: 27
Salary: 10000
2. #include <iostream>
using namespace std;
struct Person {
string first_name;
string last_name;
int age;
float salary;
// member function to display information about the person
void display_info() {
cout << "First Name: " << first_name << endl;
cout << "Last Name: " << last_name << endl;
cout << "Age: " << age << endl;
cout << "Salary: " << salary << endl;
};
int main() {
Person p1;
cout << "Enter first name: ";
cin >> p1.first_name;
cout << "Enter last name: ";
cin >> p1.last_name;
cout << "Enter age: ";
cin >> p1.age;
cout << "Enter salary: ";
cin >> p1.salary;
// display information using member function
cout << "\nDisplaying Information." << endl;
p1.display_info();
return 0;
Run Code
Output
Enter first name: Jane
Enter last name: Smith
Enter age: 27
Enter salary: 10000
Displaying Information.
First Name: Jane
Last Name: Smith
Age: 27
Salary: 10000