summaryrefslogtreecommitdiffhomepage
path: root/c++.html.markdown
blob: 5bf7e2eaca7209ab61bf62312ef72598a3f624be (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
---
language: c++
filename: learncpp.cpp
contributors:
    - ["Steven Basart", "http://github.com/xksteven"]
lang: en
---

I am writing this to highlight the differences and 
additions that C++ has with respect to C. My 
suggestion would be to follow the C tutorial first
then look here for the additions and differences.

```c++
///////////////////////////////////////   
// C++ differences   
///////////////////////////////////////  


//In C++   
//cannot use void main()   
int main() { //or int main(int argc, char **argv)   
    //cannot end with return;   
    return 0;   
    //Can also end without return statement   
}   
   
//In C++  
/*    
  //This could lead to compiler errors and is discouraged    
  //#if 0 #endif pairs are encouraged instead   
*/   
   
//In C++    
sizeof(10) //Typically 4   
sizeof('c') == 1    

//In C   
sizeof('c') == sizeof(10) //true chars are passed as ints   


//In C++ strict prototyping   
void func(); //function which accepts no arguments   

//In C   
void func(); //function which may accept arguments    


//In C++   
for(int i = 0; i < 10; i++) {;}   
//In C must int i must be declared before   


//C++ Supports Function overloading   
//Provided each function takes different   
//parameters    

void printing(char const *myString)    
{printf("String %s\n",myString);} //Hello   

void printing(int myInt)    
{printf("My int is %d",myInt);} //15   

int main ()    
{   
    printing("Hello");   
    printing(15);   
}   
   


//C++ Default Function Arguments   
void two_ints(int a = 1, int b = 4);   

int main()   
{    
    two_ints();            // arguments:  1, 4   
    two_ints(20);          // arguments: 20, 4   
    two_ints(20, 5);       // arguments: 20, 5   
}   


//C++ added the nullptr which is different from 0   
int *ip = nullptr;      // OK   
int value = nullptr;    // error: value is no pointer  


///////////////////////////////////////   
// C++ Additions ontop of C    
///////////////////////////////////////   


///////////////////////////////////////   
// C++ Namespace   
///////////////////////////////////////   

//Namespaces allow you to define your own    
//functions and variables for use    

// Use '::' to change variable (or function) scope   
// Putting '::' before a function or variable will   
// reference a global scope    

// This allows you to make normal c library calls   
// std is for standard library    
using namespace std;   

#include <stdio.h>    

int counter = 50;                // global variable    

int main()   
{   
    for (int counter = 1;        // this refers to the   
    counter < 2;                 // local variable   
    counter++)   
    {   
        printf("Global var %d local var %d\n",    
            ::counter,           // global variable    
            counter);            // local variable    
        // => Global var 50 local var 1   
    }   
}   

// Namespaces can be nested   


namespace myFirstNameSpace   
{   
    namespace myInnerSoul   
    {   
        cos(int x)   
        {   
            printf("My inner soul was made to program.");   
        }   
    }   
}   

namespace anotherNameSpace  
{   
    cos(int x) {;} //does nothing   
}   
   
int main()   
{   
    //Specify the full path because main is outside of both namespaces.    
    //Will print out My inner soul was made to program.    
    myFirstNameSpace::myInnerSoul::cos(60);   
}   


///////////////////////////////////////   
// C++ Strings   
///////////////////////////////////////   

//Strings in C++ are Objects and have many functions  
myString = "Hello";   
myOtherString = " World";   

myString + myOtherString; // => "Hello World"    

myString + ' You'; // => "Hello You"   

myString != myOtherString; //True   

//An example of a string method   
myString.append(" Dog"); // => "Hello Dog"   


///////////////////////////////////////   
// C++ Input Output   
///////////////////////////////////////   

//C++ input and output streams   
//cin, cout, cerr, << is insertion and >> is extraction operator   
#include <iostream>   

using namespace std;   

int main()   
{   

   int myInt;   
    
   //Prints to stdout (or terminal/screen)   
   cout << "Enter your fav number:\n";   
   //Takes in input   
   cin >> myInt;   

   //cout can also be formatted   
   cout << "Your fav number is " << myInt << "\n";   
   //Your fav number is ##  

   cerr << "Used for error messages";   
}   


///////////////////////////////////////   
// C++ Classes   
///////////////////////////////////////


//First example of classes   
#include <iostream>   

//define a class    
class Doggie   
{   
    std::string name;   
    int         weight;   

   // These are only the declarations   
   //Can also have private and protected   
   public:     
       //The public methods (can also include variables)   

   // Default constructor   
   Doggie();   

   void setName(std::string dogsName);   
   void setWeight(int dogsWeight);    
   void printDog();   

   //Can define functions within class declaration too   
   void dogBark() {std::cout << "Bark Bark\n"}   

   //Destructors are methods that free the allocated space   
   ~doggieDestructor();   
   //if no destructor compiler defines the trivial destructor   

//Classes are similar to structs and must close the } with ;    
};   

// This is the implementation of the class methods   
// Also called the definition   
void Doggie::Doggie () {   
    std::cout << "A doggie is born. Woof!\n";   
}   
 
void Doggie::setName (std::string doggie_name) {    
    name = doggie_name;   
}   

void Doggie::setWeight (int doggie_weight) {   
    weight = doggie_weight;   
}   

void Doggie::printDog () {   
    std::cout << "Dog is " << name << " weighs" << weight << "\n";    
}   

void Doggie::~doggieDestructor () {   
    delete[] name;   
    delete weight;    
}   

int main () {    
  Doggie deedee; // prints out a doggie is born. Woof!   
  deedee.setName ("Barkley");    
  deedee.setWeight(1000000);   
  deedee.printDog;   
  //prints => Dog is Barkley weighs 1000000    
  return 0;   
}   


//C++ Class inheritance   

class German_Sheperd : public Doggie  
{
   //This class now inherits everything public and protected from Doggie class

   //Good practice to put d_ in front of datatypes in classes   
   std::string d_type;   

   public:  
      void dogType() {d_type = "German Sheperd";}   
};   



///////////////////////////////////////   
// C++ Exception Handling   
///////////////////////////////////////    

try {   
   throw 12.25;  // throws a double no handler declared   
} catch (int errorNum)   
{   
  std::cout << "I caught an int " << errorNum << "\n";   
//default catcher   
} catch (...)   
{   
    std::cout << "I got an error. Not sure what but I can pass it up.";   
    throw;   
}   


///////////////////////////////////////    
// C++ Operator Overloading   
///////////////////////////////////////   

// In C++ you can overload operators such as +, -, new, etc.   

#include <iostream>   
using namespace std;   

class Vector {   
    public:   
        double x,y;   
        Vector () {};   
        Vector (double a, double b) : x(a), y(b) {}   
        Vector operator + (const CVector&);   
        Vector operator += (const CVector&);   
};   
 
Vector Vector::operator+ (const Vector& rhs)    
{   
    Vector temp;   
    temp.x = x + rhs.x;   
    temp.y = y + rhs.y;   
    return temp;   
}   

Vector Vector::operator+= (const Vector& rhs)   
{   
    x += rhs.x;   
    y += rhs.y;   
    return *this;   
}    

int main () {   
    Vector up (0,1);   
    Vector right (1,0);   
    Vector result;    
    // This calls the Vector + operator   
    // Vector up calls the + (function) with right as its paramater   
    result = up + right;   
    // prints out => Result is upright (1,1)   
    cout << "Result is upright (" << result.x << ',' << result.y << ")\n";   
    return 0;   
}

```
Futher Reading   

for more resources see: http://www.icce.rug.nl/documents/cplusplus/   
for other reference material: http://www.cplusplus.com/doc/tutorial/