

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


cout << "Enter an integer: "; cin >> i; cout << "Input: " << i << endl;
ifstream inputFile; inputFile.open("data.txt"); inputFile >> inputVariable; // you can also use get (char) or // getline (entire line) in addition to >> ... inputFile.close();
ofstream outFile; outfile.open("output.txt"); outFile << outputVariable; ... outFile.close();
if (expression) if (x < y) statement; cout << x;
if (expression) if (x < y) statement; cout << x; else else statement; cout << y;
switch(int expression) switch(choice) { { case int-constant: case 0: statement(s); cout << "Zero"; break; break; case int-constant: case 1: statement(s); cout << "One"; break; break; default: default: statement; cout << "What?"; } }
while (expression) while (x < 100) statement; cout << x++ << endl;
while (expression) while (x < 100) { { statement; cout << x << endl; statement; x++; } }
do do statement; cout << x++ << endl; while (expression); while (x < 100);
do do { { statement; cout << x << endl; statement; x++; } } while (expression); while (x < 100);
for (initialization; test; update) statement;
for (initialization; test; update) { statement; statement; }
for (count = 0; count < 10; count++) { cout << "count equals: "; cout << count << endl; }
return_type function(type p1, type p2, ...) { statement; statement; ... }
int timesTwo(int v) { int d; d = v * 2; return d; }
void printCourseNumber() { cout << "CSE1284" << endl; return; }
return_type function(type p1) Variable is passed into the function but changes to p1 are not passed back.
return_type function(type &p1) Variable is passed into the function and changes to p1 are passed back.
return_type function(type p1=val) val is used as the value of p1 if the function is called without a parameter.
char c = 'a'; char* cPtr; cPtr = &c;
// continued from example above *cPtr = 'b'; cout << *cPtr << endl; // prints the char b cout << c << endl; // prints the char b
int numbers[]={10, 20, 30, 40, 50}; int* numPtr = numbers; cout << numbers[0] << endl; // prints 10 cout << *numPtr << endl; // prints 10 cout << numbers[1] << endl; // prints 20 cout << *(numPtr + 1) << endl; // prints 20 cout << numPtr[2] << endl; // prints 30
ptr = new type; int* iPtr; iPtr = new int;
ptr = new type[size]; int* intArray; intArray = new int[5];
delete ptr; delete iPtr; delete [] ptr; delete [] intArray;
int* intArray; intArray = new int[5]; intArray[0] = 23; intArray[1] = 32;
struct name struct Hamburger { { type1 element1; int patties; type2 element2; bool cheese; }; };
name varName; Hamburger h;
name* ptrName; Hamburger* hPtr; hPtr = &h;
varName.element=val; h.patties = 2; h.cheese = true;
ptrName->element=val; hPtr->patties = 1; hPtr->cheese = false;
Classes
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; };
return_type classname::functionName(params) { statements; }
Square::Square() { width = 0; }
void Square::setWidth(float w) { if (w >= 0) width = w; else exit(-1); }
float Square::getArea() { return width*width; }
classname varName; Square s1(); Square s2(3.5);
classname* ptrName; Square* sPtr; sPtr=new Square(1.8);
varName.member=val; s1.setWidth(1.5); varName.member(); cout << s.getArea();
ptrName->member=val; cout<<sPtr->getArea(); ptrName->member();
Inheritance
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; };
Operator Overloading
Square operator+ (const Square &);
friend ostream & operator<< (ostream &, const Square &);
Exceptions
try { // code here calls functions that might // throw exceptions quotient = divide(num1, num2);
// or this code might test and throw // exceptions directly if (num3 < 0) throw -1; // exception to be thrown can // be a value or an object } catch (int) { cout << "num3 can not be negative!"; exit(-1); } catch (char* exceptionString) { cout << exceptionString; exit(-2); } // add more catch blocks as needed
Function Templates
template
// 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
template
// examples using the class template Point
Suggested Websites