Advanced ProgrammingC++ Program ElementsC++ Character SetLexical TokensIdentifiersAttributes of IdentifiersTypeScopeLinkagesStorage DurationStorage ClassQualifiersDerived Data TypesClassStructureUnionEnumerationReferenceReference and PointerObject StorageConstant DeclarationsConstant DeclarationsConstant Object and Pointers - ExampleDeclarationDefinitionDeclaration != ImplementationExamplesIncomplete DeclarationsParenthesis in DeclarationTypedefInterpretation of DeclarationComplex DeclarationsScope Resolution Operator (::)Scope Resolution Operator - ExampleAcknowledgementsAdvanced ProgrammingConstants, Declarations, and Definitions01/14/19 CSCI 265: Advanced Programming 2C++ Program Elements01/14/19 CSCI 265: Advanced Programming 3C++ Character Set•a -- z •A -- Z •0 -- 9 •, . ; : ? ! ' " ( ) [ ] •{ } < > | / \ ~ + - = # % & ^ *01/14/19 CSCI 265: Advanced Programming 4Lexical TokensKeywordsIdentifiersConstantsLiteral StringsOperatorsPunctuatorsSpecial Characters01/14/19 CSCI 265: Advanced Programming 5IdentifiersName to denote Object, Function, Tag, typedef, Label or MacroFirst character is alpha, followed by any number of alpha, numerals or an underscoreCase sensitiveReserved words cannot be used01/14/19 CSCI 265: Advanced Programming 6Attributes of Identifiers•Type •Visibility (Scope) •Uniqueness (Linkage) •Permanence (Duration) •Storage Class •Qualifier (Modifiability)01/14/19 CSCI 265: Advanced Programming 7Type•Fundamental -- int, char, float, double, long, short, signed, unsigned •Derived -- arrays, functions, pointers, references, classes, unions, structures, enumerations01/14/19 CSCI 265: Advanced Programming 8Scope•An Identifier can be Used within its Scope •File Scope -- Global •Class Scope -- Member of a Class •Function -- Labels •Local -- Inside a Block01/14/19 CSCI 265: Advanced Programming 9LinkagesBindings between identifiersExternal “extern” – not local to a fileInternallocal to a file – e.g., “static” No linkagee.g., classes, enumerations – must be unique01/14/19 CSCI 265: Advanced Programming 10Storage DurationStatic Storage assigned once before the program startup, initialized only once, exists throughout the programAutomaticStorage assigned in a block, their status is completely undefined after the completion of the block where they were defined01/14/19 CSCI 265: Advanced Programming 11Storage ClassAuto – default – automatic variablesExtern – external objectsRegister – internal auto variables stored in hardware registers – fast accessStatic – in a block, a file or a class01/14/19 CSCI 265: Advanced Programming 12QualifiersNo qualifier – default – alteration controlled by the programConstant – no alteration is allowedVolatile – alteration controlled by systemConstant and volatile01/14/19 CSCI 265: Advanced Programming 13Derived Data Types•Class •Structure •Union •Enumeration•Array•Function•Pointer•Reference01/14/19 CSCI 265: Advanced Programming 14Class•Collection of Homogeneous Objects •Information Hiding -- Data Members and Member Functions 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; }01/14/19 CSCI 265: Advanced Programming 15Structure•Collection of Objects a Having Meaningful Representation •A Class with ALL PUBLIC MEMBERS struct date{ int day; char *month; int year; }; main() { struct date today; //object today.day = 15; today.month = "May"; today.year = 1995; cout << "Date is: " << today.month << " " << today.day << " " << today.year << endl; }01/14/19 CSCI 265: Advanced Programming 16Union•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.01/14/19 CSCI 265: Advanced Programming 17Enumeration•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;} }01/14/19 CSCI 265: Advanced Programming 18ReferenceAn alias of an objectMust be initialized when definedNo operator acts on referenceValue 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 = 100;int &ri = i; //ri is a reference to Icout << “ri is: “ << ri << endl;ri = j; // i = jri++; //i = 101}This is the first new C++ capability. You can’t do this in C.01/14/19 CSCI 265: Advanced Programming 19Reference and PointerMain(){int i = 10, j = 100;int *ptr;ptr = &i; //ptr is pointing to i (*ptr)++; //i = 11ptr = &j; //ptr is pointing to j(*ptr)++; //j = 101}01/14/19 CSCI 265: Advanced Programming 20Object StoragePersistent – alive after the program terminationNon-persistent – alive during the program executionC++ allows only non-persistent objectsAutomatic variables are allocated staticallyDynamic allocation is achieved by using new and delete operators01/14/19 CSCI 265: Advanced Programming 21Constant 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 CONSTANT01/14/19 CSCI 265: Advanced Programming 22Constant 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!!01/14/19 CSCI 265: Advanced Programming
View Full Document