Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Advanced Programming Constants, Declarations, and Definitions Derived Data Types."— Presentation transcript:

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

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

3 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; }

4 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.)

5 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.

6 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;} }

7 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; };

8 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.

9 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

10 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!!

11 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

12 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

13 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); }

14 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

15 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

16 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 */

17 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; }

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


Download ppt "Advanced Programming Constants, Declarations, and Definitions Derived Data Types."

Similar presentations


Ads by Google