INB371 Week 3 L.docx

6 Pages
126 Views
Unlock Document

Course
INB221
Professor
Malcolm Corney
Semester
Fall

Description
INB371 Data Structures and Algorithms Lecture 3 Interfaces, Standard Libraries and Computational Complexity Interfaces are a common boundary between two distinct entities. In the physical world, think about the surface of a river. There's an 'air to water' interface, a separation of the avian world and the marine world. In programming, it's a conceptual boundary between the implementation of libraries. Its purpose is to provide information needed to use the library without revealing the implementation details, so at any time you can alter the interface, and the client programs that use the interface don't change. The interface needs to be a part of the program. The way it's done in C++ is by separating the functionality of an interface into a 'header file'. With an interface, you provide two files, one is the .h extension file, which is the definitions, function prototypes, constants, data (if necessary), newly declared types (enumerations etc), and then you implement it seperately in a .cpp file. client.cpp file Uses the lib.h file to get definitions from the library. lib.h The interface contains only the information about the library that the clients need to know. lib.cpp The implementation contains the code to make the library work, the details of which are no use to clients. EXAMPLE - Random Number Interface Used for the simulation of random numbers, e.g. rolling a die or flipping a coin. Computers operate deterministically, meaning if you run it with the same data, you'll always get the same result. Therefore, adding random behavior requires extra complex operations. For this example, we want to hide the complexity of random number generation and provide a simplistic interface for clients to use. view code in lecture slides, slides 8 onwards In the code, you could see #ifndef _random_h and #define _random_h at the start of the header file. You also saw #endif at the end. #define defines the name of the interface and #ifndef states “if not defined:”, essentially an if statement regarding definitions. The conventional syntax for naming an interface is _interfacename_h. #ifndef causes the compiler to skip all code to #endif if the interface name has already been defined, but if it hasn’t been defined, will then define the interface. Libraries There are a number of libraries that can be used in C++, including C libraries, which can be found at www.cplusplus.com. is a main library, which we've used recently that allows you to write to console and take input from users. In though, any time a white space character (e.g. space) is read, it terminates, so if you were putting two names in e.g. “John Smith”, it would only “John”. Therefore, to read an entire line, you must use a function in the iostream library called getline(stream, string variable), e.g. getline(cin, savestring) to save the cin input to the string variable savestring. There is a library for file input and output called fstream (file stream). To use it, you must declare a stream variable, such as ifstream varname (ifstream standing for input filestream) and ofstream varname (standing for output file stream). You can then open files using ifstreamname.open("data.txt");, or create a new file using ofstreamname.open("copy.txt"). The problem with fstream is that the 'open' function expects a C style string, not C++ style. If you have the C++ string variable named 'filename' you can use infile.open(filename.c_str()); which uses c_str() function to convert filename to a C string. You can never presume a file exists on a system, so fstream and iostream provide a fail method. if (infile.fail()) { cout << “Could not open file.”; } Once it's open, you can transfer data by using 'infile >> varname;' or you can even do 'getline(infile, str)' for whole line input. You can use 'outfile < with T being a type name, and sometimes even . When you declare a container, you have to include to let the container know what type of data it will hold. Vectors Vectors are sequence containers. This means it's an array which can grow when it needs to. To use a vector you have to '#include ' to include the vector library, and then declare the variable with 'vector varname'. Vectors dynamically change array sizes, and it's automatically handled by the vector. Elements are stored one after the other still, and can still be accessed by using varname[n]. The capacity will be larger than the size of all the elements in the array, which causes a space - time trade off. When it expands, it doesn't simply add a few extra, i
More Less

Related notes for INB221

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit