0% found this document useful (0 votes)
3 views5 pages

Example - C++ Struc

The document provides two C++ programs that demonstrate how to use structures to store and display information about a person. The first program directly accesses structure members to display information, while the second program utilizes a member function for displaying the data. Both programs prompt the user for input and output the entered details.

Uploaded by

estifanos haile
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Example - C++ Struc

The document provides two C++ programs that demonstrate how to use structures to store and display information about a person. The first program directly accesses structure members to display information, while the second program utilizes a member function for displaying the data. Both programs prompt the user for input and output the entered details.

Uploaded by

estifanos haile
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

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

You might also like