Download Derived Data Types in C++: Classes, Structures, Unions, Enumerations, and References and more Slides Computer Science in PDF only on Docsity!
Advanced
Programming
Constants, Declarations, and Definitions
Derived Data Types
Derived Data Types
- Class
- Structure
- Union
- Enumeration
- Array
- Function
- Pointer
- Reference
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.)
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.
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.
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.
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!!
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
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); }
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
Interpretation of Declaration
- Order of Evaluation Depends Upon the Precedence and Associativity
- int (*fun[])();
- /* Explanation
- () Alter the Order of Evaluation
- [] has Highest Precedence => fun is an array of
- *fun[] => Pointers to
- () => Functions Returning (required)
- int => Integers ==> fun is an array of pointers to functions returning integers */
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; }