Você está na página 1de 23

Copyright, 2000 © Multimedia Lab.

C++ Basic Concepts

Seong Jong Choi


chois@mmlab.net

Multimedia Lab.
Dept. of Electrical and Computer Eng.
University of Seoul
Seoul, Korea

C++ Basic
11/02/21 Seong Jong Choi
Concepts-1
1. Main Objectives

• C++ Parsing 을 위한 기본 개념 소개

• Reference: from MSDN

C++ Basic
11/02/21 Seong Jong Choi
Concepts-2
Parser

• 문장의 x 형식 :
주어 , 동사 , 목적어 , 보어 (language construct)
• 문법 (grammar) 에 의한 분류
• 문법을 표현하기 위한 여러 개념
Compiler

Translation Language Object


Parser
Unit Scanner Token Construct file
(Syntactic analysis Code Generation
(Lexical Analysis)
or parsing)

White
Space

C++ Basic
11/02/21 Seong Jong Choi
Concepts-3
C++ - object file translation

• int myInt; //declare &define an integer<nl>

Compiler

1. int (type name)


1. int (keyword)
2. myInt (variable)
2. myInt (identifier)
3. ; (end of a Object
3. ; (punctuation) Parser
int myInt; Scanner statement) file
(Syntactic analysis Code Generation
(Lexical Analysis)
or parsing)
delcare and define
//declare & ... myInt as an integer

C++ Basic
11/02/21 Seong Jong Choi
Concepts-4
Basic Concepts

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

C++ Basic
11/02/21 Seong Jong Choi
Concepts-5
Declaration & Definition

// Declaration and definition demo


// Declaration (not definition) of function sum (function prot
otype)
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;
}

C++ Basic
11/02/21 Seong Jong Choi
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.

C++ Basic
11/02/21 Seong Jong Choi
Concepts-7
Definition

• Def) A definition provides information that allows


the compiler to allocate memory for objects or
generate code for functions.

C++ Basic
11/02/21 Seong Jong Choi
Concepts-8
Lifetime

• Def) The lifetime of an object is the period during


which an object exists, including its creation and
destruction.

C++ Basic
11/02/21 Seong Jong Choi
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.

C++ Basic
11/02/21 Seong Jong Choi
Concepts-10
Object and Variable

• Def) An object is an instance (a data item) of a us


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

C++ Basic
11/02/21 Seong Jong Choi
Concepts-11
Scope

• Def) Names can be used only within specific


regions of program text. These regions are called
the scope of the name.

– Local scope
– function scope
– file scope
– class scope
– prototype scope

C++ Basic
11/02/21 Seong Jong Choi
Concepts-12
Scope

• Local scope
– A name declared within a block is accessible only within t
hat block and blocks enclosed by it, and only after the po
int of declaration.
{
int a;

}

– The names of formal arguments to a function in the scop


e of the outermost block of the function have local scope
.
int func(int a) {

}

C++ Basic
11/02/21 Seong Jong Choi
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.

C++ Basic
11/02/21 Seong Jong Choi
Concepts-14
Scope

• Hidden Names
– You can hide a name by declaring it in an enclosed bl
ock.
– You can hide names with file scope by explicitly decl
aring the same name in block scope. However, file-s
cope names can be accessed using the scope-resolut
ion operator (::).
#include <iostream.h>
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";
}

C++ Basic
11/02/21 Seong Jong Choi
Concepts-15
Storage Class

• Def) The storage class of a named object


determines its lifetime, initialization, and, in
certain cases, its linkage.

– automatic
– static
– register
– external

C++ Basic
11/02/21 Seong Jong Choi
Concepts-16
Storage Class

• Automatic
– Automatic objects in a function are (automatically) creat
ed 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 u
ntil the program terminates
– Global, namespace scope, declared with static
static int a; // static declaration
– A global object or variable that is explicitly declared as s
tatic has internal linkage (file scope).

C++ Basic
11/02/21 Seong Jong Choi
Concepts-17
(Data) Type

• Definition: A type defined the proper use of a nam


e or an expression.

C++ Basic
11/02/21 Seong Jong Choi
Concepts-18
Type Hierachy

Type

F u n d a m e n ta l T y p e D e r iv e d T y p e

A rith m e tic T y p e v o id D ir e c t ly D e r iv e d T y p e C o m p o s e d D e r iv a tiv e T y p e

F lo a t i n g T y p e In te g ra l T y p e A rra y F u n c tio n P o in te r R e fe re n c e C o n s ta n t S tru c tu re C la s s


U n io n

B o o le a n T y p e C h a ra c te r T y p e In te g e r T y p e

C++ Basic
11/02/21 Seong Jong Choi
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 c


alled “variables.”

Type

F u n d a m e n ta l T y p e D e riv e d T y p e

A rith m e tic T y p e v o id D ir e c tly D e r iv e d T y p e C o m p o s e d D e riv a tiv e T y p e

F lo a t in g T y p e In te g ra l T y p e A rra y F u n c tio n P o in te r R e fe re n c e C o n s ta n t S tru c tu re C la s s


U n io n

B o o le a n T y p e C h a ra c te r T y p e In te g e r T y p e

C++ Basic
11/02/21 Seong Jong Choi
Concepts-20
Derived Types

• Def) Directly derived types are new types derived


from fundamental types. 

Type

F u n d a m e n ta l T y p e D e riv e d T y p e

A rith m e tic T y p e v o id D ir e c tly D e r iv e d T y p e C o m p o s e d D e riv a tiv e T y p e

F lo a t in g T y p e In te g ra l T y p e A rra y F u n c tio n P o in te r R e fe re n c e C o n s ta n t S tru c tu re C la s s


U n io n

B o o le a n T y p e C h a ra c te r T y p e In te g e r T y p e

C++ Basic
11/02/21 Seong Jong Choi
Concepts-21
Composed Derivative Types

• Def) Composed Derivative types are new types


created by combining existing types.
•  

Type

F u n d a m e n ta l T y p e D e riv e d T y p e

A rith m e tic T y p e v o id D ir e c tly D e r iv e d T y p e C o m p o s e d D e riv a tiv e T y p e

F lo a t in g T y p e In te g ra l T y p e A rra y F u n c tio n P o in te r R e fe re n c e C o n s ta n t S tru c tu re C la s s


U n io n

B o o le a n T y p e C h a ra c te r T y p e In te g e r T y p e

C++ Basic
11/02/21 Seong Jong Choi
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 lva
lue.

C++ Basic
11/02/21 Seong Jong Choi
Concepts-23

Você também pode gostar