C++ CSCE 343.

Slides:



Advertisements
Similar presentations
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Advertisements

 Wednesday, 10/16/02, Slide #1 CS106 Introduction to CS1 Wednesday, 10/16/02  QUESTIONS??  Today:  Return and discuss Test #1  Input from and output.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
1 File I/O In C++, I/O occurs in streams. A stream is a sequence of bytes Each I/O device (e.g. keyboard, mouse, monitor, hard disk, printer, etc.) receives.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
CSE 232: C++ Input/Output Manipulation Built-In and Simple User Defined Types in C++ int, long, short, char (signed, integer division) –unsigned versions.
ITEC 320 C++ Examples.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++
File I/O ifstreams and ofstreams Sections 11.1 &
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Fall 2012 Lecture 8: File I/O; Introduction to classes.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
C++ Programming Part 2 Michael Griffiths Corporate Information and Computing Services The University of Sheffield
CSE 332: C++ IO We’ve Looked at Basic Input and Output Already How to move data into and out of a program –Using argc and argv to pass command line args.
CLASSES : A DEEPER LOOK Chapter 9 Part I 1. 2 OBJECTIVES In this chapter you will learn: How to use a preprocessor wrapper to prevent multiple definition.
 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Chapter 15 - Class string and String Stream.
Lecture 19 CIS 208 Wednesday, April 06, Welcome to C++ Basic program style and I/O Class Creation Templates.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
Computing and Statistical Data Analysis Lecture 6 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Introduction to classes and objects:
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
CSE 332: C++ data types, input, and output Built-In (a.k.a. Native) Types in C++ int, long, short, char (signed, integer division) –unsigned versions too.
11 Introduction to Object Oriented Programming (Continued) Cats.
Lecture 14 Arguments, Classes and Files. Arguments.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
File I/O in C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.
CSCE Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 6: Miscellaneous 1 Based on slides created by Bjarne Stroustrup.
 Data Streams  Numeric Output  Numeric Input  Multiple Numeric Output  Multiple Numeric Input  Character Output  Character Input  String Output.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
CS212: Object Oriented Analysis and Design
Chapter 12 Classes and Abstraction
Introduction to C++ (Extensions to C)
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Chapter 8 (Part 3) Library Classes for Strings (P.405)
CS Computer Science IA: Procedural Programming
Introduction to C++ Systems Programming.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists File I/O Dr. Xiao Qin Auburn University.
Chapter Structured Types, Data Abstraction and Classes
COMP 2710 Software Construction File I/O
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
File I/O with Records Lesson xx
Chapter 9 Classes: A Deeper Look, Part 1
Creation and Use of Namespaces
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
when need to keep permanently
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
C++ File Structure and Intro to the STL
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
File I/O.
C++ Templates L03 - Iterator 10 – Iterator.
Reading from and Writing to Files
Programming with Data Files
C++ File Structure and Intro to the STL
STL and Example.
COMS 261 Computer Science I
What Is? function predefined, programmer-defined
Today’s Objectives 28-Jun-2006 Announcements
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Chapter 1 c++ structure C++ Input / Output
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
An Introduction to STL.
Reading from and Writing to Files
SPL – PS1 Introduction to C++.
Presentation transcript:

C++ CSCE 343

C vs. C++ C is (more or less) a subset of C++ main() is starting point, same as C Some important differences: Stream insertion operator ‘<<‘ preferred method for output (as opposed to printf) Stream extraction operator ‘>>’ preferred method for input (as opposed to scanf) I/O has object based mechanism Library header files do not have .h extension. New feature: references! Support for classes, inheritance, etc. (OO features) Built-in string class. Support for data-structures through the STL (Standard Template Library)

Namespaces Purpose: avoid conflicting names in various contexts. Scope resolution operator ‘::’ <namespace>::<member> std::cout std::string To avoid having to type the whole name (including namespace) use the using command: using std::cout; using std::string;

Basic Console Output Use the output stream cout #include<iostream> using std::cout; using std::endl; int main( int argc, char *argv[] ) { cout << “Hello World!” << endl; } Example code hello.cpp

The string class Constructing a string object: Example code string.cpp string s1; //create empty string object string s2(“cat”); string s3 = “cat”; Example code string.cpp write code to read and print a string

Objects Creating an object is the same as declaring it! The new operator is used in a slightly different context (subject for later chapters). For now, avoid using the new operator. Examples: string stg; // creates the empty string string myString(“Mickey”); Person myPerson(“Fred”, 22);

Classes Separate interface from implementation Interface goes into .h file. Use the preprocessor wrapper to avoid multiple inclusions: #ifndef MYCLASS_H #define MYCLASS_H … #endif Implementation goes into .cpp file. Include the interface file. Any users of the class must include the interface file (.h file). Time example Time.h, Time.cpp, TimeTest.cpp g++ TimeTest.cpp Time.cpp

More Object Creation Using class Time: Time sunset; reference to a Time object Time arrayOfTimes[5]; array of Time objects Time &dinnerTime = sunset; reference to same Time object as sunset Time *timePtr = &sunset; pointer to a Time object

C++ File I/O #include <fstream> Utilizes the stream classes std::ofstream std::ifstream Create file reference and open a file: ofstream outName(“file.txt”, ios::out ); ifstream inName( “back.txt”, ios::in ); ifstream inName; if.open(“newFile.txt”); Check for success opening the file if( !outName ) { … }

File Reading Read one character: Read one line of text into char[]: ifstream fin( "temp.txt" ); while( fin.get(ch) ) cout << ch; fin.close(); Read one line of text into char[]: ifstream fin("tmp.dat");   int MAX_LENGTH = 100; char line[MAX_LENGTH];   while( fin.getline(line, MAX_LENGTH) ) { cout << "read line: " << line << endl; } Close file fin.close() Example fileio.cpp and fileio2.cpp

More I/O Read one of text into a string Read a string from keyboard ifstream fin(“data.txt”); string s; while (getline( fin, s )) cout << s << endl; Read a string from keyboard getline( cin, s ); cout << "You entered " << s << endl;

Math Library To use math functions (like sqrt, sin, cos, etc.) #include <cmath> Functions are in std namespace: std::sqrt std::sin std::cos

In Class Exercise Create a C++ program that includes a class that represents a 2D point (x,y) coordinates. Make x and y private. Add getters and setters for x and y Make two constructors, one empty, and one that takes 2 parameters (inits for x and y). Add the following methods: double distFromOrigin() (calculates and returns the distance that this point is from the origin) string toString() (returns a string that represents the point, e.g. (2, 4.35) ) Write a test program that will test your class thoroughly. Submit your code using class submission program.

Standard Template Library Provides a set of templated (generic) classes for data structures. More on templates later We’ll focus on the map data structure. #include <vector> #include <map> typedef std::vector<string> VectorType; typedef std::map<string, Person> MapType;

STL Maps MapType myMap; Person p1; p1.name = “Joe Schmoe”; p1.age = 42; myMap[“joe”] = p1;

Iterators Used to traverse STL structures MapType::iterator iter; Typical loop (over all elements): for( iter = myMap.begin() ; iter != myMap.end(); ++iter ) { //process data }

Map Example simpleVectorExample simpleMapExample Person database creates separate .h and .cpp files example of overloaded stream insertion operator << reads person data from file people.txt

In-class Map / Vector Create a C++ class to hold <movie, movie stars> information create separate .h, .cpp, and testMovie files Data fields: private map<key,data> key of type string data of type vector of strings. public methods: add (movieName, MovieStar) printMovieInfo Write a test program that creates a Movie DB with this information Tiger : Lemmon, Matthau, Spacey Sin City: Lewis, De Niro, Alba Mean Girls: Fey, Lemon