• 검색 결과가 없습니다.

Ch. 2 Setting Out to C++ C++ Programming

N/A
N/A
Protected

Academic year: 2022

Share "Ch. 2 Setting Out to C++ C++ Programming"

Copied!
20
0
0

로드 중.... (전체 텍스트 보기)

전체 글

(1)

C++ Programming

Ch. 2 Setting Out to C++

Lecture Note of Digital Computer Concept and Practice

Spring 2014

Myung-Il Roh

Department of Naval Architecture and Ocean Engineering

(2)

Ch. 2 Setting Out to C++

(3)

Contents

þ Review

þ Structure of the C++ Program

þ The C++ Preprocessor and the ‘iostream’ File þ Input and Output Statements

þ Functions

þ C++ Coding Rules: Identifiers & Keywords þ Summary

þ Practice

(4)

Review

þ Programming Using Microsoft Visual C++

n Create a Project, Coding, Compile, Link, and Execute n “Hello World!” Program

(5)

#include <iostream>

using namespace std;

double stone2kg(double);

double main() {

double stone, kgs;

cout << “input weight in unit stone”;

cin >> stone;

kgs = stone2kg(stone);

cout << stone << “ Stone is ”;

cout << kgs << “kilos.\n”;

return 0;

}

double stone2kg(double sts) {

return 6.35 * sts;

Structure of C++ Program (1/3)

þ Example of C++ Program

(2) Function prototype and

declaration of global variables

(3) Function definition (header + body) (1) Preprocessor directive

Definition of a main function

Definition of a user-defined function

(6)

Structure of C++ Program (2/3)

þ Preprocessor Directive

n A part for defining preprocessor directives (e.g., #include)

n A preprocessor is a program that processes a source file before the main compilation takes place.

n A directive is a typical preprocessor action: adding or replacing text in the source code before it’s compiled.

n Ex. #include <iostream>

l This directive causes the preprocessor to add the contents of the ‘iostream’

file to your program.

n The preprocessor directive must start with the ‘#’.

þ Function Prototype and the Declaration of Global Variables

n A part for declaring functions or global variables to be used in the source code.

(7)

Structure of C++ Program (3/3)

þ Function Definition

n A part for defining a main function and user-defined functions n The ‘main()’ function must be exist one time in the program.

(8)

C++ Preprocessor and ‘iostream’ File

þ #include <iosream>

n A statement starting with ‘#’ is called ‘Preprocessor directive’.

l Example of preprocessors: file inclusion(#include), macro expansion(#define), and conditional compilation(#if, #ifdef).

l ‘iostream’: Defines input and output functions in C++.

– When using ‘cin’ and ‘cout’ for input and output, the ‘iostream’ file must be included.

– In C language ‘.h’ is used as extension for header files, but in C++ language, the extension can be omitted. For this, the statement of ‘using namespace std;’ is required.

n Format of the preprocessor directive

l #include <aaaaa> // For header files in C++ standard library l #include <aaaaa.h> // For header files in C standard library l #include <myheader.h> // For user-defined header files

(9)

Input and Output Statements

þ Output Statement (using ‘cout’ object)

n cout << output something;

n ‘cout’ judges the data type of a variable automatically.

n ‘cout’ can use continuously

l cout << “Wow, “ << 8 << “ dogs!”

v Result: Wow, 8 dogs!

n In C language, the ‘printf()’ function is used for output.

þ Input Statement (using ‘cin’ object)

n cin >> input something;

n ‘cin’ stores the input data on a designated variable, and judges the data type of the variable automatically.

l cin >> score; // For C user: Warning! it is not ‘&score’.

n In C language, the ‘scanf()’ function is used for input.

(10)

Functions (1/5)

þ Function

n A group of statements that perform a task together

l Standard library functions vs. User-defined functions

– Standard library functions: Basic functions given in C++ language – User-defined functions: Functions which are provided by the user.

l Depend on the return value

– Function having a return value

» Ex. The statement like “y = 3,0 * sqrt(2,0);” can be used in the source code.

– Function having no return value

» Ex. void type function

int main() {

x = sqrt(6.25);

}

float sqrt(float a) {

return a;

}

Calling function Called function

① ②

(11)

#include <iostream>

using namespace std;

// This program doesn’t have a function prototype and declaration of global variables.

int main( ) // Function header

{ // beginning of function body

cout << “print a message through the monitor \n”; // print

return 0; // end of main()

}

Functions (2/5)

New line

Preprocessor directive

Function definition

(12)

Functions (3/5)

þ Function Header

n It is a capsule summary of the function’s interface (e.g., function name) with the rest of the program.

n Ex. int main()

l Return type (‘int’): The data type of a value that the function returns

l Parameter or argument (inside of ‘( )’): The list of parameters to be passed to the function when the function is called, including the data type, order, number of the parameters, etc.

n The ‘main()’ function must be exist one time in the program because the function is a special one which have to be run at first.

þ Function Body

n It represents instructions to the computer about what the function should do.

n The portion enclosed in braces ({ and })

(13)

Functions (4/5)

þ Using Functions

n A C++ program should provide a prototype for each function used in the program.

n Format of a function prototype

l double swap(double, int);

– Function name: swap

– Parameters or arguments: double and int – Data type of return value: double

n Declaration position of the function prototype

l Declare the function prototype before the main() function in the source code.

l or, declare it in the user-defined header file and then include the header file in the source code.

l The function prototype must be declared before the statement for function call.

(14)

þ User-defined functions

#include <iostream>

using namespace std;

double stone2kg(double);

double main() {

double stone, kgs;

cout << “input weight in unit stone”;

cin >> stone;

kgs = stone2kg(stone);

cout << stone << “ Stone is ”;

cout << kgs << “kilos.\n”;

return 0;

}

double stone2kg(double sts) {

return 6.35 * sts;

Functions (5/5)

(2) Function prototype and

declaration of global variables

(3) Function definition (header + body) (1) Preprocessor directive

Definition of a main function

Definition of a user-defined function

(15)

C++ Coding Rules - Identifiers & Keywords (1/2)

þ Coding Rules and the Comment

n A statement must finish with semicolon (‘;’).

n The double slash (‘//’) means the comment.

n The comment can be on its own line or it can be on the same line as code.

l // This is line for comment.

l int a; // Comment for the variable

n The comment runs from the ‘//’ to the end of the line.

n In C language ‘/*’ (start of comment) and ‘*/’ (end of comment) are used.

þ Using Character

n Uppercase and lowercase alphabet

n Case sensitive: discriminates between uppercase and lowercase.

n Number: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 (digit)

n Special character: + - / = ( ) { } < > ‘ [ ] ! # $ % & _ | ^ ~ \ . ; : ?

(16)

C++ Coding Rules - Identifiers & Keywords (2/2)

þ Identifier

n User-defined identifier: Variables, function name, array name, etc.

except for keywords in C++ language

l Identifier: Variables, function name, array name, etc. that the user define.

(e.g., Korea, ROK, …)

l The first letter of the identifier should be start with the alphabet or ‘_’

(underscore).

l There is no limitation in the length for the identifier.

þ Keywords

n Reserved words which are used by the compiler n Identifiers used by the compiler

n Ex. break, if, else, long, switch, case, enum, register, typedef, char, …

(17)

Summary (1/3)

þ A C++ program consists of one or more modules called functions.

þ Programs begin executing at the beginning of the function called

‘main()’ (all lowercase), so you should always have a function by this name.

þ A function, in turn, consists of a header and a body.

n The function header tells you what kind of return value, if any, the function produces and what sort of information it expects arguments to pass to it.

n The function body consists of a series of C++ statements enclosed in paired braces (‘{}’).

(18)

Summary (2/3)

þ C++ statement types include the following:

n Declaration statement: It announces the name and the type of a variable used in a function.

n Assignment statement: It uses the assignment operator (‘=‘) to assign a value to a variable.

n Message statement: It sends a message to an object, initiating some sort of action.

n Function call: It activates a function. When the called function terminates, the program returns to the statement in the calling function immediately following the function call.

n Function prototype: It declares the return type for a function, along with the number and type of arguments the function expects.

n Return statement: It sends a value from a called function back to the calling function.

(19)

Summary (3/3)

þ C++ provides two predefined objects (‘cin’ and ‘cout’) for handling input and output.

þ C++ can use the extensive set of C library functions. To use a

library function, you should include the header file that provides

the prototype for the function.

(20)

Practice

þ Make a program that gets 2 variables, adds variables, and shows its result.

Preprocessor directives

int main() {

int x, y, z; // declare integer variables x, y, and z.

input value to variable x.

input value to variable y.

z = x + y;

print the value of z.

return 0;

}

참조

관련 문서

At first, I arranged the current conditions and function of container terminal in the port of Busan through the existing literature study on the function

Finally the relationship of the weight rate in fiber content of the product and fiber orientation function J and that of the injection molding condition and fiber

The “Asset Allocation” portfolio assumes the following weights: 25% in the S&amp;P 500, 10% in the Russell 2000, 15% in the MSCI EAFE, 5% in the MSCI EME, 25% in the

In a recent study([10]), Jung and Kim have studied the problem of scalar curvature functions on Lorentzian warped product manifolds and obtained partial

The goal of this research is worked one reasonable function analysis of the cost reduction and quality enhancement to spread out by using QFD (Quality

In gi ngi va,LCs are found i n oralepi thel i um ofnormalgi ngi va and i n smal l er amountsi nthesul cul arepi thel i um,buttheyareprobabl yabsentfrom thejuncti onal epi thel

– This line announces that the program uses a type void function called starbar() &amp; compiler expect to find the definition for this function elsewhere.. May put

: At a critical point an infinitesimal variation of the independent variable results in no change in the value of the function..