슬라이드 1 - Snu

Lecture 1 C++ Basics I Seoul National University Graphics & Media Lab Contents compile & execute (1.1) simple C++ program (1.2)

comment (1.3) types, variables (2.1-2.8, 5.8, 7.5) scope of variables (2.3.6) const qualifier (2.4) basic expressions (5.1, 5.2, 5.4, 5.5, 5.7, 5.9, 5.11) basic statements (6.1-6.11) class (1.5) Compile & Execute Write or edit source code Compile source code Link object code Run program Fix bugs that emerge during

compilation Fix bugs that emerge during execution A Simple C++ Program #include void main() { std::cout << "Hello, World" << std::endl; } Putting Comments Comments are ignored by the compiler. Comments can help readers understand the code.

// /* */ #include void main() { int u = 1, v = 2; std::cout << u + v; // sum of u and v /* sum of u and v */ } Types, Variables Primitive built-in types void

bool, char, w_char, short, int, long, float, double unsigned char, unsigned int, #include void main() { int height = 11, width = 9, length = 40; int result = height * width * length; std::cout << "The volume of the box car is "; std::cout << result << std::endl; } Types, Variables Enumerations Enumerations provide an alternative method for defining/gro uping sets of integer type constants. #include

void main() { enum Forms { shape = 1, sphere, cylinder, polygon }; std::cout << shape << sphere << cylinder << polygon; } Types, Variables typedef typedef allows us to define a synonym for a type #include typedef double wages; // wages is double typedef int exam_score; // exam_score is int typedef wages salary; // salary is wages (double) void main() { wages wage0 = 200, wage1 = 300; exam_score score0 = 90, score1 = 100;

std::cout << wage0 << score0; std::cout << wage1 << score1; } Types, Variables sizeof The sizeof operator returns the size (in bytes) of a type or an ob ject. #include typedef double wages; // wages is double typedef int exam_score; // exam_score is int void main() { wages w; std::cout << sizeof(int) << sizeof(exam_score); std::cout << sizeof(double) << sizeof(wages); std::cout << sizeof(w);

} Scope of Variables Local and global variables #include int a = 3; Global variable void main() { int b = 5; { int c = 7; Scope of cout << a << b << c;

variable c } cout << a << b; cout << c; // Compilation Error ! } Scope of Variables extern We can declare a global variable without defining it by using th e extern keyword. main.cpp tmp.cpp #include int a = 3;

extern int a; declaration of a void main() { std::cout << a << std::endl; } definition of a Scope of Variables static global variable We can define a global variable as static to make its scope loc al to a file. main.cpp

tmp.cpp #include static int a = 5; static int f() {} void main() { std::cout << a << std::endl; } static int a = 3; static int f() { } different definitions of variable a Scope of Variables

local static variable Local static variables of a function are kept intact when the fun ction returns. #include void func() { static int a = 0; a++; std::cout << a << " "; } void main() { for(int i=0;i<10;++i) func(); std::cout << std::endl; } Local Static Variable Const Qualifier

const A constant is a special kind of variable whose value cannot be altered in the program. #include void main() { int a = 3; const int b = 5; a = 7; b = 7; } // b is const variable // Compilation Error ! Basic Expressions

Arithmetic expressions +, -, *, /, % #include void main() { std::cout << 6 + 3 << 6 - 3 << 6 * 3 << 6 / 3; std::cout << 5 / 3 << 5 % 3 << 5.0 / 3.0; } Basic Expressions Numerical predicates ==, !=, >, <, >=, <= #include void main() {

int i = 50; double d = 50.0; std::cout << (i == (int)d); std::cout << ((double) i != d); } Basic Expressions Conditional operator cond ? expr1 : expr2; #include void main() { int score; std::cin >> score; std::cout << "The score is " << score << (score==1 ? " point" : " points") << "." << std::endl;

} Basic Expressions Memory management new, delete #include void main() { int * v = new int[10]; // allocate ten consecutive // integer variables delete v; } // de-allocate Basic Statements

Conditional statement if else, switch #include void main() { const int v = 5; if(v < 3) std::cout << "v is less than 3"; else if(v < 5) std::cout << "v is less than 5"; else if(v < 7) std::cout << "v is less than 7"; else std::cout << "v is larger than 7"; } Basic Statements

Conditional statement if else, switch #include void main() { const int v = 5; switch(v) case 3: case 5: case 7: default : } } { std::cout << std::cout << std::cout <<

std::cout "v "v "v << is is is "v 3"; break; 5"; break; 7"; break; is not 3 or 5 or 7"; Basic Statements

Loops for, while, do_while Problem Do summation from 1 to 10 #include void main() { int sum = 0; for(int i=1;i<=10;++i) sum += i; std::cout << sum; } Basic Statements

Loops for, while, do_while Problem Do summation from 1 to 10 #include void main() { int sum = 0, i = 1; while(i <= 10) { sum += i; i++; } std::cout << sum; }

Basic Statements Loops for, while, do_while Problem Do summation from 1 to 10 #include void main() { int sum = 0, i = 1; do { sum += i; i++; } while(i <= 10); std::cout << sum;

} Class A class consists of the datafields and interface. #include class Box { public: void print() { std::cout << height << " " << width << " " << length << std::endl; } double height, width, length; }; void main() { Box box; box.height = 3; box.width = 5; box.length = 7; box.print();

}

Recently Viewed Presentations

  • Common Endocrine Disorders in Neonates Alaa Al Nofal,

    Common Endocrine Disorders in Neonates Alaa Al Nofal,

    Heart murmur . Cleft palate . Which of the following lab tests is not seen in infants with central hypothyroidism . Normal TSH. Normal FT4. Low TSH . Low FT4. Low FT3. Pituitary Gland. Thyroid Gland. TSH + Thyroid Hormone...
  • Equine Education For Animal Control Officers and First

    Equine Education For Animal Control Officers and First

    The equine digestive system. Basic diet requirements. Types and quality of forage. Grains and commercial concentrates. ... HANDS ON!! Lecture will include hands-on look at different types of hay and feed - both good and not-so-good. Tour of host facility...
  • #ISUCIT Welcome to CIT 2016 Session: Personally Identifiable

    #ISUCIT Welcome to CIT 2016 Session: Personally Identifiable

    An Important Point "A common misconception is that PII only includes data that can be used to directly identify or contact an individual (e.g., name, e-mail address), or personal data that is especially sensitive (e.g., Social Security number, bank account...
  • Real-Time Advanced Process Control for GaN MOCVD Processing

    Real-Time Advanced Process Control for GaN MOCVD Processing

    Real-Time Advanced Process Control for GaN MOCVD Rubloff Research Group Accomplishments Real-Time APC for GaN MOCVD Processing Accomplishment Mass spectrometric sampling of reaction products provides real-time measurement of layer growth rates and thickness Real-time end point control demonstrated at 1-2%...
  • JSM / Julia Arbitration

    JSM / Julia Arbitration

    Generally won't be enforced if evidence the clause isn't true - i.e. there is a prior agreement not expressed within the four corners of the agreement, the agreement was procured by fraud, etc. Practice Tip: Consider whether any prior related...
  • Presentación de PowerPoint - UNDERUCA

    Presentación de PowerPoint - UNDERUCA

    Gordon-Lintner "Bird-in-the-Hand Theory" C. Preferencias impositivas: los inversores prefieren altos crecimientos (capital gains) a recibir dividendos gravados. Teoría de la irrelevancia de dividendos Franco Modigliani y Merton Miller La política de dividendos no afecta el valor de la empresa Si...
  • Topological explanations, robustness and the ...

    Topological explanations, robustness and the ...

    The explanandum of a mechanistic explanation is determined by stating the "set-up" and "termination" conditions. The chronology of sequences through which entities act is crucial to explain the production of this termination condition. Topological explanations
  • Presentazione standard di PowerPoint - ventilab

    Presentazione standard di PowerPoint - ventilab

    Test blu di metilene Preparazione del test: utilizzare spazzolino/spugnetta per igiene orale, immergerla in 5 ml (1/2 fiala) di blu di metilene e strizzare fino a che non goccioli Esecuzione: con spazzolino/spugnetta sporcare: vestiboli e guance; se possibile (per apertura...