Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ CSCE 343.

Similar presentations


Presentation on theme: "C++ CSCE 343."— Presentation transcript:

1 C++ CSCE 343

2 C vs. C++ C is (more or less) a subset of C++
main() is starting point, same as C Some important differences: Stream insertion operator ‘<<‘ preferred method for output (as opposed to printf) Stream extraction operator ‘>>’ preferred method for input (as opposed to scanf) I/O has object based mechanism Library header files do not have .h extension. New feature: references! Support for classes, inheritance, etc. (OO features) Built-in string class. Support for data-structures through the STL (Standard Template Library)

3 Namespaces Purpose: avoid conflicting names in various contexts.
Scope resolution operator ‘::’ <namespace>::<member> std::cout std::string To avoid having to type the whole name (including namespace) use the using command: using std::cout; using std::string;

4 Basic Console Output Use the output stream cout
#include<iostream> using std::cout; using std::endl; int main( int argc, char *argv[] ) { cout << “Hello World!” << endl; } Example code hello.cpp

5 The string class Constructing a string object: Example code string.cpp
string s1; //create empty string object string s2(“cat”); string s3 = “cat”; Example code string.cpp write code to read and print a string

6 Objects Creating an object is the same as declaring it!
The new operator is used in a slightly different context (subject for later chapters). For now, avoid using the new operator. Examples: string stg; // creates the empty string string myString(“Mickey”); Person myPerson(“Fred”, 22);

7 Classes Separate interface from implementation
Interface goes into .h file. Use the preprocessor wrapper to avoid multiple inclusions: #ifndef MYCLASS_H #define MYCLASS_H … #endif Implementation goes into .cpp file. Include the interface file. Any users of the class must include the interface file (.h file). Time example Time.h, Time.cpp, TimeTest.cpp g++ TimeTest.cpp Time.cpp

8 More Object Creation Using class Time:
Time sunset; reference to a Time object Time arrayOfTimes[5]; array of Time objects Time &dinnerTime = sunset; reference to same Time object as sunset Time *timePtr = &sunset; pointer to a Time object

9 C++ File I/O #include <fstream> Utilizes the stream classes
std::ofstream std::ifstream Create file reference and open a file: ofstream outName(“file.txt”, ios::out ); ifstream inName( “back.txt”, ios::in ); ifstream inName; if.open(“newFile.txt”); Check for success opening the file if( !outName ) { … }

10 File Reading Read one character: Read one line of text into char[]:
ifstream fin( "temp.txt" ); while( fin.get(ch) ) cout << ch; fin.close(); Read one line of text into char[]: ifstream fin("tmp.dat");   int MAX_LENGTH = 100; char line[MAX_LENGTH];   while( fin.getline(line, MAX_LENGTH) ) { cout << "read line: " << line << endl; } Close file fin.close() Example fileio.cpp and fileio2.cpp

11 More I/O Read one of text into a string Read a string from keyboard
ifstream fin(“data.txt”); string s; while (getline( fin, s )) cout << s << endl; Read a string from keyboard getline( cin, s ); cout << "You entered " << s << endl;

12 Math Library To use math functions (like sqrt, sin, cos, etc.)
#include <cmath> Functions are in std namespace: std::sqrt std::sin std::cos

13 In Class Exercise Create a C++ program that includes a class that represents a 2D point (x,y) coordinates. Make x and y private. Add getters and setters for x and y Make two constructors, one empty, and one that takes 2 parameters (inits for x and y). Add the following methods: double distFromOrigin() (calculates and returns the distance that this point is from the origin) string toString() (returns a string that represents the point, e.g. (2, 4.35) ) Write a test program that will test your class thoroughly. Submit your code using class submission program.

14 Standard Template Library
Provides a set of templated (generic) classes for data structures. More on templates later We’ll focus on the map data structure. #include <vector> #include <map> typedef std::vector<string> VectorType; typedef std::map<string, Person> MapType;

15 STL Maps MapType myMap; Person p1; p1.name = “Joe Schmoe”;
p1.age = 42; myMap[“joe”] = p1;

16 Iterators Used to traverse STL structures
MapType::iterator iter; Typical loop (over all elements): for( iter = myMap.begin() ; iter != myMap.end(); ++iter ) { //process data }

17 Map Example simpleVectorExample simpleMapExample Person database
creates separate .h and .cpp files example of overloaded stream insertion operator << reads person data from file people.txt

18 In-class Map / Vector Create a C++ class to hold <movie, movie stars> information create separate .h, .cpp, and testMovie files Data fields: private map<key,data> key of type string data of type vector of strings. public methods: add (movieName, MovieStar) printMovieInfo Write a test program that creates a Movie DB with this information Tiger : Lemmon, Matthau, Spacey Sin City: Lewis, De Niro, Alba Mean Girls: Fey, Lemon


Download ppt "C++ CSCE 343."

Similar presentations


Ads by Google