Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming In C++ Spring Semester 2013 Lecture 12 Programming In C++, Lecture 12 By Umer Rana.

Similar presentations


Presentation on theme: "Programming In C++ Spring Semester 2013 Lecture 12 Programming In C++, Lecture 12 By Umer Rana."— Presentation transcript:

1 Programming In C++ Spring Semester 2013 Lecture 12 Programming In C++, Lecture 12 By Umer Rana

2 Advanced Variables Programming In C++, Lecture 12 By Umer Rana Storage Classes Every C variable holds a characteristic called its Storage Class. The Storage class define two characteristics of variable: Lifetime Visibility Storage Class help us to write programs that use memory more efficiently, run faster and less prone to programming errors. Correct use of storage class is especially important in large programs.

3 Advanced Variables Programming In C++, Lecture 12 By Umer Rana Lifetime The lifetime of a variable is the length of time it retains a particular value. In terms of their lifetime variables can be divided into two categories. 1.Automatic 2.Static & External

4 Advanced Variables Programming In C++, Lecture 12 By Umer Rana Lifetime Automatic Automatic variables are written inside the braces that serve as delimiters for a function. They are created when the function containing them is called as destroyed when the function is terminates. Fun() { int delta; auto int beta; register int gamms; }

5 Advanced Variables Programming In C++, Lecture 12 By Umer Rana Lifetime Static Variables: A static variable is known only to the function in which it is defined but unlike automatic variables, it does not disappear when the function terminates. Instead it keeps its place in memory and therefore its value. Fun() { static int delta; }

6 Advanced Variables Programming In C++, Lecture 12 By Umer Rana Lifetime External Variables: Like static variables external variables exist for the life of the program. Variable of type Static & External exist for the life of a program. int zeta; main () { } Fun() { }

7 Advanced Variables Programming In C++, Lecture 12 By Umer Rana Visibility: Visibility of a variable refers to which parts of a program will be able to recognize it. Could be in block, a function, a file, a group of files or an entire program. Automatic & Static Variables Visibility: An automatic variable is only recognized within the function in which it is define therefore it is some time called local variables. Static variables are also visible only in the function where they are defined.

8 Advanced Variables Programming In C++, Lecture 12 By Umer Rana Visibility: External Variables Visibility: To Create a variable that is visible to more then one function must be declare externally out side of any function. int zeta; main () { } Fun() { }

9 Advanced Variables Programming In C++, Lecture 12 By Umer Rana Blocks: A Block is a section of code set off by braces. Visibility of automatic and static variables can be restricted inside a block. main() { int Var1; { int Var2; }

10 Advanced Variables Programming In C++, Lecture 12 By Umer Rana Register Variables Register variables are stored in the microprocessor’s registers instead of storing it in memory. Register can be accessed much faster than memory locations. Register class variables that are frequently accessed can result in significantly increasing a program’s speed. Drawbacks use of register variables, only few variables can be register variables in given program. Because it all depends on how many registers the program needs for other purposes.

11 Advanced Variables Programming In C++, Lecture 12 By Umer Rana Enumerated Data Type The enumerated (enum) type give us the opportunity to invent our own data type and specify what values it can take on. Enum birds { sparrow, robin, eagle, egret }; enum birds var1,var2;

12 Advanced Variables Programming In C++, Lecture 12 By Umer Rana Enumerated Data Type The compiler treats enumerated variables as integers. Each value on the list of permissible values corresponds to an integer. Starting with 0 to onward. thus sparrow is stored as 0, robin as 1, eagle as 2 and egret as 3.

13 Enumerated Data Type Programming In C++, Lecture 12 By Umer Rana int main () { enum departments{ management, research, clerical, sales }; struct{ char name[30]; float salary; enum departments dept; }employees; strcpy(employees.name,"David Ben"); employees.salary= ; employees.dept=sales; printf("Name: %s \n",employees.name); printf("Salary: %.2f \n",employees.salary); printf("Department: %d \n",employees.dept); }

14 Advanced Variables Programming In C++, Lecture 12 By Umer Rana Type Conversion: When data type are mixed in an expression the compiler converts the variables to compatible types before carrying out the intended operation. In these conversions variables of lower rank are converted to the rank of the higher-ranking operand. The ranking corresponds roughly to how many bytes each type occupies in memory.

15 Advanced Variables Programming In C++, Lecture 12 By Umer Rana Type Casting: Typecasting provides a way to force a variable to be of a particular type. This overrides the normal type conversions we just described. The mechanism for type casting is to precede the variable by the desired type. e.g int ans; float Var1=100.50; ans= (int) Var1;

16 Advanced Variables Programming In C++, Lecture 12 By Umer Rana Labels & goto Statement. A goto statement can cause program control to end up almost anywhere in the program. Label are used only with goto. The goto must specify a label and the label must exist. The Label must be followed by a colon, can be on separate line or on the same line as another statement.

17 Advanced Variables Programming In C++, Lecture 12 By Umer Rana Labels & goto Statement. int main () { int num; LabelP2:; printf("plese enter the lucky number:"); scanf("%d",&num); if (num==1) goto P1; else goto P2; Label P1:printf(" \n\n\n************ Good Luck ************"); getch(); }


Download ppt "Programming In C++ Spring Semester 2013 Lecture 12 Programming In C++, Lecture 12 By Umer Rana."

Similar presentations


Ads by Google