C++ Syntax
//C++ Comments:
- //Single-Line Comments...
- //Multi-Line Comments...
- Read More...
//C++ Header Files:
- //Including External Files
- #include <cstdlib>
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- #include <ofstream>
- #include <ifstream>
- #include <cmath>
- #include <vector>
- #include <algorithm>
- Read More...
- //Using ifndef...endif Structure
- #ifndef MYHEADER_H
- #define MYHEADER_H
- #endif
- #include “myHeader.h”
- Read More...
//C++ Data Types & Variables:
- //Data Types usable in Variables & Function Declarations:
| Name | Description | Value Range |
| bool | Boolean (True/False) | true or false |
| char | Single Character (Letters) | signed: -128 to 127 unsigned: 0 to 255 |
| int | Integer (Whole Numbers) | signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 |
| float | Floating Point Number (Decimal #'s w/ 7 digits) | minimum: 1.175494351 E -38 maximum: 3.402823466 E +38 |
| double | Double Precision Floating Pt (Decimal #'s w/ 16 digits) | minimum: 2.2250738585072014 E -308 maximum: 1.7976931348623158 E +308 |
| string | String of Characters (Like a Sentance) | Lots of “Letters” |
- //Declaring & Initializing basic Variables...
- bool isTrue = true;
- char aLetter = ‘Y’;
- int intNum = 5;
- float floatNum = 5.125;
- double doubleNum = 2.2250738585072014;
- string sentance = “whatever”;
- int intNum1 = 5, intNum2 = 10;
- Read More...
//C++ Special Characters (used in Strings):
- //Special Characters usable in strings:
| Symbol | Name | Description |
| “ \n ” | Newline | Creates a new line character or carriage return. |
| “ \t ” | Horizontal Tab | Creates a horizontal tab space. |
| “ \v ” | Vertical Tab | Creates a vertical tab space. |
| “ \b ” | Backspace | Deletes back one space like a backspace. |
| “ \\ ” | Backslash | Creates a backslash character. |
| “ \? ” | Question Mark | Creates a question mark. |
| “ \' ” | Single Quote | Creates a single quotation mark. |
| “ \" ” | Double Quote | Creates a double quotation mark. |
| “ \0 ” | Null | Creates a NULL character. |
//C++ Operators:
| Symbol | Name | Example |
| = | Assignment | varName = 5; |
| + | Concatenation | varName = “My name is ” + “Terrance.”
|
| Symbol | Name | Example |
| << | Stream Insertion | cout << 5; |
| >> | Stream Extraction | cin >> varName; |
| Symbol | Name | Example |
| + | Addition | aNum = 1 + 2; |
| - | Subtraction | aNum = 3 - 2; |
| * | Multiplication | aNum = 2 * 3; |
| / | Division | aNum = 13 / 5; |
| % | Modulo | aNum = 13 % 5; |
| Symbol | Name | Example |
| += | Plus Equals | aNum += 4; |
| += | Concatenation Equals | aString += “ more letters.”;
|
| -= | Minus Equals | aNum -= 4; |
| *= | Multiply Equals | aNum *= 4; |
| /= | Divid Equals | aNum /= 4; |
| %= | Modulo Equals | aNum %= 4; |
| -- | Decrement | aNum--; |
| ++ | Increment | aNum++; |
| ++var | Pre-Increment | myNum = ++urNum;
|
| var++ | Post-Increment | myNum = urNum++;
|
| --var | Pre-Decrement | myNum = --urNum;
|
| var-- | Post-Decrement | myNum = urNum--;
|
| Symbol | Name | Example (Used in expressions) |
| == | Equal to | (a == b) |
| != | Not Equal to | (a != b) |
| > | Greater than | (a > b) |
| < | Less than | (a < b) |
| >= | Greater than or equal to | (a >= b)
|
| <= | Less than or equal to | (a <= b)
|
| Symbol | Name | Example (Used in expressions) |
| ! | Not | (!true) |
| && | And | (a == b && b > c) |
| || | Or | (a == b || b > c) |
| Symbol | Name | Example (Used with binary values) |
| & | And | aNum = 11 & 5;
|
| | | Inclusive Or | aNum = 12 | 9;
|
| ^ | Exclusive Or | aNum = 12 ^ 9;
|
| << | Left-Shift | aNum = 16 << 4;
|
| >> | Right-Shift | aNum = 16 >> 2;
|
//C++ Arrays:
- //Declaring & Initializing Single-Dimensional Arrays...
- dataType varName[integerValue] = {value, value, ...};
- //example...
- int anArray[3] = {0};
- int anArray2[3] = {1, 2};
- int myArray[5] = {0, 1, 2, 3, 4};
- cout << myArray[0];
- myArray[3] = 25;
- Read More...
- //Declaring & Initializing Multi-Dimensional Arrays...
- dataType varName[integerValue][integerValue] ... = {value, value, ...};
- //example...
- int myArray[3][5] = {0};
- int myArray2[3][5] = {1, 2};
- int anArray[3][5] = { 0, 1, 2, 3, 4,
- 5, 6, 7, 8, 9,
- 10, 11, 12, 13, 14 };
- for(int i = 0; i < 3; i++) {
- for(int f = 0; f < 5; f++) {
- cout << anArray[i][f] << “, ”;
- }
-
- cout << endl;
- }
- bool anotherArray[3][5];
- for(int i = 0; i < 3; i++) {
- for(int f = 0; f < 5; f++) {
- anotherArray[i][f] = false;
- }
- }
- Read More...
//C++ Decision Structures:
- //Using If/Else If/Else Statements...
- if(expressionIsTrue) {
- }
- else if(expressionIsTrue) {
- }
- else {
- }
- //example...
- int varName = 0;
- if(varName == 1) {
- }
- else if(varName == 2) {
- }
- else {
- }
- Read More...
- //Using Switch Statements...
- switch(varName) {
- case value:
- break;
- case value:
- break;
- default:
- }
- //example...
- srand(time(NULL));
- int i = rand() % 100;
- switch(i) {
-
- case 0:
-
- break;
- case 1:
-
- break;
- default:
-
- }
- Read More...
//C++ Looping Structures:
- //Using While Loops...
- while(expressionIsTrue) {
- }
- //example...
- while(varName < 10) {
-
- }
- Read More...
- //Using Do...While Loops...
- do {
- }while(expressionIsTrue);
- //example...
- do {
-
- }while(varName < 10);
- Read More...
- //Using For Loops...
- for(int indexVarName = value; indexVarName expressionWhileTrue; indexVarName loopIncrementation) {
- }
- //example...
- for(int i = 0; i < 10; i++) {
-
- }
- Read More...
//C++ Pointers & Referencing:
- //Declaring & Initializing Pointers...
- dataType *varName = memoryAddress;
- //example...
- //Assigning Memory Addresses to Pointers...
- int intVariable = 75;
- int *intPointer;
- *intPointer = intVariable;
- intPointer = &intVariable;
- int *intPointer = &intVariable;
- //Extracting data from Pointers...
- cout << intPointer << endl;
- cout << &intPointer << endl;
- cout << *intPointer << endl;
- //Using Values stored at Pointer locations...
- *intPointer = 85;
- int aNumber = *intPointer;
- //Pointers are similar to arrays - an index can access adjacent values...
- cout << intPointer[0] << endl;
- cout << intPointer[1] << endl;
- cout << &intPointer[0] << endl;
- cout << &intPointer[1] << endl;
- //Dynamically Allocating Memory with Pointers...
- int *intPointer;
- intPointer = new int;
- intPointer = new int[10];
- //You can also Declare and Initialize a pointer in the same line...
- bool *boolPointer = new bool[10];
- char *charPointer = new char[10];
- int *intPointer = new int[10];
- float *floatPointer = new float[10];
- double *doublePointer = new double[10];
- string *stringPointer = new string[10];
- //Practical Example...
- using namespace std;
- int main(int argc, char *argv[]) {
- int size = 0;
- char tempChar[100] = “\0”;
- cout << “Please enter the number of names for the list: ”;
- cin >> size;
- cin.ignore();
- cout << endl;
- string *stringList = new string[size];
- for(int i = 0; i < size; i++) {
- cout << “Please enter a name: ”;
- cin.getline(tempChar, 100);
- stringList[i] = tempChar;
- }
- cout << “\nThe names are:\n”;
- for(int i = 0; i < size; i++) {
- cout << stringList[i] << endl;
- }
- cout << endl;
- system(“PAUSE”);
- return EXIT_SUCCESS;
- }
- Read More...
//C++ Functions:
- //returnDataType functionName(dataTypesOfParameters);
- //Syntax for Function Prototypes...
- returnDataType functionName(dataType, dataType, ...);
- //example...
- int getHours(int, bool);
- Read More...
- //returnDataType functionName(dataTypesOfParameters varNames) { }
- //Syntax for Function Definitions...
- returnDataType functionName(dataType varName, dataType varName, ...) {
- return value;
- }
- //example...
- int getHours(int aNumber, bool isTrue) {
-
- return 5;
- }
- Read More...
- //varName = functionName( parameterValues );
- //Syntax for Calling Functions...
- returnDataType = functionName(varName, varName, ...);
- //example...
- int totalHours = 0;
- totalHours = getHours(15, true);
- Read More...
- //friend returnDataType functionName(dataTypesOfParameters);
- //Friend Functions...
- //returnDataType functionName(dataTypesOfParameters);
- //Overloading Functions...
- returnDataType overloadedFunctionName(dataTypeOfParameter)
- returnDataType overloadedFunctionName(dataTypeOfParameter, dataTypeOfParameter)
- //example...
- //Valid Overloaded Function Prototypes...
- int overloadMe();
- int overloadMe(bool);
- int overloadMe(int);
- int overloadMe(int, int);
- //Valid Overloaded Function Definitions...
- int overloadMe() {
-
- return integerValue;
- }
- int overloadMe(bool varName) {
-
- return integerValue;
- }
- int overloadMe(int varName) {
-
- return integerValue;
- }
- int overloadMe(int varName, int varName) {
-
- return integerValue;
- }
- //Calling Valid Overloaded Functions...
- int num1 = 0, num2 = 0, num3 = 0, num4 = 0;
- bool isTrue = false;
- num1 = overloadMe();
- num1 = overloadMe(isTrue);
- num1 = overloadMe(num2);
- num1 = overloadMe(num3, num4);
- Read More...
//C++ Classes:
- //class className { public: protected: private: };
- //Declaring a Class with Member Function Prototypes...
- class className {
- public:
- protected:
- private:
- };
- //example...
- class addClass {
-
- public:
-
-
- addClass();
- ~addClass();
- int addNum(int, int);
- int getNumOne();
- protected:
-
-
- private:
-
-
- int num1;
- int num2;
- };
- Read More...
- //returnType className::functionName( dataTypesOfParameters varNames) { }
- //Inline Class Member Function Definitions...
- inline returnDataType className::functionName(dataType varName, dataType varName, ...) {
- return value;
- }
- //example...
- inline int addClass::getNumOne() {
-
- return num1;
- }
- //Non-Inline Class Member Function Definitions...
- returnDataType className::functionName(dataType varName, dataType varName, ...) {
- return value;
- }
- //example...
- int addClass::addNum(int num1, int num2) {
- int total = num1 + num2;
- return total;
- }
- Read More...
- //classInstanceName.functionName(varNames);
- //Calling Class Member Functions...
- varName = classInstanceName.functionName();
- //example...
- int main() {
- int num1 = 1, num2 = 2, total = 0;
- addClass myClass;
- total = myClass.addNum(num1, num2);
- return 0;
- }
- cout << myClass.num1;
- Read More...
- //class className : inheritanceType otherClassName { public: prototype: private: };
- //Class Inheritance...
- class className : inheritanceType otherClassName {
- public:
- protected:
- private:
- };
- //example...
- //Parent class “soldier” in soldierClass.h file...
- class soldier {
- public:
- soldier();
- soldier(soldier&);
- soldier(int, int, int);
- ~soldier();
- int getHitPoints();
- int getAccuracy();
- int getJogSpeed();
- void setHitPoints(int);
- void setAccuracy(int);
- void setJobSpeed(int);
- protected:
-
- int hitPoints;
- int accuracy;
- int jogSpeed;
-
- private:
- };
- //Child class “sniper” in sniperClass.h file...
- class sniper : private soldier {
- public:
- sniper();
- sniper(sniper&);
- sniper(int, int, int);
- ~sniper();
- string getAbility();
- int getZeroBloomTime();
- void setAbliity(string);
- void setZeroBloomTime(int);
- protected:
- private:
- string ability;
- int zeroBloomTime;
- };
- Read More...
- //friend returnDataType functionName(dataTypesOfParameters);
- //Friend Functions to a Class...
- class className {
- public:
- friend returnDataType functionName();
- protected:
- private:
- };
- //example...
- class myClass {
- public:
- friend myClass Calculations(int);
- myClass();
- protected:
- private:
- int aNumber;
- };
- myClass Calculations(int anotherNumber) {
- myClass tempClass;
-
- tempClass.aNumber *= anotherNumber;
- return tempClass;
- }
- int calc = 6;
- myClass aClass;
- aClass = Calculations(calc);
- Read More...
- //Example shapeRec Class Declaration...
- using namespace std;
- class shapeRec {
-
- friend ostream& operator<<(ostream&, const shapeRec&);
- friend istream& operator>>(istream&, shapeRec&);
-
- friend shapeRec operator+(const shapeRec&, const shapeRec&);
- friend shapeRec operator-(const shapeRec&, const shapeRec&);
- friend shapeRec operator*(const shapeRec&, const shapeRec&);
- friend shapeRec operator/(const shapeRec&, const shapeRec&);
-
-
-
- friend shapeRec operator++(shapeRec&);
- friend shapeRec operator++(shapeRec&, int);
- friend shapeRec operator--(shapeRec&);
- friend shapeRec operator--(shapeRec&, int);
-
- friend bool operator==(const shapeRec&, const shapeRec&);
- friend bool operator!=(const shapeRec&, const shapeRec&);
- friend bool operator<=(const shapeRec&, const shapeRec&);
- friend bool operator<(const shapeRec&, const shapeRec&);
- friend bool operator>=(const shapeRec&, const shapeRec&);
- friend bool operator>(const shapeRec&, const shapeRec&);
- public:
-
- shapeRec();
- shapeRec(double, double);
- shapeRec(shapeRec&);
-
- ~shapeRec();
-
- double getLength() const;
- double getWidth() const;
-
- void setDimensions(double, double);
-
- double calcPerimeter() const;
- double calcArea() const;
- private:
-
- double length;
- double width;
- };
- //Overloaded Stream Operators...
- ostream& operator<<(ostream& varName, const className& varName) {
- return varName;
- }
- //example...
- friend ostream& operator<<(ostream&, const shapeRec&);
- ostream& operator<<(ostream& osObj, const shapeRec& myRec) {
- osObj << fixed << setprecision(2);
- osObj << “Length = ” << myRec.length << endl;
- osObj << “Width = ” << myRec.width << endl;
- osObj << “Perimeter = ” << myRec.calcPerimeter() << endl;
- osObj << “Area = ” << myRec.calcArea() << endl;
- return osObj;
- }
- int main() {
- shapeRec aRec;
- cout << aRec;
- }
- istream& operator>>(istream& varName, shapeRec& varName) {
- return varName;
- }
- //example...
- friend istream& operator>>(istream&, shapeRec&);
- istream& operator>>(istream& isObj, shapeRec& myRec) {
- double tempLength = 0.0;
- double tempWidth = 0.0;
- char temp = '\0';
- cout << “Please enter length and width (1.0,1.0):\n”;
- isObj >> tempLength >> temp >> tempWidth;
- myRec.setDimensions(tempLength, tempWidth);
- return isObj;
- }
- int main() {
- shapeRec aRec;
- cin >> aRec;
- }
- //Overloaded Arithmetic Operators...
- //example...
- //example...
- //example...
- //example...
- //example...
- //Overloaded Assignment Operator...
- //example...
- //Overloaded Compound-Assignment Operators...
- //example...
- //example...
- //example...
- //example...
- //example...
- //Overloaded Increment & Decrement Operators...
- //example...
- //example...
- //example...
- //example...
- //Overloaded Relational Operators...
- //example...
- //example...
- //example...
- //example...
- //example...
- //example...
- Read More...
- //className();
- //className(classNameReference);
- //className(dataTypesOfParameters);
- //~className();
- //COCF Class Prototypes...
- class className {
- public:
- className();
- className(className&);
- className(dataType, dataType, ...);
- ~className();
- protected:
- private:
- dataType varName;
- };
- //example...
- class myClass {
-
- public:
-
-
- //Default Constructor Prototype...
- myClass();
- //Copy Constructor Prototype...
- myClass(myClass&);
- //Working Constructor Prototype...
-
- myClass(int, int, int);
- //Default Destructor Prototype...
- ~myClass();
-
- int getANum();
- int getAotherNum();
- void setANum(int);
- void setAnotherNum(int);
- protected:
-
-
- private:
-
-
- int aNum;
- int anotherNum;
- int *anArray;
- };
- Read More...
- //className::className(){ }
- //className::className(className classInstanceNameReference){ }
- //className::className(dataTypesOfParameters varNames){ }
- //className::~className(){ }
- //COCF Class Definitions...
- //Default Constructor Definition...
- className::className() {
- }
- //example...
- myClass::myClass() {
- aNum = 0;
- anotherNum = 0;
-
- anArray = new int[10];
- }
- //Copy Constructor Definition...
- className::className(className classInstanceName&) {
- }
- //example...
- myClass::myClass(myClass anotherClass&) {
-
- aNum = anotherClass.aNum;
- anotherNum = anotherClass.anotherNum;
- anArray = anotherClass.anArray;
- }
- //Working Constructor Definition...
- className::className(dataType varName, dataType varName, ...) {
- }
- //example...
- myClass::myClass(int temp1, int temp2, int temp3) {
-
- aNum = temp1;
- anotherNum = temp2;
-
- anArray = new int[temp3];
- }
- //Default Destructor Definition...
- inline className::~className() {
- delete varName;
- }
- //example...
- inline myClass::~myClass() {
-
- delete anArray;
- }
- Read More...
- //className classInstanceName;
- //className classInstanceName(classInstanceName);
- //className classInstanceName(varNames);
- //Calling COCF Classes...
- //Calling a Default Constructor...
- className classInstanceName;
- //example...
- int main() {
- myClass aClass;
- return 0;
- }
- //Calling a Copy Constructor...
- className classInstanceName(classInstanceName);
- //example...
- int main() {
- myClass aClass;
- myClass anotherClass(aClass);
- return 0;
- }
- //Calling a Working Constructor...
- className classInstanceName(varNames);
- //example...
- int main() {
- int num1 = 5, num2 = 15, num3 = 10;
- myClass aClass(num1, num2, num3);
- return 0;
- }
- //Calling a Default Destructor...
- Read More...