-
very large language, difficult to learn
-
has 5 parts:
- C language
- C preprocessor
- Classes and Objects
- Templates
- Standard Template Library
-
passing objects onto the stack is bad practice; instead use pointers or references
- more secure
- less likely to cause bugs or crashes
- more efficient, conserving resources and speeding up programs
staticmodifier is used to place variables on heap instead of stack- additionally,
consthelps ward off unintended side-effects
-
::is scope resolution operator, which is used to differentiate between variables of the same name of different scopes, to define a function outside a class, to access a class' static variables, to deal with issues relating to multiple inheritance, for namespace, to refer to a class inside another class -
constfunctions can always be called -
non-
constfunctions can only be called by mutable (non-const) objects -
Destructors are named starting with a tilde (~)
-
Use the standard C++ namespace
using namespace std; -
Use only part of a namespace:
using std::cin; -
Including header files:
#include <iostream>- old way:
#include <iostream.h>
- old way:
Using Pointers With Arrays Example
#include <iostream>
using namespace std;
int main() {
int luckyNumbers[5] = { 1, 3, 5, 7, 9 };
cout << luckyNumbers << endl; // address of first element in array
cout << &luckyNumbers[0] << endl; // same, written differently
cout << luckyNumbers[0] << endl; // value of first element in array
cout << *luckyNumbers << endl; // same, using dereferencing syntax (*varName)
return 0;
}
- an alias, another name for an already-existing variable
- can't have
NULLreferences, must always point to legitimate storage location w/a value - once initialized, a reference cannot be changed to point to another object
- a reference must be initialized to a value when created, making wild references (pointing to arbitrary memory location) less likely
- references only have one level of indirection, i.e., a reference to a variable, whereas pointers can have multiple levels, i.e., a pointer to a pointer to a variable, etc.
- as with pointers, set up a function/method to utilize pass-by-reference in order to persistently change the value of variables after the function/method block is cleaned up/ends
- as with pointers, save resources by passing a reference to a structure instead of having to copy and pass the entire contents of the structure to a function/method
- can be used in for-each loops to modify all objects in an iterable
- the previous point applies here as well - saves resources by not having to copy structures, instead simply copying references to them
- do the above in a more restricted and safe manner than w/pointers
Reference Example
#include<iostream>
using namespace std;
int main()
{
int x = 10;
// ref is a reference to x.
int& ref = x;
// Value of x is now changed to 20
ref = 20;
cout << "x = " << x << endl ;
// Value of x is now changed to 30
x = 30;
cout << "ref = " << ref << endl ;
return 0;
}
- somewhat rare but very useful
Function Pointer Example
#include <cstdio>
void func()
{
puts("this is func()");
}
int main()
{
puts("this is main()");
void (*pfunc)() = func;
(*pfunc)(); // can also use pfunc() but it's ambiguous and not recommended
return 0;
}
- allow any number of arguments to be passed
- require
#include <cstdarg>
Variadic Function Example
#include <cstdio>
#include <cstdarg>
double average(const int count, ...)
{
va_list ap;
int i;
double total = 0.0;
va_start(ap, count);
for(i = 0; i < count; ++i) {
total += va_arg(ap, double);
}
va_end(ap);
return total / count;
}