c++ struct example
Example 1: function in struct c++
struct foo {
int bar;
foo() : bar(3) {}
int getBar()
{
return bar;
}
};
foo f;
int y = f.getBar();
Example 2: what is a struct in c++
struct Person
{
char name[50];
int age;
float salary;
};
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;
}
Example 4: c++ struct
#include<iostream>
#include<string>
using namespace std;
struct student
{
char name [20];
int age;
float marks;
};
int main()
{
student s;
cout<<"enter student name : "<<endl;
cin>>s.name;
cout<<"enter age : "<<endl;
cin>>s.age;
cout<<"enter marks : "<<endl;
cin>>s.marks;
cout<<"***********************"<<endl;
cout<<"name : "<<s.name<<endl;
cout<<"age : "<<s.age<<endl;
cout<<"marks : "<<s.marks<<endl;
return 0;
}