cpp structs code example

Example 1: what is a struct in c++

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

Example 2: struct c++

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

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

Tags:

C Example