C++ 프로그래밍실습. Visual Studio Smart Computing Laboratory

Similar documents
C++ 프로그래밍실습. Visual Studio Smart Computing Laboratory

This chapter introduces the notion of namespace. We also describe how to manage input and output with C++ commands via the terminal or files.

C++ 프로그래밍실습. Visual Studio Smart Computing Laboratory

C++ 프로그래밍실습. Visual Studio Smart Computing Laboratory

Reading from and Writing to Files. Files (3.12) Steps to Using Files. Section 3.12 & 13.1 & Data stored in variables is temporary

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 7. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

BITG 1113: Files and Stream LECTURE 10

CSc Introduc/on to Compu/ng. Lecture 19 Edgardo Molina Fall 2011 City College of New York

Scientific Computing

Chapter 14 Sequential Access Files

Text File I/O. #include <iostream> #include <fstream> using namespace std; int main() {

Input and Output File (Files and Stream )

File Operations. Lecture 16 COP 3014 Spring April 18, 2018

File I/O. File Names and Types. I/O Streams. Stream Extraction and Insertion. A file name should reflect its contents

Streams and Basic File I/O Tools for Stream I/O Character I/O Inheritance

Simple File I/O.

Programming II with C++ (CSNB244) Lab 10. Topics: Files and Stream

C++ Structures Programming Workshop 2 (CSCI 1061U)

WARM UP LESSONS BARE BASICS

C++ Quick Guide. Advertisements

Program Organization and Comments

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

C++ Programming Lecture 10 File Processing

I/O Streams and Standard I/O Devices (cont d.)

C++ basics Getting started with, and Data Types.

A SHORT COURSE ON C++

Local and Global Variables

C++ STREAMS; INHERITANCE AS

File Input / Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #9

Convenient way to deal large quantities of data. Store data permanently (until file is deleted).

CS 103 Unit 14 - Streams

Study recommendations for Quiz 5 - Chapter 5

Chapter 1 Introduction to Computers and Programming

Programming in C/C

Writing a Good Program. 7. Stream I/O

File I/O Christian Schumacher, Info1 D-MAVT 2013

Midterm Exam 5 April 20, 2015

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

FILE IO AND DATA REPRSENTATION. Problem Solving with Computers-I

Object Oriented Programming Using C++ UNIT-3 I/O Streams

AN OVERVIEW OF C++ 1

CSCS 261 Programming Concepts Exam 1 Fall EXAM 1 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

Partha Sarathi Mandal

Lecture 5 Files and Streams

G52CPP C++ Programming Lecture 17

Input and Output. Data Processing Course, I. Hrivnacova, IPN Orsay

Chapter 1 INTRODUCTION

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Object Oriented Programming In C++

Chapter 5: Loops and Files

C++ does not, as a part of the language, define how data are sent out and read into the program

Objects and streams and files CS427: Elements of Software Engineering

Lab # 02. Basic Elements of C++ _ Part1

Chapter 1: Why Program? Computers and Programming. Why Program?

Programming Language. Functions. Eng. Anis Nazer First Semester

Chapter 5: Prefix vs. Postfix 8/19/2018. The Increment and Decrement Operators. Increment and Decrement Operators in Program 5-1

CS103L SPRING 2018 UNIT 7: FILE I/O

Computer Science II Lecture 1 Introduction and Background

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

CMPS 221 Sample Final

Chapter 10 RECORDS (Structs )

Object-Oriented Programming in C++

Week 3: File I/O and Formatting 3.7 Formatting Output

I/O streams

Chapte t r r 9

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Piyush Kumar. input data. both cout and cin are data objects and are defined as classes ( type istream ) class

Week 5: Files and Streams

CS 103 Unit 14 - Streams

Chapter 1 Introduction to Computers and C++ Programming

Introduction to Programming EC-105. Lecture 2

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

In this chapter you will learn:

Discussion 10: Exam 2 Review

download instant at Introduction to C++

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

COL 100. Minor 2 Exam - Practice

Homework 4: (GRADUATE VERSION)

Understanding main() function Input/Output Streams

CS242 COMPUTER PROGRAMMING

COMP322 - Introduction to C++

C++ Scope Resolution Operator ::

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

C C C C++ 2 ( ) C C++ 4 C C

Computer Programming

CE221 Programming in C++ Part 1 Introduction

Lecture 3 The character, string data Types Files

Introduction. Lecture 5 Files and Streams FILE * FILE *

CSCE 206: Structured Programming in C++

CMSC 202 Midterm Exam 1 Fall 2015

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

File I/O CS 16: Solving Problems with Computers I Lecture #9

Unit-V File operations

Lecture 12. Monday, February 7 CS 215 Fundamentals of Programming II - Lecture 12 1

Computer Programming

Getting started with C++ (Part 2)

Functions, Arrays & Structs

CSCI 1061U Programming Workshop 2. C++ Basics

Chapter 12. Streams and File I/O

Transcription:

C++ 프로그래밍실습 Visual Studio 2015

Contents Namespaces I/O Stream Exercise

Practice1 -Namespaces

Practice 1 : Namespaces Name conflict problem Occurs when an application tries to use more than two libraries that have an identifier with the same name A potential problem in large applications involving several programmers (must be careful to find unique names) C++ provides namespaces to prevent name conflicts By disambiguating a name using the scope resolution operator :: E.g., identifiers in the standard C++ libraries are covered by the namespace std (i.e., cout in the iostream library is actually identified as std::cout) [ Library abc ] int inflag; Linkage [ Application ] inflag = 10; Linkage [ Library xyz ] int inflag; Which library does inflag belong to?

Practice 1 : Namespaces #include <iostream> int main(void) {... cout << Test ;... Valid C++ programs #include <iostream> int main(void) {... std::cout << Test ;... (a) Scope resolution OP #include <iostream> using std::cout; int main(void) {... cout << Test ;... (b) Using declaration #include <iostream> using namespace std; int main(void) {... cout << Test ;... (c) Using directive

Practice 1-1 : Namespaces n set() n n set() konkuk set() google Global

Practice 1-1 : Namespaces #include<iostream> using namespace std; //std 이름공간에선언된모든이름에 std:: 생략 int n; void set() { ::n = 10; // 명시적전역변수 ( 아무런 namespace 에속해있지않다.) namespace konkuk { int n; void set() { konkuk::n = 20; //namespace 명시하지않아도됨. namespace google { int n; void set() { google::n = 30; //namespace 명시하지않아도됨.

Practice 1-1 : Namespaces int main() { ::set(); konkuk::set(); google::set(); cout << ::n << endl; cout << konkuk::n << endl; cout << google::n << endl; Execution Result: 10 20 30

Practice 1-2 : Openness of Namespaces namespace A { int f(); namespace A { int g(); int h(); = namespace A { int f(); int g(); int h(); (a) Separate namespaces (b) Single integrated namespace

Practice 1-2 : Openness of Namespaces #include<iostream> using namespace std; int n; void set(); namespace konkuk { int n; void set(); namespace google { int n; void set();

Practice 1-2 : Openness of Namespaces int main() { ::set(); konkuk::set(); google::set(); cout << ::n << endl; cout << konkuk::n << endl; cout << google::n << endl; Execution Result: 10 20 30 void set() { n = 10; namespace konkuk { void set() { n = 20; namespace google { void set() { n = 30; = void ::set(){ n = 10; void konkuk::set(){ n = 20; void google::set(){ n = 30;

Practice 1-3 : Nested Namespaces n set() n set() student n konkuk set() Global

Practice 1-3 : Nested Namespaces #include<iostream> using namespace std; int n; void set() { n = 10; namespace konkuk { int n; void set() { n = 20; namespace student { int n; void set() { n = 30;

Practice 1-3 : Nested Namespaces int main() { ::set(); konkuk::set(); konkuk::student::set(); cout << ::n << endl; cout << konkuk::n << endl; cout << konkuk::google::n << endl; return 0; Execution Result: 10 20 30

Practice2 I/O stream

Practice 2 : I/O Stream C++ provides an alternative to I/O library of C Easier-to-use, extensible, and more flexible This section introduces basic I/O functionalities of C++ The details on C++ I/O are covered in Chap. 8 Stream-based I/O of C++ Input to a C++ program is treated as a stream of consecutive bytes from an input device (e.g., keyboard, disk, scanner, ) Output from a C++ program is also treated as a stream of consecutive bytes to an output device (e.g., video display, disk, printer, ) Plan 9 from outer space

Practice 2 : I/O Stream The standard I/O variables are used with the I/O operators Input operator >> E.g., cin >> x; // reads a value from the keyboard and store the value into x Output operator << E.g., cout << x; // writes the value of x to the display I/O operators Left-associated Evaluated from left to right E.g., cout << x << y; // writes x first and then y to the display Automatically recognize type of the data No format string is required (cf., printf or scanf requires a format string) E.g., int x; cin >> x; // == scanf( %d, &x) cout << x; // == printf( %d, x)

Practice 2-1 : I/O Stream #include<iostream> using namespace std; int main() { int width; cout << " 너비를입력하세요 : "; cin >> width; int height; cout << " 높이를입력하세요 : "; cin >> height; int area = width*height; cout << " 면적 : " << area << endl; Execution Result: 너비를입력하세요 :3 높이를입력하세요 :5 면적 : 15

Practice 2-2 : I/O Stream It is possible to read from and write to (disk) files in the same way as using cin and cout At first, fstream header needs to be included Then, we can replace cin with an object of ifstream class associated with the input file, and cout with an object of ofstream class associated with the output file The I/O operators >> and << are used in just the same way as they are used with cin and cout

Practice 2-2 : I/O Stream #include <iostream> #include<string> #include <fstream> // 입출력헤더 using namespace std; int main() { ofstream ofs; //ofstream 클래스의객체선언 string str = " 친구 3 명의이름과나이를입력하세요."; ofs.open("friend.txt"); // 파일열기 if (!ofs) { cout << " 파일을열수없다." << endl; exit(0); cout << str << endl; for (int i = 0; i<3; i++) { int Age; string Name; cout << " 이름 : "; cin >> Name; cout << " 나이 : "; cin >> Age; ofs << Name << " " << Age << endl; // 출력파일스트림에기록 ofs.close(); // 파일닫기 Reads a values from the keyboard and store the value into Name and Age //fileload();

Practice 2-2 : I/O Stream void fileload() { int Age; string Name; string filename = "friend.txt"; ifstream ifs; //ifstream 클래스의객체선언 ifs.open(filename.c_str()); // 파일열기 if (!ifs) { cout << " 파일을열수없다." << endl; exit(0); cout << "\n==list of your friends from the stored file==\n"; while (ifs >> Name >> Age) { cout << Name << " " << Age << endl; ifs.close(); // 파일닫기 입력파일스트림에서읽기

Practice 2-2 : I/O Stream

Exercise-1 자유롭게 Namespaces 를설계하시오 - 최소 3 개의 Namespaces 를포함한다. - Nested Namespaces 를사용한다. Exercise-2 자신의이름, 나이, 메일을입력하여텍스트파일로출력하시오. - 출력된텍스트파일을읽어콘솔창에출력

Course Homepage How to access URL: sclab.konkuk.ac.kr Downloading class material Students can download syllabus and lecture notes in PDF format Class announcement About homework and project Exam schedule and result And so on 24

Submit Teaching assistant: 장성수 Office: 신공학관 1216 호 ( 대학원 SCLab 연구실 ) Email: pik1100@naver.com Title of the email : [2018][Practice#]_student# _ student _ name Ex) [2018][Practice02]_201700000_ 장성수 Create zip file. (C++ project folder) - 주의 : 메일양식이잘못될경우채점이되지않을수있음. 질문메일 : pik1100@naver.com : 장성수