C++Reference Card Looping Pointers C++Data Types Data Type coutnd do-whil Loop Example do orchango th Operators biLe (expresston);hle (x1) oan:升s日 T0gnyeoeapossdpomes oI=(notequal-o】 for (;test;update) nd) statement 》 as5 signment0.*=,/=,=,+=,-= []: ett
C++ Reference Card C++ Data Types Data Type Description bool boolean (true or false) char character ('a', 'b', etc.) char[] character array (C-style string if null terminated) string C++ string (from the STL) int integer (1, 2, -1, 1000, etc.) long int long integer float single precision floating point double double precision floating point These are the most commonly used types; this is not a complete list. Operators The most commonly used operators in order of precedence: 1 ++ (post-increment), -- (post-decrement) 2 ! (not), ++ (pre-increment), -- (pre-decrement) 3 *, /, % (modulus) 4 +, - 5 , >= 6 == (equal-to), != (not-equal-to) 7 && (and) 8 || (or) 9 = (assignment), *=, /=, %=, +=, -= Console Input/Output cout > console in, reading from keyboard cerr > i; cout > inputVariable; // you can also use get (char) or // getline (entire line) in addition to >> ... inputFile.close(); Example (output): ofstream outFile; outfile.open("output.txt"); outFile element=val; hPtr->patties = 1; hPtr->cheese = false; Structures can be used just like the built-in data types in arrays
Classes Inheritance Exceptions nce allows dass it is bas getArea() public: type otdtgen,strineidn: Pbtmembesaieacst Eatch (int) rom anywhere the class is 1。 um can not be negative! essible fom the same clas class Gradstudent:public Student atch(cha exceptionstring) r(): string prevDegree add more catch blocks as needed Function Templates Base stotements; if (ab)a idtha Operator Overloading oid square:(o) t elseit(-1) ag65.e37,时 floatSquare::getArea() (const Square Class Templates fheotiecthatreei es the function call is not an ins 2oeamcaa 8 ce(PointT>p)i r(): priva Example d function Pi( he class template coutgetArea() 2.50 Suggested Websites C+Reference: http:// http://www.informit.com/guides/guide.aspx?gecplusplus C++Tutorialt: http://.cplusplus.com/doc/tutorial ttp://.sparknotes.com/cs/ C++Exan ple nttp://ww.fredosaurus.com/notes-cpp/
Developed for Mississippi State University's CSE1284 and CSE1384 courses February 17, 2009 Download the latest version from http://cse.msstate.edu/~crumpton/reference Classes Declaration Example class classname class Square { { public: public: classname(params); Square(); ~classname(); Square(float w); type member1; void setWidth(float w); type member2; float getArea(); protected: private: type member3; float width; private: }; type member4; }; public members are accessible from anywhere the class is visible. private members are only accessible from the same class or a friend (function or class). protected members are accessible from the same class, derived classes, or a friend (function or class). constructors may be overloaded just like any other function. You can define two or more constructors as long as each constructor has a different parameter list. Definition of Member Functions return_type classname::functionName(params) { statements; } Examples Square::Square() { width = 0; } void Square::setWidth(float w) { if (w >= 0) width = w; else exit(-1); } float Square::getArea() { return width*width; } Definition of Instances Example classname varName; Square s1(); Square s2(3.5); classname* ptrName; Square* sPtr; sPtr=new Square(1.8); Accessing Members Example varName.member=val; s1.setWidth(1.5); varName.member(); cout member=val; coutgetArea(); ptrName->member(); Inheritance Inheritance allows a new class to be based on an existing class. The new class inherits all the member variables and functions (except the constructors and destructor) of the class it is based on. Example class Student { public: Student(string n, string id); void print(); protected: string name; string netID; }; class GradStudent : public Student { public: GradStudent(string n, string id, string prev); void print(); protected: string prevDegree; }; Visibility of Members after Inheritance Inheritance Access Specifier in Base Class Specification private protected public private - private private protected - protected protected public - protected public Operator Overloading C++ allows you to define how standard operators (+, -, *, etc.) work with classes that you write. For example, to use the operator + with your class, you would write a function named operator+ for your class. Example Prototype for a function that overloads + for the Square class: Square operator+ (const Square &); If the object that receives the function call is not an instance of a class that you wrote, write the function as a friend of your class. This is standard practice for overloading >. Example Prototype for a function that overloads , ==, etc.) should be bool, the return type of T getMax(T a, T b) { if (a>b) return a; else return b; } // example calls to the function template int a=9, b=2, c; c = getMax(a, b); float f=5.3, g=9.7, h; h = getMax(f, g); Class Templates Example template class Point { public: Point(T x, T y); void print(); double distance(Point p); private: T x; T y; }; // examples using the class template Point p1(3, 2); Point p2(3.5, 2.5); p1.print(); p2.print(); Suggested Websites C++ Reference: http://www.cppreference.com/ http://www.informit.com/guides/guide.aspx?g=cplusplus C++ Tutorial: http://www.cplusplus.com/doc/tutorial/ http://www.sparknotes.com/cs/ C++ Examples: http://www.fredosaurus.com/notes-cpp/ Gaddis Textbook: Video Notes http://media.pearsoncmg.com/aw/aw_gaddis_sowcso_6/videos Source Code ftp://ftp.aw.com/cseng/authors/gaddis/CCSO5 (5th edition)