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
Ch. 1 Getting Started
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
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
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:
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, …
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.
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)
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
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++
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.
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.
Run Microsoft Visual C++
파일 Æ 새로 만들기 Æ 프로젝트
Create a New Project (1/6)
Create a New Project (2/6)
Test
Console program running under Dos environment
Create a New Project (3/6)
MFC Program running on the 32 bit Windows environment
Test
Create a New Project (4/6)
Win32 Console Program
Create a New Project (5/6)
Win32 Console Program
Create a New Project (6/6)
Win32 Console Program
Add a New File to the Project (1/3)
프로젝트 Æ 새 항목 추가
Add a New File to the Project (2/3)
Add a New File to the Project (3/3)
Project Folder and File Hierarchy
þ Project Folder
Æ New Created File Æ Project File
Example Code
#include "stdio.h"
void main() {
printf("Hello, World!\n");
}
Compile the Source Code
Run the Program
Screenshot of the Program
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 */
}
Reference Slides
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
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
Compiler vs. Interpreter (3/3)
Compiler Interpreter
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