Escolar Documentos
Profissional Documentos
Cultura Documentos
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 을 위한 기본 개념 소개
C++ Basic
11/02/21 Seong Jong Choi
Concepts-2
Parser
• 문장의 x 형식 :
주어 , 동사 , 목적어 , 보어 (language construct)
• 문법 (grammar) 에 의한 분류
• 문법을 표현하기 위한 여러 개념
Compiler
White
Space
C++ Basic
11/02/21 Seong Jong Choi
Concepts-3
C++ - object file translation
Compiler
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
C++ Basic
11/02/21 Seong Jong Choi
Concepts-6
Declaration
C++ Basic
11/02/21 Seong Jong Choi
Concepts-7
Definition
C++ Basic
11/02/21 Seong Jong Choi
Concepts-8
Lifetime
C++ Basic
11/02/21 Seong Jong Choi
Concepts-9
Name
C++ Basic
11/02/21 Seong Jong Choi
Concepts-10
Object and Variable
• 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
– 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;
…
}
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
– 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
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
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
Type
F u n d a m e n ta l T y p e D e riv e d T y p e
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
Type
F u n d a m e n ta l T y p e D e riv e d T y p e
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
Type
F u n d a m e n ta l T y p e D e riv e d T y p e
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
• 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