140 likes | 218 Views
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
E N D
EECS 352Data Structures and Algorithms José M. Vidal Office: 3A47 EECE 352
C++ Review • Data types • Input/Output • Classes • Data abstraction (public/private) • Constructors/Destructors • Operator Overloading • Inheritance EECE 352
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
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
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
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
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
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
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
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
Classes and Objects class Mammals inherits inherits Humans class Tigers class instance-of instance-of Hank Peggy Tony EECE 352
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
Function Definitions void human::setHeight(int heightValue) { if (heightValue > 0) height = heightValue; else height = 0; } int human::getHeight() { return(height); } EECE 352
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