Download presentation
Presentation is loading. Please wait.
1
Finally! Discussing a language!
2
History Born around 1979 as "C with Classes" Renamed to C++ in 1983
Standardized in 1998 Languished for thirteen years Save for minor updates in 2003 Has been updated with new features every three years since 2011
3
Distinguishing Features
Low-cost abstractions Inheritance-based polymorphism accomplished with virtual function tables Type-aware template polymorphism can ensure that generic code is legal on the type for which it is instantiated Type-specific optimizations can be made on generic code
4
Vestiges of C Generally a superset of C syntax
You can basically just write C code and call it C++ Header and Source files Header files are used to expose function (and class) "prototypes" Source files contain the actual implementations
5
Header & Source Files For regular classes, any static members or methods must be implemented in the source file or you will end up with multiple definitions at linking time.
6
Compiling and Linking
7
Compiling and Linking
8
Compiling and Linking
9
Compiling and Linking
10
Compiling and Linking
11
Compiling and Linking
12
Operator Overloading class Container { public:
int operator[](int index) { return m_storage[index]; }
13
Templates template <typename T> class Container { public:
T operator[](int index) { return m_storage[index]; }
14
Const template <typename T> class Container { public:
const T& operator[](int index) const { return m_storage[index]; }
15
Constructor/Destructor
Constructor called whenever a new variable is created With new, like Java Or just with a local variable
16
Constructor/Destructor
Destructor called exactly when the variable falls from scope or when delete is called Contrast this with Java, for which finalize is called when garbage collected, which may be long after the last reference is lost
17
Constructor/Destructor
class A { public A() {cout << "A+" << endl;} public ~A() {cout << "A-" <<endl;} }; class B { public B() {cout << "B+" << endl;} public ~B() {cout << "B-" <<endl;} int main() { { A a; B b; } A aa;
18
Constructor/Destructor
class A { public A() {cout << "A+" << endl;} public ~A() {cout << "A-" <<endl;} }; class B { public B() {cout << "B+" << endl;} public ~B() {cout << "B-" <<endl;} int main() { { A a; B b; } A aa; OUTPUT: A+ B+ B- A-
19
Rule of Three Prior to C++11 If you define any of Destructor
Copy Constructor Assignment Operator You should probably define all three
20
Rule of Five After C++11 If you define any of Destructor
Copy Constructor Move Constructor Assignment Operator Move Assignment Operator You should probably define all five
21
Method Syntax class class_name : public parent_class_name { public:
//constructor class_name( <args> ); //copy constructor class_name(const class_name& other); //destructor virtual ~class_name(); //pure virtual method virtual method_name( <args> ) = 0; }; // <- don't forget the semicolon
22
Reference Counting #include <memory> std::shared_ptr<type>
Create with: auto ptr = std::shared_ptr<type>( new type( <args>) ); auto ptr = std::make_shared_ptr<type>(< constructor args>); If p is a shared_ptr pointing to something auto q = p, will increase the reference count
23
Handy Bitwise Operators
Testing the nth least significant bit of x (1<<n)&x
24
Compiling C++11 g++ -std=c++11 test.cpp -o test
Compiles a the file test.cpp with C++11 support and saves the program as 'test'
25
Casting Pointers In C, you could whatever you want int X;
int* ptr = &X; float* ptr_to_float; ptr_to_float = (float*)ptr;
26
This is ridiculously dangerous!
Casting Pointers This is ridiculously dangerous!
27
Casting Pointers in C++
Assume that class B is derived from class A B instance_of_B; A* ptr_to_A = &instance_of_b; B* ptr_to_B = dynamic_cast<B>(ptr_to_A);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.