Sunday 25 March 2018

User Defined data types

Structure 
  Structure is a collection of variables of different data types grouped together under a single name.
         Each variable within a structure is called as member of structure.

Syntax of defining structure :-
     struct structure_name
      {
        Structure_member 1;
        Structure_member 2;
        ...............................
       ................................
       Structure_member n;
        }instances;

           OR

   struct structure_name
   {
      Structure_member 1;
      Structure_member 2;
        ...............................
       ................................
       Structure_member n;
        };
          struct structure_name instance;

Ex-
 struct student
  {
    char name[20];
    int rollno;
    float per;
   }stud1,stude2;
                   OR
  
 struct student
  {
    char name[20];
    int rollno;
    float per;
   } 
 struct student stud1,stud2;

Initialization of structure Variable:
   
An instance of a structure can be assigned values during declaration as follows.
        struct student
        {
          char name[20];
          int rollno;
         float per;
        }stud1={"irawen",101,78.12};

Accessing Structure Member:
   Individual members of structure can be used like other variables. Structure member can be assessed by dot operator(.).
 This dot operator (.) is used between the structure name and member name.
   Ex- stud1.name , stud1.rollno , stud1.per
values can also be assigned to structure member.
     stud1.name="irawen";
     stud1.rollno=101;
     stud1.per=78.12;

Question 1) Write a program to define structure 'Tender' having data member tender_no,cost and company_name.Accept and display data for one variable of the structure.

#include<conio.h>
#include<iostream.h>
struct Tender
{
  int tender_no;
  float cost;
  char company_name[20];
};
void main()
{
Tender t1;
clrscr();
cout<<"\tEnter Tender Number";
cin>>t1.tender_no;
cout<<"\nEnter Cost of Tender";
cin>>t1.cost;
cout<<"\nEnter Company Name";
cin>>t1.company_name;
cout<<"\n\n\tTender Number is →"<<t1.tender_no;
cout<<"\n\n\tTender cost is→"<<t1.cost;
cout<<"\n\n\tCompany Name is→"<<t1.company_name;
getch();
}

Output-
  Enter Tender Number101
  Enter Cost of Tender1234
  Enter Company NameIrawen.info
                          Tender Number is→101
                          Tender cost is→1234
                          Tender Name is→Irawen.info 

Union :
    Union are a concept borrowed from structures and therefore the same syntax as structure.
          But here is a major difference between them is in storage structures. In structure there is a separate storage memory location for each data member where all the member of union share the memory locations.
Ex-
  Union item
   {
     int a;
     float b;
     char c;
   } it;

Example Union-
#include<conio.h>
#include<stdio.h>
void main()
{
union item
 {
   int m;
   float p;
   char ch;
  }it;
clrscr();
it.m=12;
it.p=23.12;
it.ch='A';
printf("\n\n\tItem No is→%d",it.m);
printf("\n\n\tPrice is→%f",it.p);
printf("\n\n\tStarting character of item is→%c",it.ch);
getch();
}

Output-
    Item No is→2751
    Price is→23.119753
    Starting character of item is→A

Enumerated Data Types:-
   An enumerated data type is another user defined type which provides a way of attaching names to the members.
The enum keyword automatically enumerates the list of words by assigning values 0,1,2.....and so on.
    Syntax:   
          enum shape{circle.square,triangle};
          enum color{red,blue,green,yellow};
         :Declaring Variable of enum:
        Ex.shape ellipse;color background;
By default enum assigns integer values starting with '0' zero for the first enumerator,1 for the second and so on..
 We can over ride the default by just assigning integer values to enumerator.
  enum color{red,blue=4,green=3};
  enum color{red=5,blue,green};

#include<iostream.h>
#include<conio.h>
void main();
{
 enum color{red,blue,green};
 enum color1{red1,blue1=4,green1=6};
 cout<<"\n\n\tColor of Background-->"<<red;
 cout<<"\n\n\tColor of Background-->"<<blue;
  cout<<"\n\n\tColor of Background-->"<<green;
  cout<<"\n\n\tColor of Background-->"<<red1;
  cout<<"\n\n\tColor of Background-->"<<blue1;
  cout<<"\n\n\tColor of Background-->"<<green1;
  getch();
}

Output-
      Color of Background-->0
      Color of Background-->1
      Color of Background-->2
      Color of Background-->0
      Color of Background-->4
      Color of Background-->6

0 Comments:

Post a Comment

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (114) C (77) C# (12) C++ (82) Course (60) Coursera (176) coursewra (1) Cybersecurity (22) data management (11) Data Science (89) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (19) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (44) Meta (18) MICHIGAN (5) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (743) Python Coding Challenge (195) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses