what is structure in c++ code example

Example 1: what is a struct in c++

struct Person
{
    char name[50];
    int age;
    float salary;
};

Example 2: c++ structure

struct product {
  int weight;
  double price;
} ;

product apple;
product banana, melon;

Example 3: structure in c++ all in one

#include <iostream>
using namespace std;

struct Person {
    char name[50];
    int age;
    float salary;
};

struct Person p;

Person getData();
void displayData();

int main()
{
    struct Person p;
  
    p = getData();
    displayData();
  
    return 0;
}

Person getData() {

    cout << "Enter Full name: ";
    cin.get(p.name, 50);

    cout << "Enter age: ";
    cin >> p.age;

    cout << "Enter salary: ";
    cin >> p.salary;

    return p;
}

void displayData()
{
    cout << "\nDisplaying Information." << endl;
    cout << "Name: " << p.name << endl;
    cout << "Age: " << p.age << endl;
    cout << "Salary: " << p.salary;
}

Tags: