<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.scott5.org/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B_Basics</id>
	<title>C++ Basics - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.scott5.org/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B_Basics"/>
	<link rel="alternate" type="text/html" href="https://wiki.scott5.org/index.php?title=C%2B%2B_Basics&amp;action=history"/>
	<updated>2026-05-02T21:28:43Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.1</generator>
	<entry>
		<id>https://wiki.scott5.org/index.php?title=C%2B%2B_Basics&amp;diff=121&amp;oldid=prev</id>
		<title>Scott at 19:12, 31 January 2011</title>
		<link rel="alternate" type="text/html" href="https://wiki.scott5.org/index.php?title=C%2B%2B_Basics&amp;diff=121&amp;oldid=prev"/>
		<updated>2011-01-31T19:12:56Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Example Program==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;string&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
&lt;br /&gt;
int main(){&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;Enter your name: &amp;quot;;&lt;br /&gt;
    string name;&lt;br /&gt;
    cin &amp;gt;&amp;gt; name;&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;Hi there, &amp;quot; &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot;!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
compile with&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ g++ test.cpp -o test&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Header File Example==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
// use preprocessor directives to make sure that this header file&lt;br /&gt;
// is included only once&lt;br /&gt;
#ifndef TEST_H&lt;br /&gt;
#define TEST_H&lt;br /&gt;
int myFunctionPrototype(int i, int j);&lt;br /&gt;
#endif /* TEST_H */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Pointers==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
// The null pointer is 0.&lt;br /&gt;
int i = 10;&lt;br /&gt;
int* p;  // declare p a pointer to an int&lt;br /&gt;
p = &amp;amp;i;  // assigns p to the address of int i&lt;br /&gt;
*p = 20; // the variable to p points to (i) is reassigned to 20;&lt;br /&gt;
int&amp;amp; r;  // declare r a reference to an int&lt;br /&gt;
r = i;   // r is now an alias for i&lt;br /&gt;
r = 30;  // i is now 30&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Function Pointers: can pass as argument to a function.&lt;br /&gt;
&lt;br /&gt;
==Passing by Value vs Reference==&lt;br /&gt;
To return a modified argument, pass by reference&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
void swap(int&amp;amp; i1, int&amp;amp; i2)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
To increase efficiency&lt;br /&gt;
* pass built-in types by value: &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int function1(int i){...}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
* pass complex types by reference &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int function2(const MyType&amp;amp; myObject){...}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
(use const to document that you aren&amp;#039;t changing myObject)&lt;br /&gt;
&lt;br /&gt;
==Inline Functions==&lt;br /&gt;
Inline functions are copied verbatim into the code where they are used:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
inline int double(int x){return 2*x;}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Use for small functions to speed up code. &lt;br /&gt;
&lt;br /&gt;
==Function Template==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
template &amp;lt;class NumType&amp;gt;&lt;br /&gt;
NumType max(NumType a, NumType b){&lt;br /&gt;
    NumType c = (a&amp;gt;b)?a:b;&lt;br /&gt;
    return c;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Boolean Values==&lt;br /&gt;
===Old Style===&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
0        =&amp;gt; false&lt;br /&gt;
nonzero  =&amp;gt; true&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Thus,&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (my_pointer)...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
can be used to test for a null pointer. &lt;br /&gt;
&lt;br /&gt;
===New Style===&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
bool test = true;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Max/Min values of numeric types==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;limits&amp;gt;&lt;br /&gt;
int maxInt = numeric_limits&amp;lt;int&amp;gt;::max();&lt;br /&gt;
double minDouble = numeric_limits&amp;lt;double&amp;gt;::min();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Dynamic Memory Allocation Concepts==&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;stack&amp;#039;&amp;#039;&amp;#039; - each function call pushes new memory to the stack. This memory is popped when the function returns.&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;heap&amp;#039;&amp;#039;&amp;#039; - this is dynamic memory to be used at the programmer&amp;#039;s discretion&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;new&amp;#039;&amp;#039;&amp;#039; - allocates memory from the heap&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;delete&amp;#039;&amp;#039;&amp;#039; - deallocates memory from the heap&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;memory leak&amp;#039;&amp;#039;&amp;#039; - a chunk of allocated memory that never gets deallocated &lt;br /&gt;
&lt;br /&gt;
==Optional Function Arguments==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
void function1(int i, int j = 0)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
may be called as&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
function1(5)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
As a matter of style, default parameters are specified in the function declaration in the header file. &lt;br /&gt;
&lt;br /&gt;
==Sequential Containers from the Standard Template Library==&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;vector&amp;#039;&amp;#039;&amp;#039; - an array that knows how to grow&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; - doubly-linked list&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;slist&amp;#039;&amp;#039;&amp;#039; - singly-linked list&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;deque&amp;#039;&amp;#039;&amp;#039; - like a vector with efficient access to the front &lt;br /&gt;
&lt;br /&gt;
==Iterate over a container==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
vector&amp;lt;Thing&amp;gt;::iterator iter;  // a pointer&lt;br /&gt;
for (iter = vec.begin(); iter != vec.end(); iter++){&lt;br /&gt;
    Thing thing = (Thing)(*iter);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Local static object in a function==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
const vector&amp;lt;int&amp;gt;* fibonacciSeq(int size){&lt;br /&gt;
    static vector&amp;lt;int&amp;gt; elements;&lt;br /&gt;
    //...populate elements...&lt;br /&gt;
    return &amp;amp;elements;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Libraries==&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Static library&amp;#039;&amp;#039;&amp;#039; - object code that is incorporated into the executable during compilation, present at runtime&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Shared (or Dynamic) library&amp;#039;&amp;#039;&amp;#039; - object code this is referenced in the executable during compilation, used at runtime. Shared libraries are shared by any running programs that want to use them.&lt;/div&gt;</summary>
		<author><name>Scott</name></author>
	</entry>
</feed>