Advanced Programming Constants, Declarations, and Definitions Derived Data Types.

Slides:



Advertisements
Similar presentations
Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
Advertisements

Programming Languages and Paradigms The C Programming Language.
Dynamic Allocation Eric Roberts CS 106B February 4, 2013.
Chapter 7: User-Defined Functions II
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
OBJECT ORIENTED PROGRAMMING Instructor: Rashi Garg Coordinator: Gaurav Saxena.
Informática II Prof. Dr. Gustavo Patiño MJ
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
True or false A variable of type char can hold the value 301. ( F )
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Classes: A Deeper Look Systems Programming.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
Data Types.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Review of C++ Programming Part II Sheng-Fang Huang.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
1 Chapter 9 Scope, Lifetime, and More on Functions.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
Learners Support Publications Classes and Objects.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type.
Structures and Unions in C Alan L. Cox
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Functions, Scope, and The Free Store Functions Functions must be declared by a function prototype before they are invoked, return_type Function_name(type,
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
C++ for Java Programmers Chapter 2. Fundamental Daty Types Timothy Budd.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
1 Chapter 9 Scope, Lifetime, and More on Functions.
Structs and Classes Structs A struct can be used to define a data structure type as follows: struct Complex { double real, imag;} // specifying a Complex.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Structure A collection of values (members) struct date{ int day; char month[10]; int year; }; Declare a structure variable struct date today; struct struct_name.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Lecture.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
C++ Lesson 1.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 9 Scope, Lifetime, and More on Functions
Advanced Programming Basics
Pointers, Dynamic Data, and Reference Types
Classes and Objects.
Seoul National University
Recitation Course 0603 Speaker: Liu Yu-Jiun.
CS410 – Software Engineering Lecture #5: C++ Basics III
Scope of Identifier The Scope of an identifier (or named constant) means the region of program where it is legal to use that.
C Language B. DHIVYA 17PCA140 II MCA.
Seoul National University
Classes Member Qualifiers
Presentation transcript:

Advanced Programming Constants, Declarations, and Definitions Derived Data Types

3/14/2016CSCI 240: Computing II 2 Derived Data Types Class Structure Union Enumeration Array Function Pointer Reference

3/14/2016CSCI 240: Computing II 3 Class Collection of Homogeneous Objects Information Hiding -- Data Members and Member Functions #include using std::cout; using std::endl; class student{ private: char* name; public: void add_name(char *ip1) { name = new char[10]; strcpy(name, ip1);} char* get_name() {return name;} }; main() { student s1; //object or instance s1.add_name("John"); cout << "Name: " << s1.get_name() << endl; }

3/14/2016CSCI 240: Computing II 4 Structure Collection of Objects a Having Meaningful Representation A Class with ALL PUBLIC MEMBERS struct date{ int day; char *month; int year; }; #include using std::cout; using std::endl; main() { struct date today; //object today.day = 15; today.month = "May"; today.year = 1995; cout << "Date is: " << today.month << " " << today.day << " " << today.year << endl; } (There is a strong correlation between classes and structures.)

3/14/2016CSCI 240: Computing II 5 Union Objects to Occupy Same Area of Storage Different Types at Different Times struct circle{ int radius; }; struct triangle{ int side1; int side2; int angle; }; struct rectangle{ int side1; int side2; }; union shape{ struct circle s1; struct triangle s2; struct rectangle s3; }; Using the correct name is critical. Sometimes you need to add a tag field to help keep track. It is important that the tag field be in the same location in every variant.

3/14/2016CSCI 240: Computing II 6 Enumeration Assigns Numerical Values to List of Identifiers enum binary {zero, one}; enum number {one = 1, two, three}; enum boolean {False, True}; main() { boolean x = False; if (x) {cout << "True!" << endl;} else {cout << "False!" << endl;} }

3/14/2016CSCI 240: Computing II 7 Reference An alias of an object Must be initialized when defined No operator acts on reference Value of a reference cannot be changed after initialization – it always refers to the object it was initialized to. (Compile time, not run time.) main(){ int i = 10, &j = i; j = 5; cout << i; } This is the first new C++ capability. You can’t do this in C. main() { double pay; give_raise(pay); give_raise(&pay); } void give_raise(double &pay) { pay = pay * 1.05; }; // C-style void give_raise(double *pay) { *pay = *pay * 1.05; };

3/14/2016CSCI 240: Computing II 8 Object Storage Persistent – alive after the program termination Non-persistent – alive during the program execution C++ allows only non-persistent objects Automatic variables are allocated and destroyed automatically Dynamic allocation is achieved by using new and delete operators.

3/14/2016CSCI 240: Computing II 9 Constant Declarations const Keyword Makes the Object Constant const as a Prefix in a Pointer Declaration MAKES THE OBJECT POINTED TO BE A CONSTANT AND NOT THE POINTER! Use of *const Makes the POINTER to be a CONSTANT

3/14/2016CSCI 240: Computing II 10 Constant Declarations const int x = 10; /* x is a Constant Object */ const int y[] = {1, 2, 3, 4}; // y is a Array of Constant Objects const char *ptr = "csci220"; /* ptr: Pointer to a CONST OBJECT ptr[0] = 'R'; //ERROR!!! ptr = "Class_Notes"; /* ptr CAN POINT TO ANOTHER CONSTANT OBJECT! */ char *const cptr = "C++"; // cptr is a CONSTANT POINTER cptr[0] = 'c'; //LEGAL cptr = "Assignment"; //ERROR!! cptr CANNOT POINT TO // ANOTHER CONSTANT OBJECT! const char* const dptr = "Simple_Language"; /* dptr is a CONSTANT POINTER pointing to a CONSTANT OBJECT */ dptr[0] = 's'; //ERROR!! dptr = "Difficult_Language"; //ERROR!!

3/14/2016CSCI 240: Computing II 11 Class Declaration Describes the form of an Object DOES NOT Reserve Any Storage Initialization is NOT Allowed Object Definition (Class Variable Declaration) Creates an Instance Reserves a Storage Initialization is Allowed All Objects MUST BE DEFINED BEFORE THEIR USE

3/14/2016CSCI 240: Computing II 12 Declaration != Implementation Function Without Body Contains extern Specifier and NO Initializer or Function Body Static Member in the Class Declaration Class Name Declaration typedef Declaration

3/14/2016CSCI 240: Computing II 13 Examples /* Definitions */ int i, j; //storage is reserved int k = 10; //initialization /* Declarations */ int my_function(); //function extern int x; //external variable struct S; //structure typedef int INT; //typedef /* Implementation */ int my_function() { int i = 100; return(i); }

3/14/2016CSCI 240: Computing II 14 Incomplete Declarations Dimension is Not Specified Class/Structure Body is Not Specified Completed By Subsequent Declaration struct S; //incomplete S *ps; //Acceptable S s1; //ERROR struct S { int x; char *ptr; }; // Complete Declaration S s2; //FINE int array[]; //incomplete int array[5]; //FINE

3/14/2016CSCI 240: Computing II 15 Typedef Defines an Alias for Previously Defined Data Type Does NOT Create a New Type Makes Programs Readable typedef int INT; //INT => int INT x; //x is an integer variable INT y[10]; //Array typedef int *INT_PTR; //Pointer to int INT_PTR ptr; //ptr is a pointer to int /* Unnamed Class or Struct in a "typedef" gets the typedef as its name */ typedef struct { int p; char *q; } S; //struct is called S S my_struct; //instance of S

3/14/2016CSCI 240: Computing II 16 Interpretation of Declaration Order of Evaluation Depends Upon the Precedence and Associativity int (*fun[])(); /* Explanation 1. () Alter the Order of Evaluation 2. [] has Highest Precedence => fun is an array of 3. *fun[] => Pointers to 4. () => Functions Returning (required) 5. int => Integers ==> fun is an array of pointers to functions returning integers */

3/14/2016CSCI 240: Computing II 17 Scope Resolution Operator (::) Can declare local and global variable of same name. In C, the local variable takes precedence over the global variable throughout its scope. In C++, the scope resolution operator is used to access the variable of the same name in an outer block. Example int i; main() { int i; i = 35; ::i = 34; cout << "Local i = " << i << endl; cout << "Global i = " << ::i << endl; }

3/14/2016CSCI 240: Computing II 18 Acknowledgements These slides were originally produced by Rajeev Raje, modified by Dale Roberts.