cpp how to implement methods of a struct code 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: 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;
}