2014-09-10 22:04:18 +00:00
|
|
|
---
|
|
|
|
language: c++
|
|
|
|
filename: learncpp.cpp
|
|
|
|
contributors:
|
|
|
|
- ["Steven Basart", "http://github.com/xksteven"]
|
2014-10-10 05:57:03 +00:00
|
|
|
- ["Matt Kline", "https://github.com/mrkline"]
|
2014-09-10 22:04:18 +00:00
|
|
|
lang: en
|
|
|
|
---
|
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
I am writing this to highlight the differences and
|
|
|
|
additions that C++ has with respect to C. My
|
2014-09-10 22:04:18 +00:00
|
|
|
suggestion would be to follow the C tutorial first
|
|
|
|
then look here for the additions and differences.
|
|
|
|
|
|
|
|
```c++
|
2014-10-10 05:57:52 +00:00
|
|
|
///////////////////////////////////////
|
|
|
|
// C++ differences
|
|
|
|
///////////////////////////////////////
|
2014-09-10 22:04:18 +00:00
|
|
|
|
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
//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
|
|
|
|
}
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
//In C++
|
|
|
|
/*
|
|
|
|
//This could lead to compiler errors and is discouraged
|
|
|
|
//#if 0 #endif pairs are encouraged instead
|
|
|
|
*/
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
//In C++
|
|
|
|
sizeof(10) //Typically 4
|
|
|
|
sizeof('c') == 1
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
//In C
|
|
|
|
sizeof('c') == sizeof(10) //true chars are passed as ints
|
2014-09-10 22:04:18 +00:00
|
|
|
|
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
//In C++ strict prototyping
|
|
|
|
void func(); //function which accepts no arguments
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
//In C
|
|
|
|
void func(); //function which may accept arguments
|
2014-09-10 22:04:18 +00:00
|
|
|
|
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
//In C++
|
|
|
|
for(int i = 0; i < 10; i++) {;}
|
|
|
|
//In C must int i must be declared before
|
2014-09-10 22:04:18 +00:00
|
|
|
|
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
//C++ Supports Function overloading
|
|
|
|
//Provided each function takes different
|
|
|
|
//parameters
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
void printing(char const *myString)
|
|
|
|
{printf("String %s\n",myString);} //Hello
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
void printing(int myInt)
|
|
|
|
{printf("My int is %d",myInt);} //15
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
int main ()
|
|
|
|
{
|
|
|
|
printing("Hello");
|
|
|
|
printing(15);
|
|
|
|
}
|
2014-09-10 22:04:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
//C++ Default Function Arguments
|
|
|
|
void two_ints(int a = 1, int b = 4);
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
two_ints(); // arguments: 1, 4
|
|
|
|
two_ints(20); // arguments: 20, 4
|
|
|
|
two_ints(20, 5); // arguments: 20, 5
|
|
|
|
}
|
2014-09-10 22:04:18 +00:00
|
|
|
|
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
//C++ added the nullptr which is different from 0
|
|
|
|
int *ip = nullptr; // OK
|
|
|
|
int value = nullptr; // error: value is no pointer
|
2014-09-10 22:04:18 +00:00
|
|
|
|
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
///////////////////////////////////////
|
|
|
|
// C++ Additions ontop of C
|
|
|
|
///////////////////////////////////////
|
2014-09-10 22:04:18 +00:00
|
|
|
|
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
///////////////////////////////////////
|
|
|
|
// C++ Namespace
|
|
|
|
///////////////////////////////////////
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
//Namespaces allow you to define your own
|
|
|
|
//functions and variables for use
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
// Use '::' to change variable (or function) scope
|
|
|
|
// Putting '::' before a function or variable will
|
|
|
|
// reference a global scope
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
// This allows you to make normal c library calls
|
|
|
|
// std is for standard library
|
|
|
|
using namespace std;
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
#include <stdio.h>
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
int counter = 50; // global variable
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
// Namespaces can be nested
|
2014-09-10 22:04:18 +00:00
|
|
|
|
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
namespace myFirstNameSpace
|
|
|
|
{
|
|
|
|
namespace myInnerSoul
|
|
|
|
{
|
|
|
|
cos(int x)
|
|
|
|
{
|
|
|
|
printf("My inner soul was made to program.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
namespace anotherNameSpace
|
|
|
|
{
|
|
|
|
cos(int x) {;} //does nothing
|
|
|
|
}
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
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"
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
myString + ' You'; // => "Hello You"
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
myString != myOtherString; //True
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
//An example of a string method
|
|
|
|
myString.append(" Dog"); // => "Hello Dog"
|
2014-09-10 22:04:18 +00:00
|
|
|
|
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
///////////////////////////////////////
|
|
|
|
// C++ Input Output
|
|
|
|
///////////////////////////////////////
|
|
|
|
|
|
|
|
//C++ input and output streams
|
|
|
|
//cin, cout, cerr, << is insertion and >> is extraction operator
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace std;
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
|
|
|
|
int myInt;
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
//Prints to stdout (or terminal/screen)
|
|
|
|
cout << "Enter your fav number:\n";
|
|
|
|
//Takes in input
|
|
|
|
cin >> myInt;
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
//cout can also be formatted
|
|
|
|
cout << "Your fav number is " << myInt << "\n";
|
|
|
|
//Your fav number is ##
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
cerr << "Used for error messages";
|
|
|
|
}
|
2014-09-10 22:04:18 +00:00
|
|
|
|
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
///////////////////////////////////////
|
|
|
|
// C++ Classes
|
2014-09-10 22:04:18 +00:00
|
|
|
///////////////////////////////////////
|
|
|
|
|
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
//First example of classes
|
|
|
|
#include <iostream>
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
//define a class
|
|
|
|
class Doggie
|
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
int weight;
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
// These are only the declarations
|
|
|
|
//Can also have private and protected
|
|
|
|
public:
|
|
|
|
//The public methods (can also include variables)
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
// Default constructor
|
|
|
|
Doggie();
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
void setName(std::string dogsName);
|
|
|
|
void setWeight(int dogsWeight);
|
|
|
|
void printDog();
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
//Can define functions within class declaration too
|
|
|
|
void dogBark() {std::cout << "Bark Bark\n"}
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
//Destructors are methods that free the allocated space
|
|
|
|
~doggieDestructor();
|
|
|
|
//if no destructor compiler defines the trivial destructor
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
//Classes are similar to structs and must close the } with ;
|
|
|
|
};
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
// This is the implementation of the class methods
|
|
|
|
// Also called the definition
|
|
|
|
void Doggie::Doggie () {
|
|
|
|
std::cout << "A doggie is born. Woof!\n";
|
|
|
|
}
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
void Doggie::setName (std::string doggie_name) {
|
|
|
|
name = doggie_name;
|
|
|
|
}
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
void Doggie::setWeight (int doggie_weight) {
|
|
|
|
weight = doggie_weight;
|
|
|
|
}
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
void Doggie::printDog () {
|
|
|
|
std::cout << "Dog is " << name << " weighs" << weight << "\n";
|
|
|
|
}
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
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;
|
|
|
|
}
|
2014-09-10 22:04:18 +00:00
|
|
|
|
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
//C++ Class inheritance
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
class German_Sheperd : public Doggie
|
2014-09-10 22:04:18 +00:00
|
|
|
{
|
2014-09-24 03:12:21 +00:00
|
|
|
//This class now inherits everything public and protected from Doggie class
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
//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;
|
2014-09-10 22:04:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
```
|
2014-10-10 05:57:52 +00:00
|
|
|
Futher Reading
|
|
|
|
|
|
|
|
for more resources see: http://www.icce.rug.nl/documents/cplusplus/
|
2014-09-10 22:04:18 +00:00
|
|
|
|
2014-10-10 05:57:52 +00:00
|
|
|
for other reference material: http://www.cplusplus.com/doc/tutorial/
|