C++ Help

Operators
Comments
Define
Constants
Hello World
Declaring Variables
Escape Sequences
Printing on the Console
Creating Functions
Functions with Arguments
Built in Functions
Arrays
Reading from the Console
if else statements
string
if else statements
Files
The Address Operator
The Vector Class
Creating Objects
Creating Objects Dynamically
Some Simple Programs
Labs



(see https://www.tutorialspoint.com/cplusplus for more help)

===================
===================

Operators

===================
===================

Arithmetic Operators ( +, -, \, *, ++, --)

Relational Operators (==, !=, >. <, >=, <=)

Logical Operators (&&, ||, ! )

Bitwise Operators (& |, ^, ~, <<, >>)

Assignment Operators (=, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=)

Misc Operators ( sizeof, & cast, comma, conditional etc.)


===================
===================

Comments

===================
===================

/* This is a comment */
 
/* C++ comments can also
    span multiple lines
*/
 
 
========================
========================

The #define Preprocessor

========================
========================

#include
using namespace std;

#define LENGTH 10 // replace LENGTH with 10
#define WIDTH 5 // replace WIDTH with 5
#define NEWLINE '\n' // replace NEWLINE with '\n'

int main() {

    int area;
   
    area = LENGTH * WIDTH;
    cout << area;
    cout << NEWLINE;
    return 0;
}


===================
===================

Constants

===================
===================

#include
using namespace std;

int main() {
    const int LENGTH = 10;
    const int WIDTH = 5;
    const char NEWLINE = '\n';
    int area;
   
    area = LENGTH * WIDTH;
    cout << area;
    cout << NEWLINE;
    return 0;
}


===================
===================

Hello World

===================
===================


#include
using namespace std;

int main() {
    cout << "Hello World";
    return 0;
}

 
===================
===================

Declaring Variables

===================
===================

Meaning C++ Type
================================
Boolean bool
Character char
Integer int, short int, long int
Floating point float
Double floating point double
Valueless void


#include
using namespace std;

int main() {
    cout << "Size of char : " << sizeof(char) << endl;
    cout << "Size of int : " << sizeof(int) << endl;
    cout << "Size of short int : " << sizeof(short int) << endl;
    cout << "Size of long int : " << sizeof(long int) << endl;
    cout << "Size of float : " << sizeof(float) << endl;
    cout << "Size of double : " << sizeof(double) << endl;
    return 0;
}


Variable names can start with a letter or an underscore.


int x = 50;

double y;

string msg = "Hello World";


=======================
=======================

Escape Sequences

=======================
=======================

Escape sequence Meaning
=======================
\\ \ character
\' ' character
\" " character
\? ? character
\a Alert or bell
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
\ooo Octal number of one to three digits
\xhh Hexadecimal number of one or more digits



=======================
=======================

Printing on the Console

=======================
=======================

string name;

cout << "Please enter your name: ";
cin >> name;
cout << "Your name is: " << name << endl;



====================
====================

Creating Functions

====================
====================

return_type function_name( parameter list )
{
    body of the function
}


// function definition
int func() {
    return 0;
}


void func( void ) {
    cout << " Hello World " << endl;
}

                 
=================================
=================================

Functions with Arguments

=================================
=================================

Example:
--------
int max(int num1, int num2) {
    // local variable declaration
    int result;
 
    if (num1 > num2)
       result = num1;
    else
       result = num2;
 
    return result;
}

// calling the function
int retValue = max(a, b);


Example:
--------

#include
using namespace std;
 
int sum(int a, int b=20) {
    int result;

    result = a + b;
  
    return (result);
}

int main () {
    // local variable declaration:
    int a = 100;
    int b = 200;
    int result;
 
    // calling a function to add the values.
    result = sum(a, b);
    cout << "Total value is :" << result << endl;

    // calling a function again as follows.
    result = sum(a);
    cout << "Total value is :" << result << endl;
 
    return 0;
}




Example:
--------
                
                 

=================================
=================================

Built in Functions

=================================
=================================

Example:
--------
#include
#include
using namespace std;
 
int main () {
    // number definition:
    short s = 10;
    int i = -1000;
    long l = 100000;
    float f = 230.47;
    double d = 200.374;

    // mathematical operations;
    cout << "sin(d) :" << sin(d) << endl;
    cout << "abs(i) :" << abs(i) << endl;
    cout << "floor(d) :" << floor(d) << endl;
    cout << "sqrt(f) :" << sqrt(f) << endl;
    cout << "pow( d, 2) :" << pow(d, 2) << endl;
 
    return 0;
}


#include
#include
#include

using namespace std;
 
int main () {
    int i,j;
 
    // set the seed
    srand( (unsigned)time( NULL ) );

    /* generate 10 random numbers. */
    for( i = 0; i < 10; i++ ) {
       // generate actual random number
       j= rand();
       cout <<" Random Number : " << j << endl;
    }

    return 0;
}


Methods:
--------
                
Method Description
====== ===========
abs(x) Returns the absolute value of x
acos(x) Returns the arccosine of x, in radians
asin(x) Returns the arcsine of x, in radians
atan(x) Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians
ceil(x) Returns the value of x rounded up to its nearest integer
cos(x) Returns the cosine of x (x is in radians)
exp(x) Returns the value of Ex
floor(x) Returns the value of x rounded down to its nearest integer
log(x) Returns the natural logarithm (base E) of x
pow(x,y) Returns the value of x to the power of y
random() Returns a random number between 0 and 1
round(x) Returns the value of x rounded to its nearest integer
sin(x) Returns the sine of x (x is in radians)
sqrt(x) Returns the square root of x
tan(x) Returns the tangent of an angle


=================================
=================================

Arrays

=================================
=================================

Example:
--------

#include
using namespace std;
 
#include
using std::setw;
 
int main ()
{
    int n[ 10 ]; // n is an array of 10 integers
 
    // initialize elements of array n to 0
    for ( int i = 0; i < 10; i++ )
    {
       n[ i ] = i + 100; // set element at location i to i + 100
    }
    cout << "Element" << setw( 13 ) << "Value" << endl;
 
    // output each array element's value
    for ( int j = 0; j < 10; j++ )
    {
       cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl;
    }
 
    return 0;
}



=================================
=================================

Reading from the Console

=================================
=================================


#include
using namespace std;

int main() {
    cout << "Hello World";
    return 0;
}

    
=================================
=================================

if else statements

=================================
=================================

disc = b*b - 4*a*c;
if (disc > 0)
{

}
else if (disc == 0)
{


}
else
{

}



=================================
=================================

string

=================================
=================================

#include
#include
 
using namespace std;
 
int main ()
{
    string str1 = "Hello";
    string str2 = "World";
    string str3;
 
    // copy str1 into str3
    str3 = str1;
    cout << "str3 : " << str3 << endl;
 
    // concatenates str1 and str2
    str3 = str1 + str2;
    cout << "str1 + str2 : " << str3 << endl;
  
    return 0;
}



=================================
=================================

Reading from and Writing to files

=================================
=================================

#include
#include
using namespace std;
 
int main ()
{
    
    char data[100];
 
    // open a file in write mode.
    ofstream outfile;
    outfile.open("afile.dat");
 
    cout << "Writing to the file" << endl;
    cout << "Enter your name: ";
    cin.getline(data, 100);
 
    // write inputted data into the file.
    outfile << data << endl;
 
    cout << "Enter your age: ";
    cin >> data;
    cin.ignore();
   
    // again write inputted data into the file.
    outfile << data << endl;
 
    // close the opened file.
    outfile.close();
 
    // open a file in read mode.
    ifstream infile;
    infile.open("afile.dat");
 
    cout << "Reading from the file" << endl;
    infile >> data;
 
    // write the data at the screen.
    cout << data << endl;
   
    // again read the data from the file and display it.
    infile >> data;
    cout << data << endl;
 
    // close the opened file.
    infile.close();
 
    return 0;
}


=================================
=================================

The Address Operator &

=================================
=================================

#include

using namespace std;

int main () {
    int var1;
    char var2[10];

    cout << "Address of var1 variable: ";
    cout << &var1 << endl;

    cout << "Address of var2 variable: ";
    cout << &var2 << endl;

    return 0;
}


=================================
=================================

The Vector Class

=================================
=================================


#include
#include
using namespace std;
 
int main() {
    // create a vector to store int values
    vector vec;
    int i;

    // display the original size of vec
    cout << "vector size = " << vec.size() << endl;

    // push 5 values into the vector (added to the end)
    for(i = 0; i < 5; i++){
       vec.push_back(i);
    }

    // display extended size of vec
    cout << "extended vector size = " << vec.size() << endl;

    // access 5 values from the vector
    for(i = 0; i < 5; i++){
       cout << "value of vec [" << i << "] = " << vec[i] << endl;
    }

    return 0;
}


================
================

Creating Objects

================
================

#include

using namespace std;

class Box {
    public:
       double length; // Length of a box
       double breadth; // Breadth of a box
       double height; // Height of a box
};

int main( ) {
    Box Box1; // Declare Box1 of type Box
    Box Box2; // Declare Box2 of type Box
    double volume = 0.0; // Store the volume of a box here
 
    // box 1 specification
    Box1.height = 5.0;
    Box1.length = 6.0;
    Box1.breadth = 7.0;

    // box 2 specification
    Box2.height = 10.0;
    Box2.length = 12.0;
    Box2.breadth = 13.0;
    
    // volume of box 1
    volume = Box1.height * Box1.length * Box1.breadth;
    cout << "Volume of Box1 : " << volume <
    // volume of box 2
    volume = Box2.height * Box2.length * Box2.breadth;
    cout << "Volume of Box2 : " << volume <     
    return 0;
}



#include
 
using namespace std;

// Base class
class Shape {
    public:
       void setWidth(int w) {
          width = w;
       }
        
       void setHeight(int h) {
          height = h;
       }
        
    protected:
       int width;
       int height;
};

// Derived class
class Rectangle: public Shape {
    public:
       int getArea() {
          return (width * height);
       }
};

int main(void) {
    Rectangle Rect;
 
    Rect.setWidth(5);
    Rect.setHeight(7);

    // Print the area of the object.
    cout << "Total area: " << Rect.getArea() << endl;

    return 0;
}



#include
using namespace std;
 
class printData {
    public:
       void print(int i) {
          cout << "Printing int: " << i << endl;
       }

       void print(double f) {
          cout << "Printing float: " << f << endl;
       }

       void print(char* c) {
          cout << "Printing character: " << c << endl;
       }
};

int main(void) {
    printData pd;
 
    // Call print to print integer
    pd.print(5);
    
    // Call print to print float
    pd.print(500.263);
    
    // Call print to print character
    pd.print("Hello C++");
 
    return 0;
}



#include
using namespace std;

class Box {
    public:

       double getVolume(void) {
          return length * breadth * height;
       }
        
       void setLength( double len ) {
          length = len;
       }

       void setBreadth( double bre ) {
          breadth = bre;
       }

       void setHeight( double hei ) {
          height = hei;
       }
        
       // Overload + operator to add two Box objects.
       Box operator+(const Box& b) {
          Box box;
          box.length = this->length + b.length;
          box.breadth = this->breadth + b.breadth;
          box.height = this->height + b.height;
          return box;
       }
        
    private:
       double length; // Length of a box
       double breadth; // Breadth of a box
       double height; // Height of a box
};

// Main function for the program
int main( ) {
    Box Box1; // Declare Box1 of type Box
    Box Box2; // Declare Box2 of type Box
    Box Box3; // Declare Box3 of type Box
    double volume = 0.0; // Store the volume of a box here
 
    // box 1 specification
    Box1.setLength(6.0);
    Box1.setBreadth(7.0);
    Box1.setHeight(5.0);
 
    // box 2 specification
    Box2.setLength(12.0);
    Box2.setBreadth(13.0);
    Box2.setHeight(10.0);
 
    // volume of box 1
    volume = Box1.getVolume();
    cout << "Volume of Box1 : " << volume <  
    // volume of box 2
    volume = Box2.getVolume();
    cout << "Volume of Box2 : " << volume <
    // Add two object as follows:
    Box3 = Box1 + Box2;

    // volume of box 3
    volume = Box3.getVolume();
    cout << "Volume of Box3 : " << volume <
    return 0;
}


=================================
=================================

Creating Objects Dynamically

=================================
=================================


#include
using namespace std;

class Box {
    public:
       Box() {
          cout << "Constructor called!" <        }
        
       ~Box() {
          cout << "Destructor called!" <        }
};

int main( ) {
    Box* myBoxArray = new Box[4];

    delete [] myBoxArray; // Delete array

    return 0;
}



===================
===================

Simple Programs

===================
===================

#include
using namespace std;

// Variable declaration:
extern int a, b;
extern int c;
extern float f;
  
int main ()
{
    // Variable definition:
    int a, b;
    int c;
    float f;
 
    // actual initialization
    a = 10;
    b = 20;
    c = a + b;
 
    cout << c << endl ;

    f = 70.0/3.0;
    cout << f << endl ;
 
    return 0;
}


===================
===================
A Simple Program
===================
===================

#include
#include

using namespace std;

int main ()
{
    string str1 = "Hello";
    string str2 = "World";
    string str3;
    int len ;

    // copy str1 into str3
    str3 = str1;
    cout << "str3 : " << str3 << endl;

    // concatenates str1 and str2
    str3 = str1 + str2;
    cout << "str1 + str2 : " << str3 << endl;

    // total length of str3 after concatenation
    len = str3.size();
    cout << "str3.size() : " << len << endl;

    return 0;
}



===================
===================
A Simple Program
===================
===================

#include
using namespace std;
int main( )
{
  char name[50];
  cout << "Please enter your name: ";
  cin >> name;
  cout << "Your name is: " << name << endl;
}


===================
===================
A Simple Program
===================
===================

#include
using namespace std;
 
int main () {
    // number definition:
    short s;
    int i;
    long l;
    float f;
    double d;
   
    // number assignments;
    s = 10;
    i = 1000;
    l = 1000000;
    f = 230.47;
    d = 30949.374;
   
    // number printing;
    cout << "short s :" << s << endl;
    cout << "int i :" << i << endl;
    cout << "long l :" << l << endl;
    cout << "float f :" << f << endl;
    cout << "double d :" << d << endl;
 
    return 0;
}


===================
===================
A Simple Program
===================
===================

#include
#include
using namespace std;
 
int main () {
    // number definition:
    short s = 10;
    int i = -1000;
    long l = 100000;
    float f = 230.47;
    double d = 200.374;

    // mathematical operations;
    cout << "sin(d) :" << sin(d) << endl;
    cout << "abs(i) :" << abs(i) << endl;
    cout << "floor(d) :" << floor(d) << endl;
    cout << "sqrt(f) :" << sqrt(f) << endl;
    cout << "pow( d, 2) :" << pow(d, 2) << endl;
 
    return 0;
}



=================================
=================================

Labs


=================================
=================================

Quad Form

Area of a Rectangle

Volume of a Sphere

Roman Numerals

Fibonacci

Student Grades
(average, standard deviation, semester grades, ...)

Tic-Tac-Toe

Hangman

Encrypt Data

Sudoko

Magic Squares

Find a Path through a Maze (if it exists)

Sum, Product, etc. of Matrices