SlideShare une entreprise Scribd logo
1  sur  16
Lecture 7
         User Data Type



TCP1231 Computer Programming I   1
Outline
      • Defined constants (#define)
      • Definition of own types (typedef)
      • Constants (const)
      • Enumerations (enum)
      • Scope of variables (Global / Local)
      • Structures (struct)




TCP1231 Computer Programming I                2
Objectives



 To understand how to create user-
        defined data type




TCP1231 Computer Programming I   3
Defined constants (#define)
symbolic constant
syntax :            #define identifier replacement-text
This is a compiler directive used to define symbolic constant, it asks the compiler
to search the file for the identifier and replace the macro with the replacement text
specified.
                                           The constant is MAX (usually
#define MAX 5                              written in capital letters)

int main() {                                  NO semicolon (;)
 cout<< MAX;
 ...
                                             The text defined
 for (i=0; i<MAX; i++)
                                             for the MAX is : 5
   { ... }
 ...                            IT IS NOT RECOMMENDED
}                               TO USE define 4
       TCP1231 Computer Programming I
Definition of own types (typedef)

           syntax :     typedef type newtype

           define a new user-defined data type
typedef int mytype;

int main ()
{                                                2       3
   mytype a=2,b=3;
   cout << a << 't' << b << endl;               5       3
   a=a + b;
   cout << a << 't' << b << endl;
                                                 6       2
   a++;
   b--;
   cout << a << 't' << b << endl;        IT IS STRONGLY
                                          RECOMMENDED
    return 0;
}                                         TO USE typedef
      TCP1231 Computer Programming I                 5
Constants (const)
A constant is any expression that has a fixed value. They can be
divided in Integer Numbers, Floating-Point Numbers, Characters
and Strings

   const int width = 100;
   const char tab = 't';

   int main ()
   {
      cout << width << tab << width / 2;
                                           100   50
       return 0;
   }
 With the const prefix you can declare constants with a specific
 type exactly as you would do with a variable

    TCP1231 Computer Programming I                6
Enumerations (enum)
Enumerations (defines a set of constants) serve to create data
types to contain something different that is not limited to either
numerical or character constants nor to the constants true and
false. Its form is the following:
     enum model_name { value1, value2, . . . } ;

     enum colors {black, blue, green, red, yellow, white};
     int main ()
     {
        colors mycolor = blue;
        if (mycolor==black)
           cout <<"Yes";
         else
           cout <<"No";                                      No
     }
    TCP1231 Computer Programming I                7
Enumerations example
#include <iostream>
#include <stdlib.h>
using namespace std;
 typedef enum Month{January,February,March,April,May};
main () {
   Month mont;
   int n;
   cout<<"Enter the number of the month==>";
   cin>>n;
   mont =Month(n);                                       Enter the number of the month==>4
   switch(mont){
                                                         This month is May
   case 0: cout<<"this month is January";break;
   case 1: cout<<"this month is February";break;
   case 2: cout<<"this month is March";break;
   case 3: cout<<"this month is April";break;
   case 4: cout<<"this month is May";break;
   default: cout<<"this month is not in the list";
   }
   cout<<endl<<endl;
  system("pause");
            return 0;
}

        TCP1231 Computer Programming I                             8
Scope of variables

int x;                            Global variable
  ...
   main(){
        int k;
  ...
                                         Local variable
        ...
        {
              int k;
              ...
                                                Local
              ...                               variable
        }
        ...
  }



 TCP1231 Computer Programming I            9
Scope of variables
    All the variables that we are going to use must be previously declared. in C++
    we can declare variables anywhere in the source code, even between two
    executable sentences, and not only at the beginning of a block of instructions.

int i=1, j=1;                           Global variables can be referred to anywhere in the
float f;                                code, within any function, whenever it is after its
                                        declaration.
int main ()
{                                       The scope of the local variables is limited to the code
   int integer=3;                       level in which they are declared. If they are declared at
   float flt;                           the beginning of a function (like in main) their scope is
   cout << i << 't' << j << endl;      the whole main function. In the example, this means that
                                        if another function existed in addition to main(), the local
    int j=3;                            variables declared in main could not be used in the other
                                        function and vice versa.
    cout << i << 't' << j << endl;
    j++;                                  1        1
    i++;                                  1        3
    cout << i << 't' << j << endl;       2        4
}
          TCP1231 Computer Programming I                              10
Scope example
#include <iostream>
#include <stdlib.h>

using namespace std;

main () {

    int j=4;
    cout<<"First list of i value==>";           First list of i values==>0 1 2 3 4 5
    for (int i=0;i<=5;i++)                      Second list of i values==>10 9 8 7 6 5 4 3 2 1 0
         cout<<i<<" ";
     cout<<endl;

     cout<<"Second list of i values==>";
     {
     int j=10;
     for(int i=j;i>=0;i--)
       cout<<i<<" ";
     }

      cout<<endl<<endl;
    system("pause");
              return 0;
}


          TCP1231 Computer Programming I                                    11
Structure (struct)
 • A structure can be viewed as an object.
 • A structure is a set of diverse types of data that may have
   different lengths grouped together under a unique declaration.
   Its form is the following:
                           model_name is a name for the model of the
struct model_name {         structure type and the optional parameter.
  type1 element1;
  type2 element2;          object_name (optional) is a valid identifier (or
  type3 element3;            identifiers) for structure object instantiations.
        ..                 Within curly brackets { } they are the types
       ..                   and their sub-identifiers corresponding to the
} object_name;              elements that compose the structure.


    TCP1231 Computer Programming I                     12
#include <iostream>
#include <conio.h>                          A student has a name, age,
using namespace std;                        ID, and average
struct student{                         cout << "n------ From data -----------n";
   string name;                         cout << data.ID << 't' << data.avg << 't' << data.name << endl;
   int age;
   int ID;                              cout << "n------ From info ------------n";
   float avg;                           info=data;
} info;                                 cout << info.ID << 't' << info.avg << 't' << info.name << endl;

int main ()                             cout << "n *****************************n";
{                                       info.avg++;
  student data;
  student test={"ABC", 19, 111, 5.5};   cout << "n------ From data -----------n";
   cout << "Enter a student ID ==> ";   cout << data.ID << 't' << data.avg << 't' << data.name << endl;

  cin >> data.ID;                       cout << "n------ From info ------------n";
  cin.ignore();                         cout << info.ID << 't' << info.avg << 't' << info.name << endl;

  cout << " the student name ==> ";     cout << "n*****************************n";
  getline(cin, data.name);              cout << test.ID << 't' << test.avg << 't' << test.name << endl;
                                        getch();
  cout << "student average ==> ";       return 0;
  cin >> data.avg;             }
           TCP1231 Computer Programming I                                    13
Enter a student ID ==> 123
  Enter the student name ==> amer
  Enter the student average ==> 2.2

  ------ From data -----------
  123 2.2 amer

  ------ From info ------------
  123 2.2 amer

  *****************************

  ------ From data -----------
  123 2.2 amer

  ------ From info ------------
  123 3.2 amer

  *****************************
  111 5.5 ABC
TCP1231 Computer Programming I        14
Examples
struct MyStructure1 {                  typedef struct {
         char c;                                char c;
         int i;                                 int i;
         float f;                               float f;
         double d;                              double d;
};                                     }MyStructure2;
int main() {                           int main() {
         struct MyStructure1 s1, s2;            MyStructure2 s1, s2;
         s1.c = 'a';                            s1.c = 'a';
         s1.i = 1;                              s1.i = 1;
         s1.f = 3.14;                           s1.f = 3.14;
         s1.d = 0.00093;                        s1.d = 0.00093;
         s2.c = 'a';                            s2.c = 'a';
         s2.i = 1;                              s2.i = 1;
         s2.f = 3.14;                           s2.f = 3.14;
         s2.d = 0.00093;                        s2.d = 0.00093;
}                                      }
     TCP1231 Computer Programming I                  15
The End




TCP1231 Computer Programming I   16

Contenu connexe

Tendances

C aptitude.2doc
C aptitude.2docC aptitude.2doc
C aptitude.2doc
Srikanth
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6
rohassanie
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
TAlha MAlik
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
rohassanie
 
2.overview of c++ ________lecture2
2.overview of c++  ________lecture22.overview of c++  ________lecture2
2.overview of c++ ________lecture2
Warui Maina
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3
rohassanie
 
User defined functions
User defined functionsUser defined functions
User defined functions
shubham_jangid
 
FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3
rohassanie
 

Tendances (20)

Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Advanced C - Part 2
 
Captitude 2doc-100627004318-phpapp01
Captitude 2doc-100627004318-phpapp01Captitude 2doc-100627004318-phpapp01
Captitude 2doc-100627004318-phpapp01
 
C aptitude.2doc
C aptitude.2docC aptitude.2doc
C aptitude.2doc
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
 
Functions
FunctionsFunctions
Functions
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
2.overview of c++ ________lecture2
2.overview of c++  ________lecture22.overview of c++  ________lecture2
2.overview of c++ ________lecture2
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3
 
User defined functions
User defined functionsUser defined functions
User defined functions
 
computer science sample papers 2
computer science sample papers 2computer science sample papers 2
computer science sample papers 2
 
FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3
 
CBSE Grade12, Computer Science, Sample Question Paper
CBSE Grade12, Computer Science, Sample Question PaperCBSE Grade12, Computer Science, Sample Question Paper
CBSE Grade12, Computer Science, Sample Question Paper
 
C Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory managementC Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory management
 
Ch7 C++
Ch7 C++Ch7 C++
Ch7 C++
 
Programming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture NotesProgramming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture Notes
 
C++ Chapter I
C++ Chapter IC++ Chapter I
C++ Chapter I
 
Cbse marking scheme 2006 2011
Cbse marking scheme 2006  2011Cbse marking scheme 2006  2011
Cbse marking scheme 2006 2011
 

Similaire à Computer Programming- Lecture 7

C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
ssuser3cbb4c
 

Similaire à Computer Programming- Lecture 7 (20)

Lecture no 3
Lecture no 3Lecture no 3
Lecture no 3
 
CP 04.pptx
CP 04.pptxCP 04.pptx
CP 04.pptx
 
Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented Technologies
 
Lecture 9_Classes.pptx
Lecture 9_Classes.pptxLecture 9_Classes.pptx
Lecture 9_Classes.pptx
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
Data types in c++
Data types in c++ Data types in c++
Data types in c++
 
C++11 & C++14
C++11 & C++14C++11 & C++14
C++11 & C++14
 
cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...
cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...
cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...
 
Functions
FunctionsFunctions
Functions
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaon
 
Pointers in c++ programming presentation
Pointers in c++ programming presentationPointers in c++ programming presentation
Pointers in c++ programming presentation
 
C++ file
C++ fileC++ file
C++ file
 
C++ file
C++ fileC++ file
C++ file
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Nitin Mishra 0301EC201039 Internship PPT.pptx
Nitin Mishra 0301EC201039 Internship PPT.pptxNitin Mishra 0301EC201039 Internship PPT.pptx
Nitin Mishra 0301EC201039 Internship PPT.pptx
 

Dernier

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Dernier (20)

Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 

Computer Programming- Lecture 7

  • 1. Lecture 7 User Data Type TCP1231 Computer Programming I 1
  • 2. Outline • Defined constants (#define) • Definition of own types (typedef) • Constants (const) • Enumerations (enum) • Scope of variables (Global / Local) • Structures (struct) TCP1231 Computer Programming I 2
  • 3. Objectives To understand how to create user- defined data type TCP1231 Computer Programming I 3
  • 4. Defined constants (#define) symbolic constant syntax : #define identifier replacement-text This is a compiler directive used to define symbolic constant, it asks the compiler to search the file for the identifier and replace the macro with the replacement text specified. The constant is MAX (usually #define MAX 5 written in capital letters) int main() { NO semicolon (;) cout<< MAX; ... The text defined for (i=0; i<MAX; i++) for the MAX is : 5 { ... } ... IT IS NOT RECOMMENDED } TO USE define 4 TCP1231 Computer Programming I
  • 5. Definition of own types (typedef) syntax : typedef type newtype define a new user-defined data type typedef int mytype; int main () { 2 3 mytype a=2,b=3; cout << a << 't' << b << endl; 5 3 a=a + b; cout << a << 't' << b << endl; 6 2 a++; b--; cout << a << 't' << b << endl; IT IS STRONGLY RECOMMENDED return 0; } TO USE typedef TCP1231 Computer Programming I 5
  • 6. Constants (const) A constant is any expression that has a fixed value. They can be divided in Integer Numbers, Floating-Point Numbers, Characters and Strings const int width = 100; const char tab = 't'; int main () { cout << width << tab << width / 2; 100 50 return 0; } With the const prefix you can declare constants with a specific type exactly as you would do with a variable TCP1231 Computer Programming I 6
  • 7. Enumerations (enum) Enumerations (defines a set of constants) serve to create data types to contain something different that is not limited to either numerical or character constants nor to the constants true and false. Its form is the following: enum model_name { value1, value2, . . . } ; enum colors {black, blue, green, red, yellow, white}; int main () { colors mycolor = blue; if (mycolor==black) cout <<"Yes"; else cout <<"No"; No } TCP1231 Computer Programming I 7
  • 8. Enumerations example #include <iostream> #include <stdlib.h> using namespace std; typedef enum Month{January,February,March,April,May}; main () { Month mont; int n; cout<<"Enter the number of the month==>"; cin>>n; mont =Month(n); Enter the number of the month==>4 switch(mont){ This month is May case 0: cout<<"this month is January";break; case 1: cout<<"this month is February";break; case 2: cout<<"this month is March";break; case 3: cout<<"this month is April";break; case 4: cout<<"this month is May";break; default: cout<<"this month is not in the list"; } cout<<endl<<endl; system("pause"); return 0; } TCP1231 Computer Programming I 8
  • 9. Scope of variables int x; Global variable ... main(){ int k; ... Local variable ... { int k; ... Local ... variable } ... } TCP1231 Computer Programming I 9
  • 10. Scope of variables All the variables that we are going to use must be previously declared. in C++ we can declare variables anywhere in the source code, even between two executable sentences, and not only at the beginning of a block of instructions. int i=1, j=1; Global variables can be referred to anywhere in the float f; code, within any function, whenever it is after its declaration. int main () { The scope of the local variables is limited to the code int integer=3; level in which they are declared. If they are declared at float flt; the beginning of a function (like in main) their scope is cout << i << 't' << j << endl; the whole main function. In the example, this means that if another function existed in addition to main(), the local int j=3; variables declared in main could not be used in the other function and vice versa. cout << i << 't' << j << endl; j++; 1 1 i++; 1 3 cout << i << 't' << j << endl; 2 4 } TCP1231 Computer Programming I 10
  • 11. Scope example #include <iostream> #include <stdlib.h> using namespace std; main () { int j=4; cout<<"First list of i value==>"; First list of i values==>0 1 2 3 4 5 for (int i=0;i<=5;i++) Second list of i values==>10 9 8 7 6 5 4 3 2 1 0 cout<<i<<" "; cout<<endl; cout<<"Second list of i values==>"; { int j=10; for(int i=j;i>=0;i--) cout<<i<<" "; } cout<<endl<<endl; system("pause"); return 0; } TCP1231 Computer Programming I 11
  • 12. Structure (struct) • A structure can be viewed as an object. • A structure is a set of diverse types of data that may have different lengths grouped together under a unique declaration. Its form is the following: model_name is a name for the model of the struct model_name { structure type and the optional parameter. type1 element1; type2 element2; object_name (optional) is a valid identifier (or type3 element3; identifiers) for structure object instantiations. .. Within curly brackets { } they are the types .. and their sub-identifiers corresponding to the } object_name; elements that compose the structure. TCP1231 Computer Programming I 12
  • 13. #include <iostream> #include <conio.h> A student has a name, age, using namespace std; ID, and average struct student{ cout << "n------ From data -----------n"; string name; cout << data.ID << 't' << data.avg << 't' << data.name << endl; int age; int ID; cout << "n------ From info ------------n"; float avg; info=data; } info; cout << info.ID << 't' << info.avg << 't' << info.name << endl; int main () cout << "n *****************************n"; { info.avg++; student data; student test={"ABC", 19, 111, 5.5}; cout << "n------ From data -----------n"; cout << "Enter a student ID ==> "; cout << data.ID << 't' << data.avg << 't' << data.name << endl; cin >> data.ID; cout << "n------ From info ------------n"; cin.ignore(); cout << info.ID << 't' << info.avg << 't' << info.name << endl; cout << " the student name ==> "; cout << "n*****************************n"; getline(cin, data.name); cout << test.ID << 't' << test.avg << 't' << test.name << endl; getch(); cout << "student average ==> "; return 0; cin >> data.avg; } TCP1231 Computer Programming I 13
  • 14. Enter a student ID ==> 123 Enter the student name ==> amer Enter the student average ==> 2.2 ------ From data ----------- 123 2.2 amer ------ From info ------------ 123 2.2 amer ***************************** ------ From data ----------- 123 2.2 amer ------ From info ------------ 123 3.2 amer ***************************** 111 5.5 ABC TCP1231 Computer Programming I 14
  • 15. Examples struct MyStructure1 { typedef struct { char c; char c; int i; int i; float f; float f; double d; double d; }; }MyStructure2; int main() { int main() { struct MyStructure1 s1, s2; MyStructure2 s1, s2; s1.c = 'a'; s1.c = 'a'; s1.i = 1; s1.i = 1; s1.f = 3.14; s1.f = 3.14; s1.d = 0.00093; s1.d = 0.00093; s2.c = 'a'; s2.c = 'a'; s2.i = 1; s2.i = 1; s2.f = 3.14; s2.f = 3.14; s2.d = 0.00093; s2.d = 0.00093; } } TCP1231 Computer Programming I 15
  • 16. The End TCP1231 Computer Programming I 16