The document discusses different data types and variables including primitive data types like integers and characters, programmer defined types like enumerated types and arrays, and references like pointers. It provides examples of variable declaration and use in languages like C++ and Java, and covers topics like static and dynamic binding, strong typing, and data conversion. Programmer defined types allow grouping of data into records, unions, enumerated types, and arrays which can be used to build more complex data structures like linked lists.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
260 views47 pages
PPL Unit 2 PDF
The document discusses different data types and variables including primitive data types like integers and characters, programmer defined types like enumerated types and arrays, and references like pointers. It provides examples of variable declaration and use in languages like C++ and Java, and covers topics like static and dynamic binding, strong typing, and data conversion. Programmer defined types allow grouping of data into records, unions, enumerated types, and arrays which can be used to build more complex data structures like linked lists.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47
Data Type and Variable
Data Type and Variable
2.1) Primitive Data Type Data Type and Variable 2.1.1) Integer Data Type and Variable 2.1.2) Real Number Data Type and Variable 2.1.3) Boolean Data Type and Variable 2.1.4) Character Data Type and Variable 2.1.5) String Data Type and Variable 2.2) Variable Data Type and Variable 2.2.1) Variable Naming Data Type and Variable 2.2.2) Reserve Words Data Type and Variable 2.2.3) Variable Declaration Data Type and Variable 2.2.3) Variable Declaration (Example) Data Type and Variable 2.2.4) Binding of Attribute to variable Static and Dynamic Binding • Association of a ‘function definition’ to a ‘function call’ or an association of a ‘value’ to a ‘variable’, is called ‘binding’. • During compilation, every ‘function definition’ is given a memory address; as soon as function calling is done, control of program execution moves to that memory address and get the function code stored at that location executed, this is binding of ‘function call’ to ‘function definition’. • Binding can be classified as ‘static binding’ and ‘dynamic binding’. If it’s already known before runtime, which function will be invoked or what value is allotted to a variable, then it is a ‘static binding’, and if it comes to know at the run time then it is called ‘dynamic binding’. Static and Dynamic Binding BASIS FOR COMPARISON STATIC BINDING DYNAMIC BINDING Event Occurrence Events occur at compile time Events occur at run time are are "Static Binding". "Dynamic Binding".
Information All information needed to call All information need to call a
a function is known at function come to know at run compile time. time. Advantage Efficiency. Flexibility. Time Fast execution. Slow execution. Alternate name Early Binding. Late Binding. Example overloaded function call, Virtual function in C++, overloaded operators. overridden methods in java. Static Binding (C++ example) class overload{ }; int a, b; int main(){ public: overload O1; int load(int x){ // first load() function. O1.load(20); //This statement binds the a=x; calling of function to 'first' load() function. return a; O1.load(20,40); //This statement binds the } calling of function 'second' load() function. int load(int x, int y){ //second load() } function. a=x; b=y; return a*b; } Dynamic Binding (C++ example) class base{ }; public: int main() virtual void funct(){ // Virtual function. { cout<<"This is a base class's funct()"; base *p, b; } derived1 d1; }; derived2 d2; class derived1 : public base{ *p=&b; public: p->funct(); //The above statement decides which class's void funct(){ //overridden virtual function. function is to be invoked. cout<<"This is a derived1 class's funct()"; *p=&d1; // Vlaue of the pointer changes. } p->funct(); //The above statement decides which class's }; function is to be invoked. class derived2 : public base{ *p=&d2; // Again vlaue of the pointer changes. public: p->funct(); //The above statement decides which class's function is to be invoked. void funct(){ //overridden virtual function. return 0; cout<<"This is a derived2 class's funct()"; } } Static Binding (java example) class Human{ /* Reference is of HUman type and object is public static void walk() * of Human type. { */ System.out.println("Human walks"); Human obj2 = new Human(); } obj.walk(); } obj2.walk(); class Boy extends Human{ } public static void walk(){ } System.out.println("Boy walks"); } public static void main( String args[]) { /* Reference is of Human type and object is Output * Boy type */ Human walks Human obj = new Boy(); Human walks Dynamic Binding (Java example) class Human{ Human obj = new Demo(); //Overridden Method /* Reference is of HUman type and object is public void walk() * of Human type. { */ System.out.println("Human walks"); Human obj2 = new Human(); } obj.walk(); } obj2.walk(); class Demo extends Human{ } //Overriding Method } public void walk(){ System.out.println("Boy walks"); Output: } public static void main( String args[]) { Boy walks /* Reference is of Human type and object is Human walks * Boy type */ Data Type and Variable 2.2.5) Strong Typing Data Type and Variable 2.2.6) Data Conversion Data Type and Variable 2.2.7) Variable Scope Data Type and Variable 2.2.8) Program Blocks Data Type and Variable 2.2.9) Named Constants Data Type and Variable 2.2.9) Named Constants (example) Data Type and Variable 2.3) Programmer Defined Data Type Data Type and Variable 2.3.1) Enumerated Types Data Type and Variable 2.3.1) Enumerated Types (Example) Data Type and Variable 2.3.2) Sub Range Types Data Type and Variable 2.3.3) Arrays Data Type and Variable 2.3.3) Arrays (continued) Data Type and Variable 2.3.3) Arrays (in Pascal ) Data Type and Variable 2.3.3) Arrays (in Java ) Data Type and Variable 2.3.3) Arrays (in C++ ) Data Type and Variable 2.3.4) Records Data Type and Variable 2.3.4) Records (in C++) Data Type and Variable 2.3.4) Records (in C++)
Typedef StudentRecord StudentList[20];
……… StudentList ThisList; 2.3.4) Records (in Java) Data Type and Variable 2.3.4) Records (in Java) StudentRecord[] ThisList; …. ThisList = new StudentRecord[20]; Data Type and Variable 2.3.5) Unions Data Type and Variable 2.3.6) Pointers Data Type and Variable 2.3.6) Pointers
C++ Definition of a pinter and construction of link list
Data Type and Variable 2.3.6) Pointers Data Type and Variable 2.3.6) Pointers
Java Definition of a pinter and construction of link list