COMP322 - Introduction to C++

Similar documents
COMP322 - Introduction to C++

COMP322 - Introduction to C++ Lecture 07 - Introduction to C++ classes

Objects and streams and files CS427: Elements of Software Engineering

CS2141 Software Development using C/C++ Stream I/O

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

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

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

Introduction to C++ (Extensions to C)

The C++ Language. Output. Input and Output. Another type supplied by C++ Very complex, made up of several simple types.

Chapter 3 - Notes Input/Output

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

C++ Input/Output: Streams

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

Getting started with C++ (Part 2)

C++ Quick Guide. Advertisements

CS3157: Advanced Programming. Outline

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

C++ Programming Lecture 10 File Processing

CPE Summer 2015 Exam I (150 pts) June 18, 2015

This test is OPEN Textbook and CLOSED notes. The use of computing and/or communicating devices is NOT permitted.

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

Come and join us at WebLyceum

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

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

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

Formatting outputs String data type Interactive inputs File manipulators. Access to a library that defines 3. instead, a library provides input

Topic 2. Big C++ by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

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

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Streams - Object input and output in C++

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

EL2310 Scientific Programming

EL2310 Scientific Programming

Definition Matching (10 Points)

Fast Introduction to Object Oriented Programming and C++

Lecture 3 The character, string data Types Files

CSC 138 Structured Programming CHAPTER 4: TEXT FILE [PART 1]

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

Physics 6720 I/O Methods October 30, C++ and Unix I/O Streams

CSC 330 Object Oriented Programming. Operator Overloading Friend Functions & Forms

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

C++_ MARKS 40 MIN

Course "Data Processing" Name: Master-1: Nuclear Energy Session /2018 Examen - Part A Page 1

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team

CS490: Problem Solving in Computer Science Lecture 3: Input/Output

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Chapte t r r 9

Consider the following example where a base class has been derived by other two classes:

CS24 Week 3 Lecture 1

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

PROGRAMMING EXAMPLE: Checking Account Balance

We will exclusively use streams for input and output of data. Intro Programming in C++

This test is OPEN Textbook and CLOSED notes. The use of computing and/or communicating devices is NOT permitted.

EL2310 Scientific Programming

Streams. Parsing Input Data. Associating a File Stream with a File. Conceptual Model of a Stream. Parsing. Parsing

Operator overloading: extra examples

cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... C++ vs Java identifiers.

C++ Programming Classes. Michael Griffiths Corporate Information and Computing Services The University of Sheffield

UEE1303(1070) S 12 Object-Oriented Programming in C++

CISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture:

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Unit 1 : Principles of object oriented programming

Inheritance, and Polymorphism.

CMSC 202 Midterm Exam 1 Fall 2015

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

Object Oriented Programming In C++

This test is OPEN Textbook and CLOSED notes. The use of computing and/or communicating devices is NOT permitted.

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

for (int i = 1; i <= 3; i++) { do { cout << "Enter a positive integer: "; cin >> n;

Note 11/13/2014. They are like those i s, j s, and temp s that appear and disappear when the function starts and finishes...

C++ Input/Output Chapter 4 Topics

Chapter 6. I/O Streams as an Introduction to Objects and Classes. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

120++: a C++ Subset Corresponding to A Project-Based Introduction to C++

Software Design & Programming I

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

Streams. Rupesh Nasre.

BITG 1233: Introduction to C++

Chapter 12. Streams and File I/O. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Fall 2017 CISC/CMPE320 9/27/2017

Input and Output File (Files and Stream )

Absolute C++ Walter Savitch

CPSC 427: Object-Oriented Programming

Object Oriented Software Design II

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

Evolution of Programming Languages

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Short Notes of CS201

Week 5: Files and Streams

BITG 1113: Files and Stream LECTURE 10

G52CPP C++ Programming Lecture 18

W3101: Programming Languages C++ Ramana Isukapalli

I BSc(IT) [ Batch] Semester II Core: Object Oriented Programming With C plus plus - 212A Multiple Choice Questions.

Chapter 12. Streams and File I/O. Copyright 2016 Pearson, Inc. All rights reserved.

3.1. Chapter 3: Displaying a Prompt. Expressions and Interactivity

Transcription:

COMP322 - Introduction to C++ Winter 2011 Lecture 05 - I/O using the standard library & Introduction to Classes Milena Scaccia School of Computer Science McGill University February 1, 2011

Final note on pointers Pointers to constants double fee = 3.22; const double *p1 = &fee; const char *str1 = hello ; We cannot change the value that p1 or str1 point to. Constant Pointers int n = 21; int * const p2 = &n; char * const str2 = hello ; We can change the value of n or of the message, but p2 and str2 cannot point to anything else. Constant Pointers to Constants const int * const p3 = &n; const char * const str3 = hello ; We cannot alter the addresses of p3, str3, nor the value of the data pointed to.

Basic I/O in C++ Recall that in C, we have three file handles which are automatically created by the runtime library: stdin - standard input (from the keyboard/terminal) stdout - standard output (to the terminal/shell window) stderr - standard error C++creates three stream objects which encapsulate these handles: cin - standard input (from the keyboard/terminal) cout - standard output (to the terminal/shell window) cerr - standard error

Formatted I/O In C++, the << operator has been overloaded to write formatted data to stream objects: # include <iostream > using namespace std; int n = 1; double x = 2.0; cout << n << " " << x << endl; In C++, the >> operator has been overloaded to read formatted data from stream objects. # include <iostream > using namespace std; int n; double x; cin >> n >> x;

Output stream manipulators We can control the details of numeric output using stream manipulators, for example: Name Temp Description hex N Print integers in base 16 oct N Print in base 8 dec N Print in base 10 fixed N Set fixed precision scientific N Set scientific notation left Y Left justify in width right Y Right justify in width setw(n) Y Set minimum field width setprecision(m) N Set number of decimal places setfill( 0 ) N Set fill character Temp manipulators apply only to the next item printed.

Using I/O manipulators # include <iostream > # include <iomanip > using namespace std; int main() { int n = 123; double x = 123.4567; cout << hex << n << endl << dec << right << setw(6) << n << endl << setfill( * ) << left << setw(6) << n << endl << scientific << x << endl << fixed << setprecision(2) << x << endl; Produces the output: 7b 123 123*** 1.234567e+02 123.46

File input/output For I/O with named files, we use objects of class ofstream or ifstream : # include <fstream > ofstream out_file; ifstream in_file; in_file.open("in.txt"); out_file.open("out.txt"); Now one can read or write formatted data from these stream objects: in_file >> n; out_file << n << endl; When appropriate, we can close the file and reuse the object: in_file.close(); out_file.close();

Verifying file operations The >> operator returns a boolean which is true if the operation succeeds: int n; while (in_file >> n) { // process new input we can also call member functions to retrieve status: float x; in_file >> x; if (in_file.fail()) { // operation did not work, x is invalid

Reading unformatted characters If we naïvely attempt to read characters using the >> operator, this will skip spaces, end of line characters, etc. You can read individual characters, including spaces, using the get() method: char ch; while (in_file.get(ch)) { // Read next character into ch // do something We can put a character back into the input stream using unget(): in_file.unget(ch); // Pretend we didn t read ch

Reading line-by-line input We can read through an entire file using the getline() function: string nextline; ifstream in_file; in_file.open("input.txt"); while (getline(in_file, nextline)) { // process this line in_file.close(); We can then use string functions, or the istringstream class, to parse the line.

Introduction to Classes in C++ C++ is a multi-paradigm programming language: Generic Programming (Templates) (To be seen later) Imperative Programming (Procedural) (We have seen this) Object-Oriented Programming (Classes) (This lecture)

Classes in C++ A class can be thought of as an abstract data type, from which we can create any number of objects. In C++, a class allows us to hide implementation details from clients, and allows inheritance from one or more base classes, creating a class hierarchy (multiple inheritance).

Defining a Class A class definition can be broken up into two parts: 1. Class Declaration (Defined in a.h header file) Specifies what data members and member functions the class will have. 2. Class Implementation (Defined in a.cpp source file) Provides the code for the function bodies This is to separate the interface from the implementation. However, separating the code this way is not required. We may include code for the member functions in the.h file when it involves only one or two statements.

Class Declaration The following is an example declaration for the complex number class. This should be declared in a file called complex.h. class complex{ private: float real; // real part float imag; // imaginary part public: complex(); // default constructor complex(float r, float i); // full constructor float getreal(); float getimag(); void setreal(float r); void setimag(float i); complex add(const complex &y); ;

Class Declaration class complex{ private: float real; // real part float imag; // imaginary part public: complex(); // default constructor complex(float r, float i); // full constructor float getreal(); float getimag(); void setreal(float r); void setimag(float i); complex add(const complex &y); ; Visibility specifiers for members are supplied by section. Data members generally have private visibility. private = visible only to other members of this very class.

Class Declaration class complex{ private: float real; // real part float imag; // imaginary part public: complex(); // default constructor complex(float r, float i); // full constructor float getreal() const; float getimag() const; void setreal(float r); void setimag(float i); complex add(const complex &y); ; Constructors and other member functions have public (global) visibility. Note there is a third kind of visibility modifier: protected = visible to this class and all of its descendants. Class declarations end in a semi-colon.

Class Implementation We can implement the functions in a file complex.cpp as follows: #include "complex.h" complex:: complex() { // Default constructor real = imag = 0.0; complex::complex(float r, float i) { real = r; imag = i; float complex::getreal() const{ return real; void complex::setreal(float r){ real = r; //... complex complex::add(const complex &y) { return complex(real + y.real, imag + y.imag);

Using our class We can create a main function to use our class. int main(){ complex a(1.0, 2.0); complex b(2.0, 1.0); complex c; // invokes default constructor // We may also create a pointer to a complex number object complex *d = new complex(2.0,3.0); complex *e = new complex; c = a.add(b); cout << c.getreal() << " + " << c.getimag() << "i" << endl; This code will print 3 + 3i

Constructors Each class can define one or more constructors. These are special functions which have the same name as the class, and have no defined return type. Default Constructor Full Constructor

Constructors The appropriate constructor is called automatically when an object is created. The default constructor is the constructor with no arguments. It simply fills in a reasonable set of values. In our example main() function, the declaration complex a(1.0, 2.0); invokes the full constructor, while the declaration complex c; invokes the default constructor.

Member functions Unless specified otherwise, a member function is invoked by dereferencing a specific object: c = a.add(b); The object through which we invoke the member function is an implicit parameter to the function. It may be accessed simply by using a member name: complex complex::add(const complex &y) { return complex(real + y.real, imag + y.imag);

Const member functions If a member function is declared const, by placing the keyword after the parameter list, this means the member function will not make any changes to the implicit parameter: class complex { //... float getimag() const; //... ; float complex::getimag() const { return imag; In comparison, consider a function to set the imaginary part: void complex::setimag(float i) { imag = i;

Issues with constructors Consider the following class declaration: class intstack { int top; int max; int size; int *data; intstack(int max = 100) { // Constructor intstack::max = max; data = new int[max]; size = 0; int pop(); void push(int i); ;

Issues with constructors cont d What happens if we initialize a new object with an old one? int main() { intstack a; a.push(1); a.push(2); intstack b = a; // initialize new object with an old one cout << b.pop() << endl; a.push(3); cout<< b.pop() << endl; Perhaps surprisingly, this prints: 2 3 If no copy constructor is defined, C++ uses the default copy constructor which copies each field (i.e. shallow copy).

Copy constructor The solution to this problem is to provide a copy constructor, which copies the entire data structure. The most generic form of copy constructor is: class X { X(const X &src); //... ; For our intstack example, it would be: intstack(const intstack &src) { max = src.max; data = new int[max]; size = src.size; for (int i=0; i<size; i++) { data[i] = src.data[i]; Deep copy

Destructors A destructor is another special member function. The class destructor is called when an object of a given class is deleted. This gives an opportunity for the class to free memory or other resources. The destructor always has the name classname : class intstack { int top; int max; int size; int *data; intstack(int max = 100) { // Constructor intstack::max = max; data = new int[max]; size = 0; intstack(const intstack &src) { // Copy Constructor... ~intstack() { // Destructor delete [] data; int pop(); void push(int i); ;

More complex destructors class Symtable { private: Symbol *head; Symbol *find(string name) { for (Symbol *sp = head; sp!= NULL; sp = sp->link) if (sp->name == name) return sp; return NULL; public: Symtable() { head = NULL; // Empty ~Symtable() { while (head!= NULL) { // Free the list Symbol *sp = head->link; delete head; head = sp; void set(string name, int value); int get(string name) { Symbol *sp = find(name); return (sp == NULL? 0 : sp->value); ;

Recall the Symbol structure struct Symbol{ struct Symbol *link; // Next in linked list int value; // Value char name[32]; // Text name ;