On this repository you can find some basic examples in C++
I started this repository when I attended the course C++ for C Programmers. Then, I decided use this as a quick reference when need to use some features that are normally confused, like pointers, lists, casting, etc.
Each subdirectory has one or more source codes, someone has C and C++ files to show differences between them.
Just use g++ <source file>.cpp -o <executable>
To generate debug information, just use -g
option
./<executable>
lldb ./<executable>
To debug
Some basic differences between C and C++
- Files
assign1.c
assign1.cpp
- Subjects
- Comments
<iostream>
andnamespace
<vector>
const
insteaddefine
inline
A simple program to read a CSV file
- File
csv-reader.cpp
- Subjects
getline
string::find()
string::substr()
Create multidimensional arrays dynamicaly
- File
dyn-array.cpp
- Subjects
new
anddelete
chuncks of memory- pointers to array
Enum examples using C++ 11
- File
enum.cpp
- Subjects
- Enumeration
- Scope
Some algorithms of Graph Theory
connected.app
gives an example to find connected components in a undirected graph. Based on source find on http://www.geeksforgeeks.org/connected-components-in-an-undirected-graph/
- Files
connected.cpp
- Subjects
- Graph Theory
- lists
-
Basic examples of inheritance:
- Shape -> Rectangle -> Square
- Shape -> Triangle
-
File
inheritance.cpp
-
Subjects
- Inheritance
- Derived classes
- Constructors
Shows the "value" of a variable inside a block
- File
innerblock.c
A very simple examples of IO: read and write files, cin
and cout
- Files
ioexample.cpp
readFile.cpp
- Subjects
namespace std
cout
cin
ifstream
ofstream
Chaining list`
- File
list.cpp
- Subjects
- constructor
- destructor
- memory allocation
- pointers
Processing parameters passing by commamnd line
Some simple instrcutions on how to parse command line options
Found tips on Stack Overflow
-
File
mainParameters.cpp
-
Subjects
- argv, argc
- Command line Options, Parameters
These examples use classes to store multidimensional points. Using overloading to allow basic operations with points as display, add and multiply
The pointAtt.cpp file shows classes inheritence, including a new attribute on the new Class derived from Point Class
Here there is an example on how separate header and implementation files of a Class
to run just execute g++ main.cpp point.cpp -o main -std=c++11
include option -g
to debug
Or you can run make
to compile and generate executable file main
- Files
point.h
point.cpp
pointAtt.cpp
main.cpp
Makefile
- Subjects
- Classes
ostream
- Operators overloading
- Dynamic allocation
<vector>
- Inheritance
- Makefile
Examples on how to use STL
-
Files
square.cpp
-
Subjects
- Iterator - See
square.cpp
to see how to iterate a vector
- Iterator - See
Use of template. You don't need to worry about convertions
- Files
copyVector.cpp
template.cpp
- Subjects
- template
- Casting
Example of how to define a varible type.
- File
typedef.cpp
- Subjects
- `typedef'
template
- Casting
- Operators overloading
This example shows how to use two functions with same name. The only difference is the parameters types.
- Files
swap.c
swap.cpp
- Subjects
inline
- Use two different functions with same name
Some examples to show how to use vectors
- Files
vectorArg.cpp
- Show how to pass vector as an argument
- Subjects
- Vector as an arguments
vector::push_back()
vector::size()