Chapter 8: Object and Classes in C++(Part – 2)
Types of Member Function
Includes static and non-static member functions.
Static Keywords
class Math { public: static int count; }; int Math::count = 0;
CONST Keyword
class Person { public: const int age; Person(int a) : age(a) {} };
Array within Class
class Collection { public: int items[5]; };
Class – Structure and Union
Classes support all features of structures and unions with added OOP benefits.
Friend and Nested Class
class A { friend class B; }; class B {};
Container Class
A class containing objects of other classes.
Array of Object
Person people[2];
Data Abstraction
Hiding the implementation details.
Data Encapsulation
Bundling the data and methods that operate on the data.
Objects with various Storage Class
static Person p1; Person p2;
Chapter 9: Inheritance
Basics of Inheritance
Inheritance allows a class to acquire properties of another class.
Inheritance visibility Mode
Specifies how inherited members are accessed.
class Derived : public Base {};
Types of Inheritance
Includes single, multiple, multilevel, hierarchical, and hybrid.
Hybrid Inheritance
Combination of multiple inheritance types.
Ambiguity Inheritance
Resolved using virtual inheritance.
class A {}; class B : virtual public A {}; class C : virtual public A {}; class D : public B, public C {};
Types of Base Class
Abstract and concrete base classes.
Constructor and Inheritance
Base class constructors are called first.
class Derived : public Base { public: Derived() : Base() {} };
Destructor with Inheritance
Base class destructors are called last.
class Derived : public Base { public: ~Derived() {} };
Function Overriding
Derived class redefines base class methods.
class Base { public: virtual void show() { std::cout << "Base"; } }; class Derived : public Base { public: void show() override { std::cout << "Derived"; } };
Composition
Combining objects to form complex types.
Chapter 10: Operator Overloading
Basics of Operator Overloading
Defining custom behavior for operators.
Implementing Operator Overloading
class Complex { public: int real, imag; Complex operator + (const Complex &c) { Complex temp; temp.real = real + c.real; temp.imag = imag + c.imag; return temp; } };
Unary Operator Overloading
class Number { public: int value; Number operator - () { Number temp; temp.value = -value; return temp; } };
Binary Operator Overloading
Complex operator + (const Complex &c);
Relational Operator Overloading
bool operator == (const Complex &c);
Assignment Operator
Complex& operator = (const Complex &c);
Input/Output Operators Overloading
friend std::ostream& operator << (std::ostream &out, const Complex &c);
Function Call Operator { } Overloading
class Functor { public: void operator() () { std::cout << "Functor called"; } };
Subscripting [ ] Operator Overloading
int& operator [] (int index);
Class Member Access Operator (->) Overload
class Ptr { public: Object* operator -> () { return obj; } private: Object *obj; };
Type Casting
operator int() const;
Chapter 11: Polymorphism
Introduction about Polymorphism
Polymorphism allows methods to do different things based on the object it is acting upon.
Virtual Function
class Base { public: virtual void show() { std::cout << "Base"; } }; class Derived : public Base { public: void show() override { std::cout << "Derived"; } };
Pure Virtual Function
class Base { public: virtual void show() = 0; };
Virtual and Pure Virtual Destructor
class Base { public: virtual ~Base() = 0; };
Types of Polymorphism
Compile-time (overloading) and runtime (overriding).
“this” Pointer
class Person { public: void display() { std::cout << this->name; } };
Chapter 12: File Handling
Introduction File Handling and Streams
Files provide a way to store data permanently.
Opening and Closing a File
std::ifstream infile("file.txt"); infile.close();
Reading/Writing Data From Files
std::ofstream outfile("file.txt"); outfile << "Hello, World!"; outfile.close();
Text and Binary Files
Text files store data in human-readable format; binary files store data in binary format.
Chapter 13: Exception Handling
Basic of Exception handling
Exceptions handle runtime errors.
try { // Code that may throw an exception } catch (const std::exception &e) { std::cout << e.what(); }
Throwing and Catching Exceptions
void test() { throw std::runtime_error("Error occurred"); }
Rethrowing Exceptions
try { throw; } catch (...) { std::cout << "Rethrown exception"; }
Nesting Exceptions
try { try { throw std::runtime_error("Error"); } catch (...) { std::cout << "Inner catch"; throw; } } catch (...) { std::cout << "Outer catch"; }
Exception Specification
Specifying which exceptions a function might throw.
void func() throw(int, std::runtime_error);
C++ Standard Exceptions
Includes std::exception
, std::runtime_error
, std::logic_error
, etc.
Chapter 14: Template
Introduction about Templates
Templates allow generic programming.
Types of Templates
Function templates and class templates.
template <typename T> T add(T a, T b) { return a + b; }
Default Parameters For Templates
template <typename T = int> class Test { T data; };
Function Template and Static Variables
template <typename T> T getMax(T a, T b) { static int count = 0; return (a > b) ? a : b; }
Class Template and Static Variable
template <typename T> class Test { static int count; }; template <typename T> int Test<T>::count = 0;
Non-Type Template Arguments
template <typename T, int size> class Array { T arr[size]; };
Chapter 15: Standard Template Library
Introduction about Standard Template Library
STL provides a set of template classes and functions.
Container Library
Includes vector, list, deque, set, map, etc.
#include <vector> std::vector<int> v = {1, 2, 3};
Bitset
Represents a fixed-size sequence of bits.
#include <bitset> std::bitset<8> bits(255);
Iterator
Used to traverse the elements of a container.
std::vector<int>::iterator it = v.begin();
Auto_PTR
Smart pointer for automatic memory management (deprecated in C++11).
#include <memory> std::auto_ptr<int> p(new int(10));
Algorithm
Includes functions for searching, sorting, counting, etc.
#include <algorithm> std::sort(v.begin(), v.end());
First Part – 1st Part
Here is the Link of the First (1st) Part of C++ Cheat Sheet.
PDF Download File
Here You can download the complete PDF file of C++ Cheat Sheet.
Send download link to: