In [1]:
%load_ext cppmagic
int double float short long char struct enum union void signed unsigned
auto const extern static volatile
if else switch case
for while do continue break
default goto register sizeof typedef return
bool true false wchar_t
class namespace this
new delete
try catch throw
public protected private
dynamic_cast reinterpret_cast const_cast static_cast
asm explicit mutable typeid operator template typename friend using inline virtual
count : 62
In [2]:
%%cpp
#include <stdio.h>
int main() {
puts("Hello World");
return 0;
}
In [3]:
%%cpp
#include <cstdio>
int main() {
for (int i = 0; i < 3; i++)
puts("Hello World");
return 0;
}
In [5]:
%%cpp
#include <iostream>
int main() {
std::cout << "Hello World\n";
return 0;
}
Contain memory location of a variable.
Created with: TYPENAME *ptr
.
&VARIABLE_NAME
*POINTER_NAME
.int a = 7;
int *ptr = &a; // ptr 'points' to a
cout << a; // 7
cout << ptr; // memory location (0x7fff5500e792)
cout << *ptr; // 'dereference' a (= 7)
*ptr = 12; // 'a' now equals 12
cout << a; // 12
In [6]:
%%cpp
#include <iostream>
using namespace std;
int main() {
int a = 7, b = 9;
int *ptr = &a;
cout << a << '\n';
cout << ptr << '\n';
cout << *ptr << '\n';
*ptr = 12;
cout << "a == " << a << "\n";
return 0;
}
In [8]:
%%cpp
#include <iostream>
using namespace std;
void add_three(int& x) {
x += 3;
cout << "X is at location: " << &x << "\n";
}
int main() {
int a = 7;
cout << a << '\n';
add_three(a);
cout << a << '\n';
cout << "a is at location: " << &a << "\n";
}
C : malloc(), free()
← Standard System Calls int *a = malloc(sizeof(int) * 100);
...
free(a);
C++: new, delete
← Language keywordsint *i = new int; // create integer in dynamic memory space
// (the 'heap')
*i = 1025; // Set the value at memory position 'i' to 1025
int *a = new int[100]; // create 100 integers -
// size is figured out for you!
*(a + 3) = 8; // Set the third value from memory
// position 'a' to 8
a[3] = 4; // Set the same value to 4
a[0] = 31415; // Set the ZEROTH value to 31415
delete i;
delete[] a;
In [26]:
%%cpp
#include <iostream>
using namespace std;
int main()
{
int *a = new int;
cout << "a : " << a << ' ' << *a << "\n";
*a = 100;
cout << "a : " << a << ' ' << *a << "\n";
delete a;
return 0;
}
In [9]:
%%cpp
#include <cstdio>
using namespace std;
void foo(int a) {
printf("int: %d\n", a);
}
void foo(float a) {
printf("float: %f\n", a);
}
void foo(double a) {
printf("double: %f\n", a);
}
int main() {
foo(10);
foo(10.);
foo(10.f);
return 0;
}
Functions must be declared BEFORE use
In [17]:
%%cpp
#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
printstuff();
return 0;
}
void printstuff() {
cout << "cout : " << std::scientific << 88923749827.234 << " : " << 88923749827.234 << "\n";
printf("printf: %e : %f ", 88923749827.234, 88923749827.234);
}
In [16]:
%%cpp
#include <cstdio>
#include <iostream>
using namespace std;
void printstuff();
int main()
{
printstuff();
return 0;
}
void printstuff() {
cout << "cout : " << std::scientific << 88923749827.234 << " : " << 88923749827.234 << "\n";
printf("printf: %e : %f ", 88923749827.234, 88923749827.234);
}
Group data and functions together into classes.
Create objects from classes
area(circle)
area(rectangle)
scalar * scalar
vector * vector
scalar * vector
#include <iostream>
std::cout
- standard outputstd::cin
- standard inputstd::cerr
- error output#include <fstream>
#include <sstream>
>>
and <<
int *ptr;
std::cout << "one " << ptr << ' ' << *ptr << " " << 3;
int i;
std::cout << "Please type an integer: ";
std::cin >> i;
if (i < 100) {
std::cerr << "Error, number too small!\n";
exit(1);
}
In [42]:
%%cpp
#include <iostream>
#include <sstream>
int main()
{
int i = 9;
std::stringstream ss;
ss << "one " << 2 << ' ' << 3.0 << " " << i << ' ' << &i << "\n";
const char *ptr = ss.str().c_str();
printf("%p %s\n", ptr, ptr );
return 0;
}
In [49]:
%%cpp
#include <iostream>
using namespace std;
template <typename T>
void foo(T x) {
cout << "[foo] " << x << '\n';
}
void foot(char x) {
cout << "[foot] " << x << '\n';
}
int main() {
foo<char*>(78);
foot(78);
return 0;
}
push_back()
/insert()
#include <vector>
std::vector<int> vi; // vector of integers
std::vector<float> vf; // vector of floats
std::vector<std::vector<int> > vv; //vector of int vectors
In [18]:
%%cpp
#include <vector>
#include <iostream>
int main()
{
std::vector<int> *v = new std::vector<int>();
for (int i = 0; i < 200000; i++) {
v->push_back(i * 10);
}
std::cout << "Size : " << v->size() << "\n";
std::cout << "v[19] : " << (*v)[9] << '\n';
delete v;
return 0;
}
In [57]:
%%cpp
#include <vector>
#include <iostream>
int main()
{
// CONSTRUCTOR :: Vector has 20 elements with the number '2'
std::vector<float> v(20);
std::cout << "Size : " << v.size() << "\n";
std::cout << "v[19] : " << v[19] << '\n';
std::cout << "-----\n";
for (int i = 0; i < 20; i++) {
v.push_back(i * 10.5);
}
std::cout << "Size : " << v.size() << "\n";
std::cout << "v[29] : " << v[29] << '\n';
return 0;
}
In [19]:
%%cpp
#include <string>
#include <iostream>
int main() {
std::string s("Hello"); // Create a string with the char[] "Hello";
s += " world"; // Add " " to the end of string 's'
std::cout << s << " :-D\n";
if (s != "Hello world") {
std::cout << "EQUAL!\n";
}
std::cout << "substr: " << s.substr(3, 8) << "\n";
std::cout << s.substr(s.find('o')) << "\n";
return 0;
}
In [22]:
%%cpp
#include <string>
#include <map>
#include <iostream>
int main() {
std::map<int, std::string> m; // Maps numbers to strings
m[17] = "Seventeen";
m[1] = "One";
m[42] = "The answer to life the universe and everything";
m[2] = "The Only even prime";
std::cout << "m.size() == " << m.size() << "\n";
std::cout << "m[17] = '" << m[17] << "'\n";
std::cout << "m[2] = '" << m[2] << "'\n";
return 0;
}
public
: Everybody has accessprotected
: Children have accessprivate
: Only you have accessclass CLASSNAME {
public:
CLASSNAME(); // Constructor
CLASSNAME(int x); // Constructor
void SayHi(); // Method
int GetX() const; // Constant Method
private:
int _x; // Private Member
};
int CLASSNAME::GetX() {
return _x;
}
this
keywordthis
is a pointer to the current object, and can be treated as suchvoid SayHi() {
std::cout << "the object at " << this << " says hi.\n";
}
In [66]:
%%cpp
#include <iostream>
// Class Declaration
class A {
public:
A();
void SayHello();
int _count; // Counts number of times 'A' says "hello"
};
// Constructor Definition
A::A():
_count(0) // initialize _count to zero
{
}
void A::SayHello() {
std::cout << "HELLO! (" << _count++ << ", "<< this <<") \n";
}
int main() {
A a;
a._count = 20;
a.SayHello();
a.SayHello();
a.SayHello();
A b;
b.SayHello();
a.SayHello();
return 0;
}
In [24]:
%%cpp
#include <iostream>
class A {
public:
A() {
std::cout << "Constructing A @" << this << "\n";
}
void Print() {
std::cout << "I am an 'A'\n";
}
};
class B : public A {
public:
B() {
std::cout << "Constructing B @" << this << "\n";
}
};
int main() {
A a;
B b;
std::cout << "---\n";
a.Print(); // A::Print()
b.Print(); // A::Print()
//# A *c = (A*)&b;
//# c->Print();
return 0;
}
class Circle : public Shape {
public:
Circle(double radius): _r(radius){};
double Area() {
return 3.1415 * _r * _r;
}
protected:
double _r;
};
In [25]:
%%cpp
#include <iostream>
using namespace std;
class Shape {
public:
Shape(){}; // constructor
virtual double Area() = 0; // 'pure virtual' function
};
int main()
{
Shape s;
return 0;
}
In [26]:
%%cpp
#include <iostream>
using namespace std;
class Shape {
public:
Shape(){}; // constructor
virtual double Area() = 0; // 'pure virtual' function
};
class Circle : public Shape {
public:
Circle(double radius): _r(radius){};
double Area() {
return 3.1415 * _r * _r;
}
protected:
double _r;
};
class Rectangle : public Shape {
public:
Rectangle(double length, double width): _l(length), _w(width)
{
cout << "Created rectangle with length=" << _l
<< " and width=" << _w << "\n";
};
double Area() {
return _l * _w;
}
protected:
double _l;
double _w;
};
void PrintArea(Shape& s)
{
std::cout << "area == " << s.Area() << endl;
}
int main()
{
Rectangle r(5,10);
Circle c(25.);
PrintArea(r);
PrintArea(c);
cout << "---\n" << "area == " << r.Area() << endl;
return 0;
}
In [ ]:
%%cpp
#include <iostream>
using namespace std;
class Shape {
public:
Shape(){}; // constructor
virtual double Area() = 0; // 'pure virtual' function
};
class Circle : public Shape {
public:
Circle(double radius): _r(radius){};
double Area() {
return 3.1415 * _r * _r;
}
protected:
double _r;
};
int main()
{
Circle c(25.);
cout << "Circle area == " << c.Area() << endl;
return 0;
}
operatorXXX()
, where XXX is the symbolclass A {
protected:
int _x; // protected integer _x
public:
A():_x(0){}; // Default _x = 0
A(int i): _x(i) {}; // Construct with _x
A operator+(A& a) { // ADDITION OPERATOR
return A(_x + a._x); // A+A
}
A operator+(int i) { // ADDITION OPERATOR
return A(_x + i); // A + int
}
A& operator+=(int i) { // ADDITION/ASSIGNMENT
_x += i; // A += int
return *this;
}
void Print() {
std::cout << "My _x == " << _x << "\n";
}
};
In [31]:
%%cpp
#include <iostream>
using namespace std;
class A {
public:
A():_x(0){};
A(int i): _x(i) {};
A operator+(A& a) {
return A(_x + a._x);
}
A operator+(int i) {
return A(_x + i);
}
void operator+=(int i) {
_x += i;
}
void Print() {
std::cout << "My _x == " << _x << "\n";
}
protected:
int _x;
};
int main()
{
A a;
cout << "a: "; a.Print();
a += 4;
cout << "a: "; a.Print();
A b(10), c = a + b;
cout << "b: "; b.Print();
cout << "c: "; c.Print();
return 0;
}
In [ ]: