helloworld.c

#include <stdio.h>    // for printf()
#include <stdlib.h>   // for EXIT_SUCCESS

int main(int argc, char** argv) {
  printf("Hello, World!\n");
  return EXIT_SUCCESS;
}

helloworld.cc

// C++'s iostream declares stream objects cin, cout, etc.,
// but within the 'std' namespace.  Note that you don't
// need the .h when you're #including headers from the
// C++ standard library, but you do need them when including
// a local header (e.g.: #include "ll.h")
#include <iostream> // for cout, endl

// We want to include C's header file stdlib.h to use the EXIT_SUCCESS
// preprocessor symbol.  To include a C standard library header foo.h
// in a C++ program, you use #include <cfoo>
#include <cstdlib> // for EXIT_SUCCESS

int main(int argc, char **argv) {
  // "std::cout" means "the symbol 'cout' scoped to the 'std'
  // namespace.  'cout' happens to be an object instance of type
  // 'ostream' whose job is to write to the console.
  //
  // "<<" is an operator.  Objects of type 'ostream' have overloaded
  // this operator, meaning they have defined methods that are invoked
  // whenever an 'ostream' is on the left-hand-side of the "<<"
  // operator.  'ostream' uses C++'s static polymorphism to define
  // multiple methods that handle the "<<" operator; each method
  // takes one argument of a specific type.  So, there is a method
  // that handles the "<<" operator that accepts a C-style string
  // (i.e., a const char *).
  //
  // "Hello, World!" is a C-style string.
  //
  // The expression (std::cout << "Hello, World!") is evaluated
  // first; it invokes the method in the 'std::cout' object of
  // class 'ostream' that overloads the '<<' operator, passing it
  // a pointer to the memory location that contains the C-style
  // string "Hello, World!".  This method writes the string to
  // console, then returns a reference to the "std::cout" argument
  // as its return value.
  //
  // So, that return value will be the left-hand-side of the
  // next expression evaluated, namely (return-val-from-"<<" << endl).
  //
  // "endl" is a 'manipulator' function defined in "std".  This
  // function operates on an ostream to flush the stream and
  // write a newline character.  'std::cout' has a method
  // that is invoked when the '<<' operator is invoked on an ostream
  // and the right-hand-side is a manipulator function pointer.
  std::cout << "Hello, World!" << std::endl;
  return EXIT_SUCCESS;
}

helloworld2.cc

#include <cstdlib>

#include <iostream>
// C++ supports strings as objects!  The std::string class and its methods
// are defined in the string.h C++ standard library header.
#include <string>

// The "using" keyword introduces part of a namespace, or an entire
// namespace, into the current declarative region.  So, the following
// would essentially import all names defined within the "std"
// namespace into the current namespace:
//
//   using namespace std;
//
// From that moment until the end of the current scope, you can
// refer to names in std without prefixing them with "std::".
//
// Alternatively, if you wanted to introduce a specific name into
// the current region without importing the entire namespace, you
// can say something like:
//
//   using std::cout;
//
// Then you can use "cout" without prefixing it with "std::".
using namespace std;

int main(int argc, char **argv) {
  // Here, we're intantiating a new instance of the class std::string,
  // and we're passing a C-style string ("Hello, World!") as an
  // argument to a constructor method.  The new string object instance
  // is stack-allocated, so when main() returns, the instance will be
  // de-allocated (and its destructor will be invoked).  The
  // constructor makes a private copy of the C-style string.
  string hello("Hello, World!");

  // Now, instead of passing a C-style string to the ostream object
  // instance std::cout's method for handling the "<<" operator, we're
  // passing (a reference to) the hello object instance.  It turns out
  // that the string.h header has declared a function that overloads
  // "<<" and accepts (a reference to) a string object as its RHS
  // argument, so this is what's invoked, rather than a method of the
  // ostream object.
  cout << hello << endl;
  return EXIT_SUCCESS;
}

concat.cc

// The string library may be included in other headers
// including iostream (at least on the CSE Linux environment)
#include <cstdlib>
#include <iostream>

using std::cout;
using std::endl;
using std::string;

int main(int argc, char **argv) {
  string hello("Hello");
  hello = hello + ", World!\n";
  cout << hello << endl;
  return EXIT_SUCCESS;
}

manip.cc

#include <cstdlib>

#include <iostream>
// C++'s iomanip.h header defines a bunch of useful stream manipulator
// functions.  These manipulators can be used to modify the formatting
// used when inputting or outputting data, similar to how printf's
// format string lets you specify different kinds of conversions.
#include <iomanip>

using namespace std;

int main(int argc, char **argv) {
  // setw(x) sets the field with to x for the next item
  // sent to the stream.
  cout << "Hi! " << setw(4) << 5 << " " << 5 << endl;

  // hex tells the stream to output integers in hexadecimal;
  // it remains active until you set it back to some other
  // base using dec or oct.
  cout << hex << 16 << " " << 13 << endl;
  cout << dec << 16 << " " << 13 << endl;
  return EXIT_SUCCESS;
}

helloworld3.cc

#include <cstdio>
#include <cstdlib>

// Yes, printf still works in C++.
// No, you should not normally use it in ordinary C++ code.

int main(int argc, char **argv) {
  printf("Hello from C!\n");
  return EXIT_SUCCESS;
}

echonum.cc

#include <iostream>
#include <cstdlib>

using std::cout;
using std::cin;
using std::endl;

int main(int argc, char **argv) {
  int num;
  cout << "Type a number: ";
  cin >> num;
  cout << "You typed: " << num << endl;
  return EXIT_SUCCESS;
}

pointer.cc

#include <iostream>  // cout, endl
#include <cstdlib>   // EXIT_SUCCESS

using std::cout;
using std::endl;

int main(int argc, char** argv) {
  int x = 5, y = 10;
  int* z = &x;

  *z += 1;  // sets *z (and therefore x) to 6
  x += 1;   // sets x (and therefore *z) to 7

  z = &y;   // sets z to the address of y
  *z += 1;  // sets *z (and therefore y) to 11

  cout << "x: " << x << "; y: " << y << "; z: " << z << endl;
  return EXIT_SUCCESS;
}

reference.cc

#include <iostream>  // cout, endl
#include <cstdlib>   // EXIT_SUCCESS

using std::cout;
using std::endl;

int main(int argc, char** argv) {
  int x = 5, y = 10;
  int& z = x;  // binds the name "z" to variable x

  z += 1;  // sets z (and thus x) to 6
  x += 1;  // sets x (and thus z) to 7

  z = y;   // sets z (and thus x) to 10 (the value of y)
  z += 1;  // sets z (and thus x) to 11

  cout << "x: " << x << "; y: " << y << "; z: " << z << endl;
  return EXIT_SUCCESS;
}

passbyreference.cc

#include <iostream>  // cout, endl
#include <cstdlib>   // EXIT_SUCCESS

using std::cout;
using std::endl;

void Swap(int& x, int& y) {
  int tmp = x;
  x = y;
  y = tmp;
}

int main(int argc, char** argv) {
  int a = 5, b = 10;

  Swap(a, b);
  cout << "a: " << a << "; b: " << b << endl;
  return EXIT_SUCCESS;
}

poll.cc

#include <iostream>  // cout, endl
#include <cstdlib>   // EXIT_SUCCESS

void foo(int& x, int* y, int z);

int main(int argc, char** argv) {
    int a = 1;
    int b = 2;
    int& c = a;
    foo(a, &b, c);
    std::cout << "(" << a << ", " << b << ", " << c << ")" << std::endl;
    return EXIT_SUCCESS;
}

void foo(int& x, int* y, int z) {
    z = *y;
    x += 2;
    y = &x;
}

Makefile

CC = gcc
CC2 = g++
CFLAGS = -Wall -g -std=c11
CFLAGS2 = -Wall -g -std=c++17
PROGS = hello helloworld helloworld2 concat manip helloworld3 echonum pointer reference passbyreference poll

all: $(PROGS)

hello: helloworld.c
    $(CC) $(CFLAGS) -o $@ $<

helloworld: helloworld.cc
    $(CC2) $(CFLAGS2) -o $@ $<

helloworld2: helloworld2.cc
    $(CC2) $(CFLAGS2) -o $@ $<

concat: concat.cc
    $(CC2) $(CFLAGS2) -o $@ $<

manip: manip.cc
    $(CC2) $(CFLAGS2) -o $@ $<

helloworld3: helloworld3.cc
    $(CC2) $(CFLAGS2) -o $@ $<

echonum: echonum.cc
    $(CC2) $(CFLAGS2) -o $@ $<

pointer: pointer.cc
    $(CC2) $(CFLAGS2) -o $@ $<

reference: reference.cc
    $(CC2) $(CFLAGS2) -o $@ $<

passbyreference: passbyreference.cc
    $(CC2) $(CFLAGS2) -o $@ $<

poll: poll.cc
    $(CC2) $(CFLAGS2) -o $@ $<

clean:
    rm -f $(PROGS)