C++ Notebook

Another rustic, very basic C++ Notebook and CheatSheet.

Notes:

:: is called Scope Resulution Operator

std::cout << “text to show” << “another string” << “escape character to newline \n”;

std::cin >> variableName; //prompts value to assign to the variable.

std::cout << "Hello" //std stands for standard as a Namespace, also may stand above of the code as pre-defined:

#include <iostream>
using namespace std; // a namespace "std" is defined, 
int main() 
{ 
    cout << "..."; // no more std:: additional syntax needed.
    return 0; 
}

Anatomy of a C++ basic coding scope

#include <iostream> // Importing Libraries
using namespace std; // Definition of namespaces
int main() // default function, main scope, like body.
{ 
    return foo(); // belongs to main function, a must.
}

void foo() {
    return "Hello, this is Foo.";
} 

Classes

Simple Class

include <iostream>
using namespace std;

class Triangle {

  public:
    float width;
    float height;

  float area(){
    return ((width * height) / 2);
  }
};

int main () {

  Triangle myTriangle;

  myTriangle.width = 5; //member access using the .dot operator
  myTriangle.height = 6;

  cout << "Triangle area: " << myTriangle.area() << endl;

  return 0;
}

Start the Discussion!Leave a Reply