• Like
Computer Programming- Lecture 7
Upcoming SlideShare
Loading in...5
×

Computer Programming- Lecture 7

  • 6,033 views
Uploaded on

User Data Type

User Data Type

More in: Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
6,033
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
40
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Lecture 7 User Data TypeTCP1231 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 typeTCP1231 Computer Programming I 3
  • 4. Defined constants (#define)symbolic constantsyntax : #define identifier replacement-textThis is a compiler directive used to define symbolic constant, it asks the compilerto search the file for the identifier and replace the macro with the replacement textspecified. 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 typetypedef 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 bedivided in Integer Numbers, Floating-Point Numbers, Charactersand 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 datatypes to contain something different that is not limited to eithernumerical or character constants nor to the constants true andfalse. 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 variablesint 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 thefloat 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 thestruct 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 averagestruct 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 ABCTCP1231 Computer Programming I 14
  • 15. Examplesstruct 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 EndTCP1231 Computer Programming I 16