C++ programming language

C++ programming lang:

C++ is backward compatible with C, that means you can use all your C code in your C++ pgm, and pgm will still compile fine. So, you already can write C++ pgm, if you know C. Infact all C pgms that you have can be renamed as C++. However, a lot of object oriented features were added in C++, which programmers take advantage of, by modifying existing C code. So, C++ allows you to take incremental steps from a C pgm. All C functions like printf, malloc, etc are available in C++ too.


http://www.learncpp.com/

sample pgm: hello.cpp (C++ files can also be named as .cxx, it's an old style extension that's still used)
----
#include <iostream> //needed for std io func, newer system header files do not have .h extension
#include "myfile.h" //for user defined header files, use " "

void printA(int x, int y) //each func needs to be defined separately
{
    std::cout << "A" << x << y << std::endl; //std:: prefix says that cout is in std namespace
}
 //If func printA is defined after main(), then we need to do forward declaration
void printA(int x, int y); //using func prototype for forward declaration. also used if printA is defined in separate file by itself

// Definition of main()
int main()
{
  int a;
  std::cout << "Enter number " ; //cout to print out on screen. << indicates RHS is transferred to LHS. string is transffered to cout
  std::cin >> a; //cin to take i/p from screen. >> indicates LHS is transferred to RHS. cin transfers val to a
  std::cout << "Hello World num is " << a << std::endl; //endl to put newline. multiple prints can be in same stmt as long as separated by <<
    std::cout << "Starting main()" << std::endl;
    printA(1,a);
    std::cout << "Ending main()" << std::endl;
    return 0; //any +ve num returned means error
}


complie C++:

g++ ~/hello.cpp
execute: ./a.out

Enter number 1
Hello World num is 1
Starting main()
A 1 1
Ending main()

------------------------

Keywords : C++ reserves a set of 84 words (as of C++14) for its own use.
ex: char, int, for, case, while, struct, void, ...

identifiers: The name of a variable, function, type, or other kind of object in C++ is called an identifier. The identifier can only be composed of letters (lower or upper case), numbers, and the underscore character. identifiers are case sensitive

Literals: A literal is a fixed value that has been inserted (hardcoded) directly into the source code, such as 5, or 3.14159. Literals always evaluate to themselves.

operands: Literals, variables, and function calls that return values are all known as operands.

operators: Operators tell the expression how to combine one or more operands to produce a new result.

data types:
boolean: bool (true or false). true is stored as int 1, false as int 0
character: char, char16_t(C++11 only), char32_t(C++11 only). char16_t and char32_t store char in 16 or 32 bit as UTF-16 or UTF-32 Unicode char. ex: 'c'. char stored as 1 byte int (usually signed). Since char is 8 bits, ASCII char numbers are b/w 0 to 127. some char from 0 to 31 are escape char, \n=newline, \t=tab, char code 27 is escape \.
floating point: float, double, long double. signed by default
integer: short, int, long, long long (C++11 only). signed by default (avoid unsigned)
void: for functions that do not take any param or return a value. In C++, empty param are allowed
 ex: int Value(void) { ...} same as int Value() {...}
sizeof(char) => returns size of char in bytes. C++ gurantees min size of each data type, actual size may be bigger. int is min 2 bytes, while float is min 4 bytes. Fixed size int, etc were defined later in C++ inside std namespace. i.e int8_t, uint8_t, int16_t, ...

3 ways to init a var
A. int nval = 5; bool b1=true; //copy initialization
B. int nval(5); //direct initialization
C. int nval{5}; //uniform initialization, works for all data types, but only with C++11. Note: curly braces instead of circle brackets. recommended to use this style.
 int value{}; // default initialization to 0

var assgn (not init):
int nValue;
nValue = 5; // copy assignment (no way to do direct or uniform assignment)
const double gravity { 9.8 }; => assigns const val. can't be changed
const int maxNameLength { 30 }; => assigned const 30
-----------

preprocessor:
ex: #define NUM 7
ex: #ifdef PRINT_J std::cout << "joe"; #endif
ex: header guards
#ifndef SQUARE_H
#define SQUARE_H
....
#endif

---------
namespace:
sample pgm:
ex: constants.h
namespace constants
{
    const double pi(3.14159);
    const double avogadro(6.0221413e23);
    // ... other related constants
}
ex: myfile.cpp
#include "constants.h"
double circumference = 2 * radius * constants::pi;

-------------