C++ Basic Concepts: Seong Jong Choi

  • Uploaded by: gopalkota
  • 0
  • 0
  • July 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View C++ Basic Concepts: Seong Jong Choi as PDF for free.

More details

  • Words: 1,869
  • Pages: 23
Copyright, 2000 © Multimedia Lab.,

C++ Basic Concepts Seong Jong Choi [email protected] Multimedia Lab. Dept. of Electrical and Computer Eng. University of Seoul Seoul, Korea

12/04/09

Seong Jong Choi

C++ Basic Concepts-1

1. Main Objectives • C++ Parsing 을 위한 기본 개념 소개 • Reference: from MSDN

12/04/09

Seong Jong Choi

C++ Basic Concepts-2

Parser • 문장의 x 형식 : 주어 , 동사 , 목적어 , 보어 (language construct)

• 문법 (grammar) 에 의한 분류 • 문법을 표현하기 위한 여러 개념

Compiler

Translation Unit

Scanner (Lexical Analysis)

Token

Parser (Syntactic analysis or parsing)

Language Construct

Code Generation

Object file

White Space

12/04/09

Seong Jong Choi

C++ Basic Concepts-3

C++ - object file translation • int myInt; //declare &define an integer

Compiler

int myInt;

Scanner (Lexical Analysis)

1. int (keyword) 2. myInt (identifier) 3. ; (punctuation)

Parser (Syntactic analysis or parsing)

Code Generation

Object file

delcare and define myInt as an integer

//declare & ...

12/04/09

1. int (type name) 2. myInt (variable) 3. ; (end of a statement)

Seong Jong Choi

C++ Basic Concepts-4

Basic Concepts • • • • • • • • •

12/04/09

Declaration Definition Lifetime Name Object Scope Storage class (Data) Type lvalue and rvalue

Seong Jong Choi

C++ Basic Concepts-5

Declaration & Definition // Declaration and definition demo // Declaration (not definition) of function sum (function prototype) int sum (int , int ); void main () { // declaration and definition of variables int a=10, b=20; int result; result = sum(a, b); } //Declaration and definition of function sum int sum (int x, int y) { int c; c = x + y; return c; } 12/04/09

Seong Jong Choi

C++ Basic Concepts-6

Declaration • Def) A declaration introduces names and their types into a program without necessarily defining an associated object or function. However, many declarations serve as definitions.

12/04/09

Seong Jong Choi

C++ Basic Concepts-7

Definition • Def) A definition provides information that allows the compiler to allocate memory for objects or generate code for functions.

12/04/09

Seong Jong Choi

C++ Basic Concepts-8

Lifetime • Def) The lifetime of an object is the period during which an object exists, including its creation and destruction.

12/04/09

Seong Jong Choi

C++ Basic Concepts-9

Name • Def) A name denotes an object, function, set of overloaded functions, enumerator, type, class member, template, value, or label. • C++ programs use names to refer to their associated language element. • Names can be type names or identifiers.

12/04/09

Seong Jong Choi

C++ Basic Concepts-10

Object and Variable • Def) An object is an instance (a data item) of a user-defined type (a class type). • The difference between an object and a variable: – variables retain state information – objects can also have behavior.

• Note: – object means instance of a user-defined type – variable means instance of a fundamental type.

12/04/09

Seong Jong Choi

C++ Basic Concepts-11

Scope • Def) Names can be used only within specific regions of program text. These regions are called the scope of the name. – – – – –

12/04/09

Local scope function scope file scope class scope prototype scope

Seong Jong Choi

C++ Basic Concepts-12

Scope • Local scope

– A name declared within a block is accessible only within that block and blocks enclosed by it, and only after the point of declaration.

{

}

int a; …

– The names of formal arguments to a function in the scope of the outermost block of the function have local scope.

int func(int a) { … }

12/04/09

Seong Jong Choi

C++ Basic Concepts-13

Scope • Function scope

• Labels are the only names that have function scope

• File scope

– Any name declared outside all blocks or classes has file scope. It is accessible anywhere in the translation unit after its declaration. – Names with file scope that do not declare static objects are often called “global” names.

• Class scope.

– Names of class members have class scope.

• Prototype scope.

– Names declared in a function prototype are visible only until the end of the prototype.

12/04/09

Seong Jong Choi

C++ Basic Concepts-14

Scope • Hidden Names

– You can hide a name by declaring it in an enclosed block. – You can hide names with file scope by explicitly declaring the same name in block scope. However, file-scope names can be accessed using the scope-resolution operator (::).

#include int i = 7;      // i has file scope-declared outside all blocks void main( int argc, char *argv[] ) { int i = 5;  // i has block scope-hides the i with file scope cout << "Block-scoped i has the value: " << i << "\n"; cout << "File-scoped i has the value: " << ::i << "\n"; }

12/04/09

Seong Jong Choi

C++ Basic Concepts-15

Storage Class • Def) The storage class of a named object determines its lifetime, initialization, and, in certain cases, its linkage. – – – –

12/04/09

automatic static register external

Seong Jong Choi

C++ Basic Concepts-16

Storage Class • Automatic – Automatic objects in a function are (automatically) created when it is called and (automatically) destroyed when it ends. – Default unless otherwise specified – Usually, use stack for the storage

• Static Objects – Static objects are created and initialized once and live until the program terminates – Global, namespace scope, declared with static

static int a; // static declaration – A global object or variable that is explicitly declared as static has internal linkage (file scope). 12/04/09

Seong Jong Choi

C++ Basic Concepts-17

(Data) Type • Definition: A type defined the proper use of a name or an expression.

12/04/09

Seong Jong Choi

C++ Basic Concepts-18

Type Hierachy

T y p e F u n d a m A r it h m F lo a t in g

B

e t ic

T y p e

e n t a l

T y p e

v o id

TI n y t pe eg r a l T y pA er r a y

o o l e a n C T hy ap re a c t e r I n T t ye pg e e r

12/04/09

D D

ir e c t ly

F u n c t io n

P

D

e r iv e d

o in t e r

R

e r iv e d

T y p e

e f e r e n c Ce

T y p e C

o m

p o s e d

o n s t a n tS t r u c t u r e U n io n

C

D

e r

la s s

T y p e

Seong Jong Choi

C++ Basic Concepts-19

Fundamental Types • Def) Fundamental types are built into the language (such as int, float, or double). • Instances of these fundamental types are often called “variables.”

T y p e F u n d a m A r i t h m F lo a t in g

B

e t ic

e n t a l

T y p e

TI n y t pe eg r a l

v o id

T y pA er r a y

o o l e a n C T h y ap r e a c t e r I n T t ye pg ee r

12/04/09

T y p e

D D

ir e c t ly

F u n c t io n

P

D

e r iv e d

o i n t e r

R

e r iv e d

T y p e

T y p e

C

e f e r e n c Ce

o n s t a n tS

o m

U

p o s e d

t r u c t u r e n io n

C

D

e r

la s s

T y p e

Seong Jong Choi

C++ Basic Concepts-20

Derived Types • Def) Directly derived types are new types derived from fundamental types. 

T y p e F u n d a m A r i t h m F lo a t in g

B

e t ic

e n t a l

T y p e

TI n y t pe eg r a l

v o id

T y pA er r a y

o o l e a n C T h y ap r e a c t e r I n T t ye pg ee r

12/04/09

T y p e

D D

ir e c t ly

F u n c t io n

P

D

e r iv e d

o i n t e r

R

e r iv e d

T y p e

T y p e

C

e f e r e n c Ce

o n s t a n tS

o m

U

p o s e d

t r u c t u r e n io n

C

D

e r

la s s

T y p e

Seong Jong Choi

C++ Basic Concepts-21

Composed Derivative Types • Def) Composed Derivative types are new types created by combining existing types. •  

T y p e F u n d a m A r i t h m F lo a t in g

B

e t ic

e n t a l

T y p e

TI n y t pe eg r a l

v o id

T y pA er r a y

o o l e a n C T h y ap r e a c t e r I n T t ye pg ee r

12/04/09

T y p e

D D

ir e c t ly

F u n c t io n

P

D

e r iv e d

o i n t e r

R

e r iv e d

T y p e

T y p e

C

e f e r e n c Ce

o n s t a n tS

o m

U

p o s e d

t r u c t u r e n io n

C

D

e r

la s s

T y p e

Seong Jong Choi

C++ Basic Concepts-22

lvalue and rvalue • There are two values associated with a variable. – Its data value = variable’s rvalue – Its address value = variable’s lvalue

• Example:

a = a – 1; // 우변의 a 는 a 의 데이터값 (10), 좌변의 a 는 a 의 주소값 i = 7;            // Correct. A variable name, i, is an l-value. 7 = i;            // Error. A constant, 7, is an r-value. j * 4 = 7;        // Error. The expression j * 4 yields an r-value. *p = i;           // Correct. A dereferenced pointer is an l-value. const int ci = 7; // Declare a const variable. ci = 9;            // ci is a nonmodifiable l-value, so the assignment causes an // error message to be generated. ((i < 3) ? i : j) = 7; // Correct. Conditional operator (? :) returns an lvalue.

12/04/09

Seong Jong Choi

C++ Basic Concepts-23

Related Documents

Basic Concepts
May 2020 19
Basic Concepts
May 2020 17
Basic Concepts
May 2020 22
Basic Concepts
June 2020 20
Basic Concepts
December 2019 30

More Documents from "puneet"

C++ Mini-course
July 2020 10
C++ Review
July 2020 8
Introduction To Unix
July 2020 13