what is a struct C++ 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: function in struct c++

struct foo {
  int bar;
  foo() : bar(3) {}   //look, a constructor
  int getBar() 
  { 
    return bar; 
  }
};

foo f;
int y = f.getBar(); // y is 3

Example 3: what is a struct in c++

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

Example 4: struct c++

struct product {
  int weight;
  double price;
} apple, banana, melon;

Example 5: c++ structure

struct product {
  int weight;
  double price;
} ;

product apple;
product banana, melon;

Example 6: struct c++

//Struct is a compound data type that contains different variables of different types.
struct Student
{
    char stuName[30];
    int stuRollNo;
    int stuAge;
};