CS 117 Spring 2002 Review for Exam 3 arrays strings files classes.

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Advertisements

Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Chapter 10.
CS 117 Spring 2002 IDAHO TRANSPORTATION DEPARTMENT Internship Announcement POSITION TITLE: Computer Science Intern JOB DESCRIPTION: Translate technical.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
Chapter 9: Arrays and Strings
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 8 Arrays and Strings
Chapter 3: Input/Output
Program Input and the Software Design Process ROBERT REAVES.
Review of C++ Programming Part II Sheng-Fang Huang.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
In Addition... To the string class from the standard library accessed by #include C++ also has another library of string functions for C strings that can.
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
 2006 Pearson Education, Inc. All rights reserved Arrays.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 8 Arrays and Strings
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
EGR 2261 Unit 9 Strings and C-Strings  Read Malik, pages in Chapter 7, and pages in Chapter 8.  Homework #9 and Lab #9 due next week.
File I/O ifstreams and ofstreams Sections 11.1 &
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Define our own data types We can define a new data type by defining a new class: class Student {...}; Class is a structured data type. Can we define our.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Review Binary Numbers Bit : 0 or 1 Byte: 8 bites 256 different values 2 8 KB : 1024 bytes 2 10 bytes MB : 1024 * 1024 bytes 2 10 * 2 10 (2 20 ) bytes GB.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Structured Programming Instructor: Prof. K. T. Tsang Lecture 6: Arrays 数据序列, 数组 1.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Data Types Storage Size Domain of all possible values Operations 1.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Using Member Functions and Data Members.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
CS 1704 Introduction to Data Structures and Software Engineering.
Std Library of C++ Part 2. vector vector is a collection of objects of a single type vector is a collection of objects of a single type Each object in.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Fundamentals of Characters and Strings
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Chapter 3 Input output.
Recap Week 2 and 3.
CS 144 Advanced C++ Programming February 7 Class Meeting
Arrays Arrays A few types Structures of related data items
C++ Programming Lecture 20 Strings
Instructor: Dr. Michael Geiger Spring 2019 Lecture 13: Exam 1 Preview
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
Review for Midterm 3.
Corresponds with Chapter 5
Presentation transcript:

CS 117 Spring 2002 Review for Exam 3 arrays strings files classes

Chapters covered Hanly –Chapter 6 skip sections 1, 7 –Chapter 7 skip section 6 –Chapter 9 Friedman-Koffman –Section 3.7 –Chapter 8 –Chapter 9 not section 7 –Chapter 10 –Section 11.7 Don't forget that there are web notes about all of these

Arrays a collection of values/objects of the same type –they can be of any type int values[20] sets aside memory for 20 integers The elements are accessed by putting the index of the element in square brackets, e.g. values[3] For an array declared to have n elements, the array index runs from 0 to n-1 –You have to keep track of how many of the array elements have been assigned values.

For arrays, know how to declare an array int score[30]; initialize an array when it is declared double x[ ] = {1.9, 2.8, 3.7, 4.6, 5.5}; access an element of the array score[3] –first element has index 0

Array know how cont. use a loop to do something with every element of the array for (int i=0; i<numElements; i++) sum = sum + score[i]; pass the entire array to a function highScore = max( score, nstudents);

C-style strings A C-style string is an array with elements of type char It should be terminated with the null character –character whose ASCII code is 0 –'\0' Some useful functions for using C-style strings are in

C-style Strings know how to declare a C-style string char cstr[6]; –this can hold 5 characters plus the termination character access an element of a C-style string –array index starts at 0 –ith element is cstr[i-1] initialize a C-style string when you declare it –char dstr[7]="bat"; –number of elements optional in this case

I/O for C-style strings input a C-style string with >> –get next sequence of non-space characters from the input stream get multiple words with istream::getline( char[], int) output a C-style string with <<

string.h strlen - returns number of characters in the string –doesn't count the null character –strlen(dstr) will return 3 strcpy - to copy a string into another one –strcpy( cstr, "man") will put man into cstr strcat - to append one string onto another –strcat( dstr, cstr) puts "batman in dstr strcomp - for comparing two string: –returns 0 if they are the same –strcomp( dstr, cstr) will be non-zero –strcomp( cstr, "man") will return 0

C++ string class An object-oriented way to work with text strings #include You can –declare a string string str1; –initialize a string when you declare it string str2("two"); –access an element of a string the ith element is str[I-1] or str1.at(i-1) –get the length with str1.length()

C++ string class operators input a string with >> - get a sequence of non- space characters output a string with << assign a value using = str1 = "one"; compare using familiar operators str1 == str2 str2 < str1 concatenate with + str1 + str2

File I/O #include classes –ifstream for input –ofstream for output Constructors –default –initializing takes c-string filename

File I/O functions you should know how to use –open( char filename[]) –eof() - to test for end of file –fail() - to test for all errors –close() read/write the same as for cin/cout

iomanip setiosflags(ios::flagname) resetiosflags(ios::flagname) –right / left –fixed –scientific –showpoint setw( int fieldwidth) setprecision( int digits)

Class Declaration what member variables are needed –data needed to represent the object what functions are needed –functions represent behavior and/or services

Form of declaration class aClass { public: aClass();// zero parameter constructor aClass( int v1, double v2); int getVar1() const; // accessors void setVar2( double d); // mutator functions private: int var1; double var2; friend operator<<( ostream &, const Angle &); };

Class functions constructors used to initialize objects –name of constructor is same as the class name member functions –accessors provide read-only access to member variables –mutators allow user to change member variables –friend functions –overloaded operators

Functions associated with classes overloaded operators provide arithmetic and/or comparisons in same ways as for numbers friend functions allowed access to the private members of the class

Member functions function declarations (prototypes) go into class definition function definitions –usually outside of class definition (need scope resolution operator ::) –inline definitions (usually very short) are contained in the class definition and don't need the scope resolution operator

Member Function Signatures Inside the class definition, prototypes look just like those of a regular function double someFunction( int param1, int param2); member function has access to –member variables –parameters –any locally declared variables

Member Function Definitions The function definition contains the code that is executed when the function is called. double aClass::memberFunction( int param1, int param2) { // code that is executed return aDouble; } scope resolution operator: aClass:: indicates to the compiler that this is a member function of class aClass

Declaring Objects You declare an object by giving the className (its type) and a variable name. theClass obj1; This calls the zero parameter constructor You can initialize an object when you declare it by providing arguments theClass obj2( 12, 3.4); This calls a constructor which has int and double parameters.

Calling member functions From outside the class, you call a member function using the. operator double x = obj1.someFunction( arg1, arg2); Inside the class, only need the. operator for an object other than the current one You use overloaded operators the same way you always use operators: obj1 < obj2; cout << obj1;

Things to remember member variables should not be passed to member functions –these functions already have access to them all class members are private unless they appear in the public section of the class definition –private members are not directly accessible from outside the class don't redeclare the member variables –they are declared in the class declaration member functions that use only member variables need no parameters

Member Functions accessor functions (get functions) return the values of the member variables –don't always have one for every member variable provide set functions if you want the user of the class to be able to change the value of a member variable