1 / 14

EECS 352 Data Structures and Algorithms

EECS 352 Data Structures and Algorithms. José M. Vidal Office: 3A47. C++ Review. Data types Input/Output Classes Data abstraction (public/private) Constructors/Destructors Operator Overloading Inheritance. C++ Data Types. char 1 byte short 2 bytes int 4 bytes long 4 bytes

zarek
Download Presentation

EECS 352 Data Structures and Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EECS 352Data Structures and Algorithms José M. Vidal Office: 3A47 EECE 352

  2. C++ Review • Data types • Input/Output • Classes • Data abstraction (public/private) • Constructors/Destructors • Operator Overloading • Inheritance EECE 352

  3. C++ Data Types • char 1 byte • short 2 bytes • int 4 bytes • long 4 bytes • float 4 bytes • double 8 bytes • bool 1 byte • Signed and unsigned keywords can be used with char, short, int and long. • Variations are possible: long double, long long, signed char, unsigned long... EECE 352

  4. Output #include<iostream.> Tell compiler that we are doing I/O cout Object to which we can send data. << Device for sending data. endl `\n’ `\t’ Special symbols that we can send. EECE 352

  5. Formatting Output cout.setf(long flag) ios::left left justify the output ios::right right justify the output ios::scientific use scientific notation for numbers ios::hex print numbers in hexadecimal base ios::dec print numbers in decimal base ios::uppercase print all characters in upper case cout.unsetf(long flag) Set different formatting parameters for next output. Disable these formatting parameters. EECE 352

  6. Example Code #include<iostream.h> main() { cout.width(10); //sets width to 10 cout << “hello” << endl; cout.setf(ios::left); cout << “hello” << endl; cout << 16 << endl; cout.setf(ios::hex); cout << 16 << endl; } Output hello hello 16 10 EECE 352

  7. Input #include <iostream.h> Tell the linker we are doing basic I/O cin The input object. It retrieves input from the keyboard >> The extractor operator. EECE 352

  8. Example Code #include <iostream.h> main () { int userInput; cout << “Enter number:”; cin >> userInput; cout << “You entered ” << userInput << endl; } Output Enter number:12345 You entered 12345 EECE 352

  9. I/O From a File • I/O from a file is done in a similar way. #include <iostream.h> #include <fstream.h> main() { int inputNumber; ofstream myOutputFile(“outfile”); ifstream myInputFile(“infile”); myOutputFile << “text to file.” << endl; myInputFile >> inputNumber; } EECE 352

  10. Classes • Provide a mechanism for defining classes of objects. • We can define the class of all computers to have certain characteristics. • An instance of a computer is your home PC. • Classes contain member variables and member functions. EECE 352

  11. Classes and Objects class Mammals inherits inherits Humans class Tigers class instance-of instance-of Hank Peggy Tony EECE 352

  12. Example Class class human { // this data is private to instances of the class int height; char name[]; int weight; public: void setHeight(int heightValue); int getHeight(); } EECE 352

  13. Function Definitions void human::setHeight(int heightValue) { if (heightValue > 0) height = heightValue; else height = 0; } int human::getHeight() { return(height); } EECE 352

  14. Example Usage // first we define the variables. int height = 72; int result = 0; human hank; //set our human’s height hank.setHeight(height); //get his height result = hank.getHeight(); cout << “Hank is = “ << result << “inches tall” << endl; Output Hank is 72 inches tall EECE 352

More Related