Breaking

C++ Structure and Enumerate

                                 C++ Structure

A structure is a grouping of several variables under one name and is sometimes called conglomerate data type. 
The structure is a collection of variables having different data types under a single name. It is also a collection of different type of data type.
Note-

Difference between C-style structures and C++ Structures:

C Style Structure
C++ style Structure


1.   struct <structure tag>
{
   /* member elements declaration:
};

2.   A C-style structure is a collection of a variable under one name.



1.   struct <structure tag>
{
[public:]  [private:] [protected:]

/* data members declaration*/
/*member functions declaration*/
}
2.   A C++ struct is similar to a class i.e., it can hold data members as well as member functions.

How to declare a C++ Structure?


A structure is like a class declared within keyword struct. The keyword struct tells to the compiler that a structure is being defined. A structure can not be inherited.

     
struct Date
{
    short day;
     short  month;
    short year;
};                                                                           

How to define a structure variable in C++?

To declare a structure variable having the data from as defined by Date. A compiler allocates memory to the structure when a structure variable is defined.


Date date;


This declares a structure variable date of type Date.
The complete c++ structure definition is as follows:  


struct Date
{
short day;
short month;
short year;
}
date;

How to access members of a structure in C++ structure?

The syntax for accessing a structure element:


Structure_name.element_name/structure_variable


Remember that the first component of an expression involving the dot(.) operator is the name of the specific structure variable (date), not the name of the structure specifier (Date).
so we can say that structure is access by the dot(.) operator.

If we want to access date by using structure variable/element name day and assign it Monday. So we can follow this bellow setps.

Example:-

date.day=Monday;
date.month=Sept;
date.day=18;

C++ Structure Example:-


#include<iostream.h>
#include<stdio.h>

struct Student
{
int roll no;
char name[20];
float marks[5];
char grade;
};

Student learner;       //structure variable declared

int main()
{
//Read in student data


cout<<”\n”<<”Enter Roll no:”;
cin>>learner.rollno;

cout<<”\n”<<”Enter Name:”;
gets(learner.name);

cout<<”\n”<<”Enter marks in 5 subject:”<<”\n”;
for(int i=0;i<5;i++)
{
cout<<’\n”<<”Subject”<<i+1<<”:”;
cin>>learn.marks[i];

}

//Determine Grade

float avg, total;
total =( learner.marks[0] + learner.marks[1] + learner.marks[2] + learner.marks[3] + learner.marks[4]);

avg =total/5;

if (avg<50)
{
 learner.grade = ‘F’;
}
else if
{
(avg<60) learner.grade = ‘C’;
}
else if  
{
(avg<80) learner.grade = ‘B’;
}
else
{
 learner.grade= ‘A’;
}

//Print Result

cout<<”\n”<<”\n”<<”Student Result:”<<”\n”;
cout<<”Roll no:”<<learner.rollno<<”\t”;
cout<<”Name”;
cout.write(learner.name,21);
cout<<”\n”<<<”Total Marks:”<<total;
cout<<”\t”<<”Grade:”<<learner.grade<<endl;
return 0;

}  //end of main()



Output:-

Enter Roll no: 20
Enter Name: Pawan
Enter marks in 5 subjects:

Subject 1: 98
Subject 2: 82
Subject 3: 72
Subject 4: 90
Subject 5: 99

Student Result:
Roll no:10         Name: Pawan
Total marks:  441     Grade: A

Enumerated Data Type in C++

  • Enum is a set of named integers constant.

  • Enum is a value type data type.

  • It is a user-defined data type

  • Enum can't be inherited.

  • It is declared by using the keyword enum.

 Syntax of Enumerate:-

enum <enumname>

{

variable/enum list

 

Example:-

#include <iostream>

enum Days{sunday=0, monday=1, tuesday=2, wednesday=3, thursday=4, friday=5, saturday=6};

int main()

{

Days D;

D=monday;

cout<<D;

return 0;

}

Output:-

2