1 / 17

Programming In C++

Programming In C++. Spring Semester 2013 Lecture 12. Advanced Variables. Storage Classes Every C variable holds a characteristic called its Storage Class. The Storage class define two characteristics of variable: Lifetime Visibility

kalea
Download Presentation

Programming In C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Advanced Variables 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. Programming In C++, Lecture 12 By Umer Rana

  3. Advanced Variables 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. Automatic Static & External Programming In C++, Lecture 12 By Umer Rana

  4. Advanced Variables 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; autoint beta; registerintgamms; } Programming In C++, Lecture 12 By Umer Rana

  5. Advanced Variables 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; } Programming In C++, Lecture 12 By Umer Rana

  6. Advanced Variables 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() { } Programming In C++, Lecture 12 By Umer Rana

  7. Advanced Variables 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. Programming In C++, Lecture 12 By Umer Rana

  8. Advanced Variables 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() { } Programming In C++, Lecture 12 By Umer Rana

  9. Advanced Variables 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; } } Programming In C++, Lecture 12 By Umer Rana

  10. Advanced Variables 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. Programming In C++, Lecture 12 By Umer Rana

  11. Advanced Variables 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; Programming In C++, Lecture 12 By Umer Rana

  12. Advanced Variables 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. Programming In C++, Lecture 12 By Umer Rana

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

  14. Advanced Variables 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. Programming In C++, Lecture 12 By Umer Rana

  15. Advanced Variables 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 intans; float Var1=100.50; ans = (int) Var1; Programming In C++, Lecture 12 By Umer Rana

  16. Advanced Variables 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. Programming In C++, Lecture 12 By Umer Rana

  17. Advanced Variables Labels & goto Statement. int main () { intnum; 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(); } Programming In C++, Lecture 12 By Umer Rana

More Related