16
1 TCP1231 Computer Programming I Lecture 7 User Data Type

Computer Programming- Lecture 7

Embed Size (px)

DESCRIPTION

User Data Type

Citation preview

Page 1: Computer Programming- Lecture 7

1TCP1231 Computer Programming I

Lecture 7User Data Type

Page 2: Computer Programming- Lecture 7

2TCP1231 Computer Programming I

• Defined constants (#define)

• Definition of own types (typedef)

• Constants (const)

• Enumerations (enum)

• Scope of variables (Global / Local)

• Structures (struct)

Outline

Page 3: Computer Programming- Lecture 7

3TCP1231 Computer Programming I

To understand how to create user-defined data type

Objectives

Page 4: Computer Programming- Lecture 7

4TCP1231 Computer Programming I

Defined constants (#define)symbolic constant

syntax :

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.

#define identifier replacement-text

#define MAX 5

int main() { cout<< MAX; ... for (i=0; i<MAX; i++) { ... } ...}

The constant is MAX (usually written in capital letters)

The text defined for the MAX is : 5

NO semicolon (;)

IT IS NOT RECOMMENDED TO USE define

Page 5: Computer Programming- Lecture 7

5TCP1231 Computer Programming I

Definition of own types (typedef)

syntax :

define a new user-defined data type

typedef int mytype;

int main (){ mytype a=2,b=3; cout << a << '\t' << b << endl; a=a + b; cout << a << '\t' << b << endl; a++; b--; cout << a << '\t' << b << endl; return 0;}

typedef type newtype

2 3

5 3

6 2

IT IS STRONGLY RECOMMENDED TO USE typedef

Page 6: Computer Programming- Lecture 7

6TCP1231 Computer Programming I

A constant is any expression that has a fixed value. They can be divided in Integer Numbers, Floating-Point Numbers, Characters and Strings

Constants (const)

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

int main (){ cout << width << tab << width / 2; return 0;}

100 50

With the const prefix you can declare constants with a specific type exactly as you would do with a variable

Page 7: Computer Programming- Lecture 7

7TCP1231 Computer Programming I

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:

Enumerations (enum)

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

Page 8: Computer Programming- Lecture 7

8TCP1231 Computer Programming I

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); switch(mont){ 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;}

Enter the number of the month==>4

This month is May

Page 9: Computer Programming- Lecture 7

9TCP1231 Computer Programming I

Scope of variables

int x;...

main(){int k;

......{

int k;......

}...

}

Global variable

Local variable

Local variable

Page 10: Computer Programming- Lecture 7

10TCP1231 Computer Programming I

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.

Scope of variables

int i=1, j=1;float f;

int main (){ int integer=3; float flt; cout << i << '\t' << j << endl; int j=3; cout << i << '\t' << j << endl; j++; i++; cout << i << '\t' << j << endl;}

1 11 32 4

Global variables can be referred to anywhere in the code, within any function, whenever it is after its declaration.

The scope of the local variables is limited to the code level in which they are declared. If they are declared at the beginning of a function (like in main) their scope is the whole main function. In the example, this means that if another function existed in addition to main(), the local variables declared in main could not be used in the other function and vice versa.

Page 11: Computer Programming- Lecture 7

11TCP1231 Computer Programming I

Scope example#include <iostream>#include <stdlib.h>

using namespace std;

main () { int j=4; cout<<"First list of i value==>"; for (int i=0;i<=5;i++) 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;}

First list of i values==>0 1 2 3 4 5Second list of i values==>10 9 8 7 6 5 4 3 2 1 0

Page 12: Computer Programming- Lecture 7

12TCP1231 Computer Programming I

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:

struct model_name { type1 element1; type2 element2; type3 element3;

. . . .

} object_name;

model_name is a name for the model of the structure type and the optional parameter.

object_name (optional) is a valid identifier (or identifiers) for structure object instantiations.

Within curly brackets { } they are the types and their sub-identifiers corresponding to the elements that compose the structure.

Page 13: Computer Programming- Lecture 7

13TCP1231 Computer Programming I

#include <iostream>#include <conio.h>

using namespace std;

struct student{ string name; int age; int ID; float avg;} info;

int main (){ student data; student test={"ABC", 19, 111, 5.5}; cout << "Enter a student ID ==> "; cin >> data.ID; cin.ignore();

cout << " the student name ==> "; getline(cin, data.name);

cout << "student average ==> "; cin >> data.avg;

cout << "\n------ From data -----------\n"; cout << data.ID << '\t' << data.avg << '\t' << data.name << endl; cout << "\n------ From info ------------\n"; info=data; cout << info.ID << '\t' << info.avg << '\t' << info.name << endl;

cout << "\n *****************************\n"; info.avg++; cout << "\n------ From data -----------\n"; cout << data.ID << '\t' << data.avg << '\t' << data.name << endl; cout << "\n------ From info ------------\n"; cout << info.ID << '\t' << info.avg << '\t' << info.name << endl;

cout << "\n*****************************\n"; cout << test.ID << '\t' << test.avg << '\t' << test.name << endl; getch(); return 0;}

A student has a name, age, ID, and average

Page 14: Computer Programming- Lecture 7

14TCP1231 Computer Programming I

Enter a student ID ==> 123Enter the student name ==> amerEnter 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

Page 15: Computer Programming- Lecture 7

15TCP1231 Computer Programming I

Examples

struct MyStructure1 { char c; int i;float f; double d;

}; int main() {

struct MyStructure1 s1, s2;s1.c = 'a';s1.i = 1; s1.f = 3.14; s1.d = 0.00093; s2.c = 'a'; s2.i = 1; s2.f = 3.14; s2.d = 0.00093;

}

typedef struct { char c; int i; float f; double d;

}MyStructure2; int main() {

MyStructure2 s1, s2;s1.c = 'a'; s1.i = 1; s1.f = 3.14; s1.d = 0.00093; s2.c = 'a'; s2.i = 1; s2.f = 3.14; s2.d = 0.00093;

}

Page 16: Computer Programming- Lecture 7

16TCP1231 Computer Programming I

The End