Académique Documents
Professionnel Documents
Culture Documents
* File: 20160909_CPP_L01.cpp.
* Title: 2016-09-09 C++ Lesson 01
* Author: Renato Montes
* Description: Notes for Albert Wei's C++ class
*/
/* First program */
#include <iostream>
int main() {
std::cout << "hello world" << std::endl;
}
/*Second Program*/
#include <iostream>
struct Hello {
Hello() { std::cout << "hello world" << std::endl; } //print at constructor
// use "ctor" as abbreviation for constructor
~Hello() { std::cout << "goodbye world" << std::endl; } //destructor
// use "dtor" for destructor
};
int main() {
Hello h; // cf. int n;
// Hello is type, h is the name of the variable
}
int m = 1, n = 2;
swap(m, n);
//a is a reference to m, b is a reference to n
//calling it:
double d1 = 3.14, d2 = 2.71;
swap(d1,d2);
long l1 = 1234, l2=5678;
swap(l1,l2);
swap(l1,d1);//doesn't compile
swap<double>(l1,d1);//also won't work, but would temporarily convert l1
// to a double
/* Class Templates */
//e.g. vector<>, which is dynamic (it can grow at the end)
//when using vector<>, must specify the type: vector<int>
vector<int> v;
v.push_back(3);
v.push_back(1);