1 / 18

Advanced Programming

Constants, Declarations, and Definitions. Advanced Programming. Derived Data Types. Derived Data Types. Class Structure Union Enumeration Array Function Pointer Reference . Class. Collection of Homogeneous Objects Information Hiding -- Data Members and Member Functions

yardley
Download Presentation

Advanced Programming

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. Constants, Declarations, and Definitions Advanced Programming Derived Data Types

  2. Derived Data Types • Class • Structure • Union • Enumeration • Array • Function • Pointer • Reference CSCI 240: Computing II

  3. Class • Collection of Homogeneous Objects • Information Hiding -- Data Members and Member Functions • #include <iostream>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; } CSCI 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 <iostream>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.) CSCI 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. CSCI 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;} } CSCI 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. CSCI 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. CSCI 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 CSCI 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!! CSCI 240: Computing II

  11. Declaration • Describes the form of an Object • DOES NOT Reserve Any Storage • Initialization is NOT Allowed Definition • Creates an Instance • Creates an Instance • Reserves a Storage • Initialization is Allowed • All Objects MUST BE DEFINED BEFORE THEIR USE CSCI 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 CSCI 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);            } CSCI 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 CSCI 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 CSCI 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 */ CSCI 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; } CSCI 240: Computing II

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

More Related