Introduction to C++
C++ is a general-purpose programming language created by Bjarne Stroustrup as an extension of the C language. It supports both procedural and object-oriented programming paradigms.
Key Features of C++
- Object-Oriented: Supports encapsulation, inheritance, and polymorphism
- Platform Dependent: C++ programs are platform-dependent and need to be compiled for each platform
- Mid-level Language: Can be used for both system programming and application development
- Rich Library Support: Has a rich set of library functions
- Memory Management: Supports dynamic memory allocation
Basic Structure of a C++ Program
using namespace std; directive allows us to use names for objects and variables from the standard library without the std:: prefix.
Tokens and Data Types
Tokens are the smallest individual units in a program. C++ has the following tokens:
Types of Tokens
- Keywords: Predefined reserved words with special meaning (e.g., int, float, if, for)
- Identifiers: Names given to variables, functions, etc.
- Constants: Fixed values that don't change
- Strings: Sequence of characters
- Operators: Symbols that perform operations
Data Types in C++
- Basic Data Types: int, char, float, double, bool, void
- Derived Data Types: array, pointer, reference
- User-defined Data Types: structure, union, enum, class
Operators in C++
Operators are symbols that perform operations on operands.
Types of Operators
- Arithmetic Operators: +, -, *, /, %
- Relational Operators: ==, !=, >, <, >=, <=
- Logical Operators: &&, ||, !
- Assignment Operators: =, +=, -=, *=, /=
- Increment/Decrement Operators: ++, --
- Bitwise Operators: &, |, ^, ~, <<, >>
- Ternary Operator: ? :
Control Structures
Control structures determine the flow of execution in a program.
Decision Making Statements
- if statement: Executes a block if condition is true
- if-else statement: Executes one block if true, another if false
- switch statement: Selects one of many code blocks to execute
Looping Statements
- for loop: Executes a block for a specific number of times
- while loop: Executes a block while condition is true
- do-while loop: Executes a block at least once, then repeats while condition is true
Functions in C++
A function is a block of code that performs a specific task.
Function Components
- Function Declaration: Tells the compiler about a function's name, return type, and parameters
- Function Definition: Provides the actual body of the function
- Function Call: Invokes the function
Types of Functions
- Library Functions: Predefined in C++ libraries
- User-defined Functions: Created by the programmer
Object-Oriented Programming (OOP) Concepts
OOP is a programming paradigm that uses objects and classes to organize code.
Four Pillars of OOP
- Encapsulation: Wrapping data and functions into a single unit (class)
- Abstraction: Hiding complex implementation details and showing only essential features
- Inheritance: Creating new classes from existing ones
- Polymorphism: Ability to take many forms
Class and Object
A class is a blueprint for objects, and an object is an instance of a class.
Constructors and Destructors
Constructors
A constructor is a special member function that is automatically called when an object is created.
- Default Constructor: Has no parameters
- Parameterized Constructor: Has parameters
- Copy Constructor: Initializes an object using another object of the same class
Destructors
A destructor is a special member function that is automatically called when an object is destroyed.
Inheritance
Inheritance allows a class to inherit properties and characteristics from another class.
Types of Inheritance
- Single Inheritance: A class inherits from one base class
- Multiple Inheritance: A class inherits from multiple base classes
- Multilevel Inheritance: A class inherits from a derived class
- Hierarchical Inheritance: Multiple classes inherit from a single base class
- Hybrid Inheritance: Combination of two or more types of inheritance
Polymorphism
Polymorphism allows objects of different classes to be treated as objects of a common super class.
Types of Polymorphism
- Compile-time Polymorphism (Static): Achieved through function overloading and operator overloading
- Runtime Polymorphism (Dynamic): Achieved through function overriding using virtual functions
Templates
Templates allow generic programming by enabling functions and classes to operate with generic types.
Function Templates
Class Templates
Exception Handling
Exception handling allows programs to handle runtime errors gracefully.
Exception Handling Keywords
- try: Block of code to test for errors
- catch: Block of code to handle the error
- throw: Throws an exception when a problem is detected
File Handling
C++ provides fstream library for file operations.
File Stream Classes
- ofstream: Output file stream (for writing to files)
- ifstream: Input file stream (for reading from files)
- fstream: General file stream (for both reading and writing)