Object-Oriented Programming
Total Page:16
File Type:pdf, Size:1020Kb
OBJECT-ORIENTED PROGRAMMING MODULE #8 Best Practice Principles Objectives: Understand Predefined Classes Understand Standard Input Understand Standard Output C++ Standard Input and Output !C++ header files & predefined streams •iostream - the standard input and output library for C++ •cin - unbuffered standard input •cout - unbuffered standard output •cerr - unbuffered standard error •clog - buffered standard error !I/O Classes for predefined objects - a user defined data type with data and functions within its declaration •streambuf - A temporary repository for sequences of characters that are in transit •ios - Formats the sequences of characters into values of a particular type for display to the console !Two level process •Data is treated as a sequence of characters •Data is treated as a series of values of a particular type C++ I/O Controls !Operators - an overloaded operator defines a different use for an existing symbol •<< insertion operator used with cout •>> extraction operator used with cin !Manipulators •dec - forces the conversion to base 10 •hex - forces the conversion to base 16 •oct - forces the conversion to base 8 •endl - flushes the buffer and inserts a new line character •flush - flushes the buffer •ws - extracts whitespace characters •ends - inserts a null character C++ Standard Output Object - cout !overloaded operator •<< insertion operator is used to display fundamental data types !char •Displayed in a field one character wide !strings •Displayed in a field equal to its length !int •Displayed in a field large enough for the number and sign (includes long) !float •Displayed in a field large enough for the number and six places to the right of the decimal. •Trailing zeroes are not displayed. (include doubles) C++ Standard Input Object - cin !C++ standard input (keyboard) !>> extraction operator is (overloaded) used to extract characters from the input stream and stores the value to a fundamental data type !Input • Skips white space to locate the first valid character in the sequence and reads until an invalid character type is found or end of character sequence is encountered • Can use manipulators dec, hex, oct, ws, setfill, setprecision and setw !Member functions • get(char &) or get(void) -extracts one character • getline(char *,char='\n') -extracts a character sequence • ignore(int=1,int=EOF) -discards a character sequence C++ Predefined Text Object - string !String objects are a special type of container, specifically designed to operate with sequences of characters !C++ string objects belong to a class with many built-in features to operate with strings •size method - Returns a count of the number of characters in the string. •length method - Returns a count of the number of characters in the string •capacity method - Returns the size of the allocated storage space in the string object •clear method - sets to an empty string, erasing any previous content and leaving its size 0 •empty method - return true if empty, false of the size is not zero •at method - Returns the character at position in the string •append method - The current string content is extended by adding an additional appending string at its end •substring method - Returns a string object with its contents initialized to a substring of the current object C++ Displaying Output !Constants defined within the ios class !Whitespace and padding • ios::skipws ios::left ios::right ios::internal !Base conversion • ios::dec ios::oct ios::hex !Integral formatting • ios::showpos !Floating-point • ios::showpoint ios::scientific ios::fixed !Case • ios::uppercase !Buffer flushing • ios::stdio ios::unitbuf Java Standard Input and Output !Java predefined streams • Represents a stream of bytes entering into the program from an external source or a stream of bytes flowing from the program to an external sink • The default standard output device is the screen • The default standard input device is the keyboard but can be redirected to a different device at the operating system level ! java.io package - contains predefined classes to perform I/O in Java • FileReader - most frequently used class for reading input data • FileWriter - most frequently used class for writing output data !Stream Types - a stream is a sequence of data •InPutStream - used to read data from a source •OutPutStream - used for writing data to a destination Java Stream Types !Standard Streams • System.in - sends data to the program (usually from the keyboard representing standard input stream) • System.out - outputs data by the program (usually from a computer screen representing standard output stream) • System.err - outputs error data by the program (usually from a computer screen representing standard output stream) !Other Stream Types • Byte Streams - use to perform I/O of 8-bit bytes • Character Streams - used to perform I/O of 16-bit unicode • File InputStream - used for reading data from files Java System Class !java.lang.System - contains several classes to support I/O capabilities in Java •Standard output - System.out with methods such as print() or println() •Standard input - System.in with methods such as read() !Other System facilities • Access to externally defined properties and environment variables • A utility method for quickly copying a portion of an array • A means of loading files and libraries Java Standard Input Object - System.in !Java standard input (keyboard) !InputStream Reader - reads the standard input stream !Member functions • read () - reads a single character • ready() - indicates if the stream is ready to be read Java Input Object - Scanner !Java input (keyboard) !java.util.Scanner - reads the standard input stream (System.in) • A simple text scanner to parse primitive types and strings • Scanner breaks its input into tokens which by default is whitecaps • Scanner is not safe for multithreaded use without external synchronization !Scanner Methods • nextLine() - reads the line of data from standard input • nextInt() - reads numeric data from standard input • nextDouble() - reads numeric fractional data from standard input Standard Output Object - System.out !Java standard output (Screen) !System.out - writes to the standard output device !Member functions • print() - displays the the text without a carriage return, line feed • println() - displays the text with a carriage return, line feed afterwards Java Predefined Text Object - String ★ A string is a sequence of characters ★ Java Strings are standard objects with built-in language support ★ String represents a fixed-length sequence of characters (immutable) ★ Does not create a new instance if the string is already represented in the string pool Java Predefined Text Object - StringBuffer ★ A StringBuffer is a sequence of characters ★ String Buffer represents a variable-length sequence of characters (mutable) C# Standard Input and Output !C# using System • Contains commonly used values and reference data types and more • System.Console - represents the standard input, output and error streams • Streams - is a sequence of data ! using System namespace - contains predefined classes for I/O support C# Stream Types !Standard Streams • System.Console.In - sends data to the program (usually from the keyboard representing standard input stream) • System.Console.Out - outputs data by the program (usually from a computer screen representing standard output stream) !Methods • System.Console.WriteLine() - writes the current line terminator to the standard output stream or write() without the terminator • System.Console.ReadLine() - reads the next line of characters from eh standard input stream • System.Console.ReadKey() - reads the next character or function key pressed • System.Console.Read() - reads the next character from the standard input stream C# Standard Input Object - System.Console.In !C# standard input (keyboard) !System.Console.In - reads the standard input stream C# Standard Input Object - System.Console.Out !C# standard output (screen) !System.Console.Out - writes to the standard output stream Predefined C# Reference Types - Object "Object is the root of the hierarchy "The reference utilizes four bytes of storage "Storage overhead ★Value types - zero bytes ★Reference types - eight bytes "Public methods ★void Object() ★String ToString() ★bool Equals(object) ★bool ReferenceEquals(object) ★public System.Type GetType() ★int GetHashCode() "Protected methods ★void Finalize() ★Object MemberwiseClone() C# Type System Type Size (bytes) object System.Object 0/8 overhead Root type object Character string string Predefined C# Reference Types - String "A string is an immutable sequence of Unicode characters "String Literal is enclosed in double quotes (e.g. “text information”) "Additional backslashes required for strings with escape characters (e.g. “\\\\myfile\\fileName.cs”) "Verbatim string literals (e.g. @”\\myfile\fileName.cs”) ★Ignores escape sequences except double quotes ★Can span multiple lines C# Type System Type Size (bytes) String System.String 20 minimum Root type object Character string string Predefined C# Reference Types - String "A String is a reference type "A String can contain nulls "String is overloaded with the == operator which checks for content equality, not reference equality (e.g. “Hello”.Substring(0,2)==”he”) "Intern pool - guarantees the same string literal if referenced by different references "Useful Methods ★Equals - test for reference equality ★CompareTo - returns 0 if equal, negative if less than, positive if greater than ★Contains - searches for the string token in the object, returns bool True or False ★IndexOf - returns the starting indexed value of the found string or a negative value if the string is not found in the object ★Substring - returns the substring value based on the start and end value ★ToUpper - returns the upper case values for the string.