struct in c++ variable add program code example

Example 1: structs in c++

#include <bits/stdc++.h>
#include <iostream>

#define ll long long

using namespace std;

struct student{
	int roll;
	string name;
	int age;
	
	void studentDetails(){
		cout<<"Name is "<<name<<" Age is "<<age<<" roll no is "<<roll<<endl;
	}
};


int main(){
	
	student sumant;
	sumant.roll = 30;
	sumant.name = "Sumant Tirkey";
	sumant.age = 18;
	
	sumant.studentDetails();
	cout<<endl;

    return 0;
}

Example 2: 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;
}