c ++ struct code example
Example 1: constructor c++ struct
struct Rectangle {
int width;
int height;
Rectangle()
{
width = 1;
height = 1;
}
Rectangle( int width_ )
{
width = width_;
height = width_ ;
}
Rectangle( int width_ , int height_ )
{
width = width_;
height = height_;
}
};
Example 2: 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;
}