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.

kasa
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 (인용,참조)

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

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

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

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

  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. 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. 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. 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. 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. 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. Acknowledgements(인지) • These slides were originally produced by Rajeev Raje, modified(변경) by Dale Roberts.

More Related