Intro to C++

  1. History of C++
    1. 1950s - Early programming languages where difficult to use, and maintaining large programs was becomming increasingly difficult
    2. 1960s - Martin Richards, working the University of Cambridge, developed a structured programming language called BCPL (Basic Combined Programming Language). Ken Thompson at Bell Labs created B, a modified version of BCPL
    3. 1970s - Dennis Ritchie at Bell Labs improved on the B language and called his creation C. It is one of the most widely used programming lanugages of all time
    4. 1980s - A new paradigm was being adopted called Object-Oriented Programming (OOP) which sought to reduce the cost of designing, implementing, debugging, and modifying large, complex programs. Bjarne Stroustrup at Bell Labs created C++ by adding OOP constructs to the C programming language. C++ is one of the most popular programming languages in the world
    5. Many programming languages (Java, C#, JavaScript, Python) are very similar to C++, so once you learn C++ it is not difficult to learn other programming languages
  2. Creating a C++ program
    1. Compiler - software that converts a program written in a high-level language (HLL) like C++ into object code or machine language
    2. A compiler is part of an integrated development environment (IDE) which contains a number of tools for writing software
      1. Now we are running C++ programs in your zyBook without an IDE
      2. Later we will use a professional IDE called Visual Studio to type in our C++ programs, build, and run them
        1. Runs only on Windows
        2. Visual Studio Community can be downloaded for free
        3. Harding has free access to Visual Studio Enterprise in Microsoft Imagine (read the instructions)
        4. If you have a Mac, you might want to run Boot Camp which allows you to install Windows on a separate partition on your hard drive. You can download Windows 10 for free from Microsoft Imagine
      3. CodingGround allows you to type and run C++ programs on your web browser
    3. Building (or making) is composed of two steps:
      1. Compiling - Interpretting the source code (.cpp) to create an object file (.obj).
        1. The compiler cannot create an object file unless your program is free of syntax errors
        2. Syntax error - any typing mistake in your code
      2. Linking - Combining the object file with other object files and libraries to form a complete executable (.exe)
        1. The executable contains machine code, instructions that the computer natively understands
        2. The executable can be distributed to others without distributing the source code
    4. Once you have an executable, you should run it and verify that it is working correctly
    5. If you find logic errors (bugs), you will need to modify your source code, re-build it, and run it again to ensure it is free of logic errors
  3. Program that calculates the area of a rectangle:
    // This program calculates the area of a rectangle.
    // By Frank McCown
    
    #include <iostream>
    using namespace std;
    
    int main() {
    	int width;
    	int height;
    	int	area;
    	
    	cout << "Width? ";
    	cin >> width;
    	
    	cout << "Height? ";
    	cin >> height;
    	
    	area = width * height;
    	cout << "Area is " << area;
    
    	return 0;
    }
    
  4. Programming components
    1. Comments
      1. Start with // or surrounded by /* */
        // This is a single line comment.
        
        /* This is a 
           multi-line 
           comment. */
        
      2. Everything to the right of // is ignored by the compiler
        int x;   // This comment is ignored by the compiler
        
      3. Used only to help make the program easier to read for humans
      4. Always a good idea to document (use comments) what your program does
    2. Include statement
      1. Tells the compiler to import a library: #include <library-name>
      2. A library is a block of code that a program may use to do something helpful
      3. iostream is a library that sends output to the console (cout) and reads input from the keyboard via the console (cin)
      4. The using statement is necessary, but don't worry about what it's used for now
    3. main(): int main() is the beginning of the executable statements
      1. Uses { and } to surround statements
      2. return 0; at the end says the program finished successfully
    4. Semicolons mark the end of a statement and are necessary on almost every line of code
    5. Variable declarations
      1. A variable used by a program must be declared before the variable is used; not necessary in a flowchart
      2. int means that the variable may hold integers (no decimal place), like 2 and -10
      3. width, height, area are the three variables used in the program
      4. We could have used single letter for variables like w, h and a, but using complete words makes our programs easier to understand and is preferred by most programmers
      5. Rules for choosing variable names (identifiers):
        1. Cannot be a reserved word like using or int
        2. Can only contain letters, digits, or underscores
          int test5_0;   // OK
          int test50%;   // WRONG (% is not a legal char)
          int test 50;   // WRONG (space is not legal)
          
        3. Must begin with a letter or an underscore
          int test_2;   // OK
          int _2test;   // OK
          int 2_test;   // WRONG (starts with digit)
          
      6. C++ is case-sensitive, so width and Width are not the same variable!
    6. Output statements
      1. Send data out to the console window
      2. Always starts with cout
      3. Use the insertion operator << to send string literals like "Hello!" and the value of variables to the console
        cout << "height is " << height; 
        
      4. Use endl to output a newline character, which makes output go to the next line
        /* Outputs:
        Hello
        there! */
        cout << "Hello" << endl << "there!"; 
        
    7. Input statements
      1. Read input from the keyboard
      2. Always starts with cin
      3. Use the extraction operator >> with a variable to read a number from the keyboard into the variable
        // Read a number into x
        cin >> x;
        
        // Read in three numbers
        cin >> x >> y >> z;
        
    8. Assignment statements
      1. Set a variable to a value using the assignment operator =
        final = 80.0;
        
      2. Always put the variable being set on the left side of =
        80.0 = final;   // Syntax error!
        
      3. Arithmetic expressions
        1. Variables may be assigned to an arithmetic expression which is evaluated before the assignment
        2. C++ arithmetic operators
          1. +   addition
          2. -   subtraction
          3. *   multiplication
          4. /   division
          5. %   modulus (for finding remainders, more later)
        3. Parenthesis are used to enforce order of evaluation
          		// Calculate the average of the final and midterm
          		aveGrade = (final + midterm) / 2;
          		
          		// Wrong since since / is done first!
          		aveGrade = final + midterm / 2;