cpp new l2cl notes(k)
Data Types
Basic Data Types:
Basic data types are the foundational types provided by programming languages.
Primitive Types: These are pre-defined data types, usually including:
char(1 byte)int(4 bytes)short(2 bytes)long(8 bytes)float(4 bytes)double(8 bytes)boolean(1 byte)
Derived Types: Such as arrays, functions, pointers.
User-defined Types: Structures, unions, enums, and classes.
/
Memory Size of Data Types
Memory Allocations for Different Types:
Bit Sizes:
16-bit: 2 bytes
32-bit: 4 bytes
64-bit: 8 bytes
Storage Sizes:
1 KB = 1024 Bytes
1 MB = 1024 KB
1 GB = 1024 MB
Functions
What is a Function?
A function is a block of statements designed to perform a specific task.
It allows for code reuse and modular programming.
Types of Functions:
Predefined Functions: Already defined in programming libraries (e.g.,
getch(),clrscr(),strcmp()).User-defined Functions: Defined by the user to perform specific tasks.
Syntax of Functions:
return_type function_name(parameter_list) { /* statements */ }
Function Calls
Types of Calling Functions:
Call by Value:
A copy of the variable is passed to the function.
Changes made in the function do not affect the original variable.
Call by Reference:
The address of the variable is passed, allowing changes in the function to reflect in the original variable.
Recursion
What is Recursion?
A technique where a function calls itself in order to solve a problem.
Example: Factorial function implementation:
int factorial(int n) { if (n <= 0) return 1; else return n * factorial(n-1); }
Pointer Basics
Pointers:
A pointer is a variable that stores a memory address.
Syntax to declare a pointer:
datatype *pointer_name;Example:
int *p; int x = 10; p = &x; // p now holds the address of x
Arrays
What is an Array?
A collection of elements of the same type that are stored in contiguous memory locations.
Types of Arrays:
1D (Single-Dimensional) Array:
Example in C++:
int x[5] = {10, 20, 30, 40, 50};
2D (Multi-Dimensional) Array:
Example in C++:
int n[3][3] = {{10,20,30}, {40,50,60}, {70,80,90}};
Structures and Unions
Structures (struct):
Used to define a data type that can hold multiple variables of different types.
struct Student { int roll_no; float percent; int age; };Unions:
Similar to structures but uses a shared memory location for its members.
union Student { int roll_no; float percent; int age; };
Object-Oriented Programming Concepts
Classes and Objects:
A class is a blueprint for creating objects (instances of the class).
An object consists of data (attributes) and methods (functions).
Constructor and Destructor:
Constructor: A special function called when an object is created to initialize the object.
Destructor: A special function called when an object is destroyed to free resources.
Inheritance:
A mechanism where one class inherits the properties and behavior of another class.
Types: Single-Level, Multi-Level, Hierarchical, Multiple, Hybrid.
Polymorphism:
The ability of a function, class, or object to take on many forms.
Achieved through function overloading (compile-time) and function overriding (run-time with virtual functions).
Exception Handling
Exception Handling in C++:
Mechanism to handle errors gracefully without crashing the program.
Keywords:
try,catch,throw.Example:
try { // code that may throw exception } catch (const char* msg) { // error handling code }
Friend Functions
Friend Function: A function that is given access to the private members of a class.
Declared inside the class but defined outside.
Syntax:
friend return_type function_name(parameters);
Dynamic Arrays
Dynamic Arrays: Arrays whose size can be determined during runtime, providing flexibility to manage memory.
Example:
int *arr = new int[n]; // dynamically allocate space for n integers