os-70-350

os-70-350 Commit Details


Date:2014-01-18 21:55:15 (11 years 7 months ago)
Author:Natalie Adams
Branch:master
Commit:1d5b475ca893ef280a6d69582bf904e513d5c621
Parents: 93fa0b17842f3ca98001b707b143b19a6a7a67b9
Message:Adding crash course examples to C and C++

Changes:

File differences

c-crash-course/example1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Simple C++ example
#include <iostream>
using namespace std;
int main(int argc, char * argv[]) // parameters are optional
{
// cout is located in the std namespace
cout << "hello, world" << endl;
// could also be written as
std::cout << "hello, world" << endl;
return 0;
}
c-crash-course/example10.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//C - could also do this - valid in C++ as well
#include <stdio.h>
#include <stdlib.h>
void f(int * i)
{
*i = 4;
}
int main()
{
int x = 10;
f(&x);
printf("%i", x);
return 0;
}
c-crash-course/example11.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//C++
#include <iostream>
class A
{
public:
A() { std::cout << "Default ctor" << endl; }
A(int i) { std::cout << "Non default ctor" << endl; }
~A() { std::cout << "dtor" << endl; }
void f() { std::cout << "woof" << std::endl; }
};
int main()
{
A a;
A b(1);
a.f();
d.f();
}
c-crash-course/example12.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Linked lists in C
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int i;
struct node * next;
} node;
int main()
{
node * root = malloc(sizeof(node));
root->next = NULL;
free(root);
return 0;
}
c-crash-course/example13.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Linked lists in C++
#include <iostream>
struct node
{
int i;
node * next;
};
int main()
{
node * root = new node();
root->next = NULL;
delete root;
return 0;
}
c-crash-course/example14.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//C++ Smart pointers
#include <iostream>
struct node
{
int i;
node * next;
};
template <class T>
class smartptr
{
private:
T * ptr;
public:
smartptr(T * t) { ptr = t; }
~smartptr() { delete ptr; }
T* operator ->() { return ptr; }
};
int main()
{
smartptr<node> n(new node());
n->next = NULL;
}
c-crash-course/example15.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
//C++ built in smart pointers
#include <iostream>
#include <memory>
using namespace std;
int main()
{
shared_ptr<int> x(new int(5));
unique_ptr<int> y(new int(10));
shared_ptr<int> z = x;
unique_ptr<int> yy = move(y);
}
c-crash-course/example16.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//C++ - using vectors as C arrays
#include <vector>
#include <iostream>
void f(int * arr, size_t count)
{
for(size_t i = 0; i < count; i++)
std::cout << *(i+arr) << std::endl; //arr[i] also works as well
}
int main()
{
std::vector<int> x;
x.push_back(1);
x.push_back(2);
f(&x[0], x.size());
return 0;
}
c-crash-course/example17.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//C++ - using vectors as C arrays
#include <vector>
#include <iostream>
#include <Windows.h>
#include <string>
int main()
{
std::vector<wchar_t> wstr;
std::string x = "normal string";
wstr.resize(x.size() + 1);
size_t convertedChars = 0;
mbstowcs_s(&convertedChars, &wstr[0], x.size() + 1, x.c_str(), _TRUNCATE);
std::wcout << &wstr[0] << std::endl;
}
c-crash-course/example18.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//C++ - using vectors as C arrays
#include <vector>
#include <iostream>
#include <cstdlib>
#include <string>
int main()
{
std::vector<wchar_t> wstr;
std::string x = "normal string";
std::wstring z = std::wstring(x.begin(), x.end());
std::wcout << z.c_str() << std::endl;
std::wcout << &z[0] << std::endl;
}
c-crash-course/example19.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//C++ - using iterators
#include <vector>
#include <iostream>
void slice(std::vector<int>::iterator start, std::vector<int>::iterator end)
{
for(std::vector<int>::iterator i = start; i != end; i++)
std::cout << *i << std::endl;
}
int main()
{
std::vector<int> x;
for (int i= 0; i < 10; ++i) { x.push_back(i); }
for(std::vector<int>::iterator i = x.begin(); i != x.end(); i++)
std::cout << *i << std::endl;
std::cout << std::endl;
slice(x.begin(), x.begin() + 2);
std::cout << std::endl;
slice(x.end() - 2, x.end());
}
c-crash-course/example2.c
1
2
3
4
5
6
7
8
// Simple C example
#include <stdio.h>
int main(int argc, char * argv[])
{
printf("Hello, world");
return 0;
}
c-crash-course/example3.cpp
1
2
3
4
5
6
7
8
9
// Simple C++ with C libraries
#include <cstdio>
// #include <stdio.h> would also work
int main(int argc, char * argv[])
{
printf("Hello, world");
return 0;
}
c-crash-course/example4.cpp
1
2
3
4
5
6
7
8
9
// Declaring variables
#include <string> //string
#include <vector> //vector
int main()
{
int i;
std::string x;
std::vector y;
}
c-crash-course/example5.cpp
1
2
3
4
5
6
7
8
9
10
11
// Pointers are variables that point to blocks of memory
// C++
#include <iostream>
int main()
{
int * i = new int(5);
// In order to view that block of memory you must look at
std::cout << *i << std::endl;
delete i;
}
c-crash-course/example6.c
1
2
3
4
5
6
7
8
9
10
11
12
// C example
#include <stdio.h>
#include <stdlib.h>
int main()
{
int * i = malloc(sizeof(int));
*i = 5;
printf("%i", *i);
return 0;
}
c-crash-course/example7.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//By default values are passed by value (or copy)
#include <iostream>
void f(int i)
{
i = 4;
}
int main()
{
int x = 10;
f(x);
std::cout << x << std::endl;
return 0;
}
c-crash-course/example8.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// However, C and C++ does support passing by reference
// C++
#include <iostream>
void f(int & x)
{
x = 4;
}
int main()
{
int z = 8;
f(z);
std::cout << z << std::endl;
}
c-crash-course/example9.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// C - also works in C++
#include <stdio.h>
#include <stdlib.h>
void f(int * x)
{
*x = 4; // This is dangerous as x could be NULL but for the sake of a simple example - we will assume it won't ever be
}
int main()
{
int * i = malloc(sizeof(int));
f(i);
printf("%i", i);
free(i);
}

Archive Download the corresponding diff file

Branches

Number of commits:
Page rendered in 1.13608s using 14 queries.