1//Struct is a compound data type that contains different variables of different types.
2struct Student
3{
4 char stuName[30];
5 int stuRollNo;
6 int stuAge;
7};
8
1
2#include <bits/stdc++.h>
3#include <iostream>
4
5#define ll long long
6
7using namespace std;
8
9struct student{
10 int roll;
11 string name;
12 int age;
13
14 void studentDetails(){
15 cout<<"Name is "<<name<<" Age is "<<age<<" roll no is "<<roll<<endl;
16 }
17};
18
19
20int main(){
21
22 student sumant;
23 sumant.roll = 30;
24 sumant.name = "Sumant Tirkey";
25 sumant.age = 18;
26
27 sumant.studentDetails();
28 cout<<endl;
29
30 return 0;
31}
1
2struct Rectangle {
3 int width; // member variable
4 int height; // member variable
5
6 // C++ constructors
7 Rectangle()
8 {
9 width = 1;
10 height = 1;
11 }
12
13 Rectangle( int width_ )
14 {
15 width = width_;
16 height = width_ ;
17 }
18
19 Rectangle( int width_ , int height_ )
20 {
21 width = width_;
22 height = height_;
23 }
24 // ...
25};
26
27
1struct product {
2 int weight;
3 double price;
4} ;
5
6product apple;
7product banana, melon;
1#include<iostream>
2#include<string>
3using namespace std;
4struct student
5{
6 char name [20];
7int age;
8float marks;
9};
10int main()
11{
12student s;
13cout<<"enter student name : "<<endl;
14cin>>s.name;
15cout<<"enter age : "<<endl;
16cin>>s.age;
17cout<<"enter marks : "<<endl;
18cin>>s.marks;
19cout<<"***********************"<<endl;
20cout<<"name : "<<s.name<<endl;
21cout<<"age : "<<s.age<<endl;
22cout<<"marks : "<<s.marks<<endl;
23return 0;
24}