• Tidak ada hasil yang ditemukan

C++ Programming

N/A
N/A
Protected

Academic year: 2024

Membagikan "C++ Programming"

Copied!
33
0
0

Teks penuh

(1)

C++ Programming

Ch. 1 Getting Started

Lecture Note of Digital Computer Concept and Practice

Spring 2014

Myung-Il Roh

Department of Naval Architecture and Ocean Engineering

(2)

Ch. 1 Getting Started

(3)

Contents

þ History of C++

þ Characteristics of C++ Language þ Programming General

þ Mechanics of Creating a Program þ Creating the Source Code File

þ Compilation and Linking

þ C++ Programming Using Microsoft Visual C++

þ Practice

(4)

History of C++ (1/2)

þ Programming Languages

n Low-level language: Machine language, Assembly language

l It is represented with 0 and 1, and thus familiar with computer.

l It is different according to CPU (Intel, Sun, etc.) of computer.

n High-level language: FORTRAN, BASIC, PASCAL, C, C++, …

l It is represented with language-specific grammar, and thus familiar with human.

l It needs a compiler or interpreter.

l Cf. Compiler vs. Interpreter

þ C Language

n Developed by Dennis Ritchie of Bell Laboratories in the early 1970s.

n Named after B language by Kenneth L. Thompson of Bell Labs.

n Improve disadvantages from non-structured languages, such as BASIC and FORTRAN.

n Structured language

(5)

History of C++ (2/2)

þ C++ Language

n Developed by Bjarne Stroustrup of Bell Laboratories in the early 1980s.

n Including C language Æ Expansion of C language

n One of languages for making an object-oriented program

l Procedural programming:

“Data + Algorithm = Program”

Ex. Baking the cake Data

» Ingredients: 1 cup of flour, 5 eggs, …

» Designated temperature and time: 180℃, 20 minutes

Algorithm

» Measuring ingredients and mixing them in the right order.

» Baking the cake at designated temperature and right time in the oven.

l Object-oriented programming:

(6)

Characteristics of C++ Language (1/2)

þ Class

n A specification describing a data form of C++.

n A class defines how data is stored and used.

þ Object

n Particular data structure constructed according to the plan n Instance generated from the instantiation of class

þ Example

n Class of “Corporation executive”

l Name, Title, Salary, Unusual abilities, Phone number, …

n Object of “Corporation executive”

l Dong-Il Kim, Vice President, $325,000, Drinking, 010-1111-2222, …

l Su-Min Park, Executive Director, $250,000, Working instead of vice president, 010- 2222-3333, …

(7)

Characteristics of C++ Language (2/2)

þ Object-Oriented Programming (OOP)

n Inheritance

l Mechanism of reusing and extending existing classes without modifying them

l Inheriting existing classes’ properties (definition of data structures and functions) and adding only needed part

l Ex. Employee class, Executive class, Staff class

n Polymorphism

l Creating multiple definitions for operators and functions, with the programming context determining which definition is used

l Ex. Area(radius), Area(width, height)

n Abstraction

l Providing only essential information and hiding classes’ background details (data hiding)

l Ex. When you transfer money to another person, you don’t have to know receiver’s balance.

(8)

Programming General

þ Portability

n “Does my code which is written on my computer run at workstation in school?”

n Obstacles to portability

l Hardware problem

Ex. A hardware dependent program cannot work at the different computer, such as a control program of video card.

l Software problem

Ex. A programming language, operating system, compiler, etc. are the same between computers.

þ Standards

n C: Ritchie & Kernighan published ANSI standards based on C language.

n C++: ANSI, ISO standards (1998, ISO/IEC 14882: 1998)

(9)

Mechanics of Creating a Program

Compiler translates the source code to the machine language and creates object code.

• At this time, preprocessing (attaching header file, etc.) and checking

programming grammar are performed.

Linker links object codes and library codes, and makes executable code (*.exe)

• That is, Linker combines object codes with the startup code and the library code to make executive file.

* Library: A set of functions which is used fluently (Ex. math library, graphic library).

myprog.cpp

myprog.obj

myprog.exe

(10)

Creating the Source Code File

spiffy

.

cpp

Base name for file File name extension

File name of the source code file

A period

C++ Implementation(Compiler) Source Code Extension(s) Uix

GNU C++

Digital Mars Borland C++

Watcom C++

Microsoft Visual C++

Metrowerks Codewarrier (Mac)

C, cc, cxx, c

C, cc, cxx, cpp, c++

cpp, cp cpp

cpp

cpp, cxx, cc

cpp, cp, cc, cxx, c++

(11)

Compilation and Linking

þ Compile: Compile the code in the file that is currently open.

n Build (make): Compile the changed code from all the source code files in the project.

n Build all: Compile all the source code files.

þ Link: Combine the compiled source code (object code) with the necessary library codes (startup code, library code).

þ Run (Execute): Run the program.

(12)

C++ Programming Using Microsoft Visual C++

þ Integrated Development Environment

n Project management (program management)

þ Code Editor

n Text editor, Highlighting instructions

þ Compiler

n Translate the source code to the machine language which can be recognized by computer.

þ Debugging Tool

n Check and diagnosis C/C++ Grammar error.

n Show error message when the error occurred.

(13)

Run Microsoft Visual C++

파일 Æ 새로 만들기 Æ 프로젝트

(14)

Create a New Project (1/6)

(15)

Create a New Project (2/6)

Test

Console program running under Dos environment

(16)

Create a New Project (3/6)

MFC Program running on the 32 bit Windows environment

Test

(17)

Create a New Project (4/6)

Win32 Console Program

(18)

Create a New Project (5/6)

Win32 Console Program

(19)

Create a New Project (6/6)

Win32 Console Program

(20)

Add a New File to the Project (1/3)

프로젝트 Æ 새 항목 추가

(21)

Add a New File to the Project (2/3)

(22)

Add a New File to the Project (3/3)

(23)

Project Folder and File Hierarchy

þ Project Folder

Æ New Created File Æ Project File

(24)

Example Code

#include "stdio.h"

void main() {

printf("Hello, World!\n");

}

(25)

Compile the Source Code

(26)

Run the Program

(27)

Screenshot of the Program

(28)

Practice

þ A Program without tasks (Do nothing)

n Comment

l // : Comment one whole line (From begin to end of the line) l /* */ : Comment between /* and */ (can be multiple line)

main() {

// This is comment. Do nothing.

/* C style comment */

}

(29)

Reference Slides

(30)

Compiler vs. Interpreter (1/3)

þ Compiler

n Compiler is a special program that translates a high-level language to the internal language (machine language) of a particular computer.

n The compiler translates the source code to machine language in order to create the object code. An object code is a sequence of statements or instructions with machine language which can be processed by

CPU.

n Usually, the compiler is saved on secondary memory unit (HDD, SSD,

…). The compiler separates all statements in the source code, and

creates the object code continuously when a statement refers another statement to make object code accurate.

n The compiler is adopted to the 3rd, 4th, and 5th generation

programming language. Thus, most of high-level languages such as FORTRAN, C, C++, COBOL, and PASCAL require compiler.

Source Code

Object Compiler Code

(31)

Compiler vs. Interpreter (2/3)

þ Interpreter

n Another way to run a high-level language program. The interpreter read a source code one line at once. When a statement of the source code was reading, the interpreter defines a statement’s function and executes the statement as its function repeatedly.

n It is hard to find errors on the source code before running the

program when using an interpreter. Also, the creation of the object code is done by the interpreter. Thus, disk storage for the object code is not required.

n Usually, the interpreter is saved on the main memory unit (ROM), and the interpreter translates all statements in the source code. Thus, the interpreter is slower than the compiler that uses the object code

which is stored on the disk drive. The interpreter is used by page description language such as LISP and PostScript.

Source Code

Execute Command Interpreter

(32)

Compiler vs. Interpreter (3/3)

Compiler Interpreter

(33)

Procedural Programming vs. Object-Oriented Programming

þ Example of The game Rock Paper Scissors

int main() {

int u, c, winner;

// A user selects a hand.

u = GetUserHand();

// A computer selects a hand.

c = GetComputerHand();

// Judge a winner.

winner = GetWinner(u, c);

// Display the winner.

ShowWinner(winner);

return 0;

}

int main() {

int u, c, winner;

JanUser user;

JanComputer computer;

JanJudge judge;

// A user selects a hand.

u = user.GetHand();

// A computer selects a hand.

c = computer.GetHand();

// Judge a winner.

winner = judge.GetWinner(u, c);

// Display the winner.

judge.ShowWinner(winner);

return 0;

Procedural programming Object-oriented programming

Referensi

Dokumen terkait

As the main part of my final-year project, I developed a system where it will interface the circulator to microcontroller using embedded C programming to measure

The primary aim of the language extensions made in the last six years has been to enhance C++ as a language for data abstraction and object-oriented programming in general and

The primary aim of this book is to help the reader understand how the facilities offered by C++ support key programming techniques.. The aim is to take the reader far beyond the

The implicit conversion of an array name to a pointer to the initial element of the array is exten- sively used in function calls in C-style code.. The snag is that it is impossible

Visualization Results Using Genetic Programming Fuzzy C-Means Furthermore, an assessment process will be carried out to determine the segment of the customer by taking the minimum and

C O N T E N T S FOOD HYGIENE BASIC TEXTS Fourth edition PREFACE iii RECOMMENDED INTERNATIONAL CODE OF PRACTICE GENERAL PRINCIPLES OF FOOD HYGIENE 1 CAC/RCP 1-1969 PRINCIPLES FOR

Regarding this, the research aims to implement the Kruskal algorithm in the C++ programming language to find the fastest route between tourist attractions.. Using the Kruskal algorithm,