CPP Notes
CPP Notes
Templates - Templates provide the ability to use a data type as a parameter in functions and
classes. These are referred to as generic types. This provides the ability to define a set of
related classes or functions that can operate on many different types with a single declaration.
Templates are like macros in that they are expanded at compile-time.
Function Templates:We can define a template for a function. For example, if we have an add()
function, we can create versions of the add function for adding the int, float or double type values.
Generic functions- Generic functions use the concept of a function template. Generic functions
define a set of operations that can be applied to the various types of data.
o The type of the data that the function will operate on depends on the type of the data
passed as a parameter.
o For example, Quick sorting algorithm is implemented using a generic function, it can be
implemented to an array of integers or array of floats.
o A Generic function is created by using the keyword template. The template defines what
function will do.
Function Templates with Multiple Parameters We can use more than one generic type in
the template function by using the comma to separate the list.
Overloading a Function Template We can overload the generic function means that the
overloaded template functions can differ in the parameter list.
File handling is used to store data permanently in a computer. Using file handling we can store our
data in secondary memory (Hard disk).
A file is sequence of bites, bytes, lines or records whose meaning is defined by its creator and
user.
1.To define a file properly it is necessary to consider the operation which can be performed on
files. The os system provides most of the essential file manipulators services such as create, open, write,
read, rewind, close, and delete.
Stream classes
ofstream:-
3.Inherits the functions put(), write(), seekp() and tellp() functions from the ostream.
ifstream:-
3.Inherits the functions get(), getline(), read(), seekg() and tellg() functions from the istream.
fstream:-
1.This class provides support for simultaneous input and output operations.
2.Inherits all the functions from istream and ostream classes through iostream.
Chapter 7 managing console i/o operations
streams
In C++, streams are a way to handle input and output operations in a flexible and efficient manner. They
provide a uniform way to perform input and output operations regardless of the specific source or
destination, whether it's a file, console, or any other device.
C++ stream classes are part of the C++ Standard Library and are used for input and output operations.
These classes are organized in a hierarchy where each class represents a different type of stream or
stream buffer.
unformatted
Unformatted I/O operations in C++ refer to input and output operations that deal with raw data without
any specific formatting or interpretation. In other words, unformatted I/O allows you to read or write
data directly without any additional formatting or processing.
Unformatted input operations typically involve reading raw data from a source, such as a file or
keyboard, and storing it directly into memory without any transformation. This can be useful when you
need to process the raw data in a specific way or when the data doesn't have a specific format that
needs to be followed.
Formatted
Formatted I/O operations in C++ involve reading and writing data in a specific format. This means that
data is displayed or interpreted according to a predefined format, which can include things like spacing,
alignment, precision for floating-point numbers, and more. Formatted I/O operations are commonly
used when you need to present data in a human-readable form or when you need to parse data that
follows a specific format.
In C++, formatted I/O operations are typically performed using various manipulators and formatting
functions provided by the iostream library. These manipulators allow you to control various aspects of
the output, such as specifying the number of decimal places for floating-point numbers, setting the
width of output fields, controlling alignment, and more.
Output formatting using manipulators in C++ involves using special functions and objects provided by the
iostream library to control the appearance of data when it's output to the console or other output
streams. Manipulators are essentially functions or objects that modify the behavior of the output
stream.
User-defined manipulators
User-defined manipulators in C++ are custom functions or objects that you can create to modify the
behavior of the input or output streams. These manipulators are typically implemented as overloaded
insertion (<<) or extraction (>>) operators, allowing you to use them in a similar way to standard
manipulators provided by the C++ standard library.
Chapter 6 polymorphims
The word “polymorphism” means having many forms. In simple words, we can define polymorphism as
the ability of a message to be displayed in more than one form. A real-life example of polymorphism is a
person who at the same time can have different characteristics. A man at the same time is a father, a
husband, and an employee. So the same person exhibits different behavior in different situations. This is
called polymorphism. Polymorphism is considered one of the important features of Object-Oriented
Programming.
Unary operators are overloaded by defining member functions for the class representing the operand.
These functions have no arguments for unary postfix operators and one argument for unary prefix
operators. The return type can be any type, including a reference to the modified object.
Binary operators are overloaded by defining member functions for the class representing the left
operand or using a non-member function if the left operand is not an object of the class. For member
functions, the left operand is implicitly passed as this, while for non-member functions, both operands
are explicitly passed as arguments.
In C++, operator overloading using friend functions allows non-member functions to access private and
protected members of a class, enabling operator overloading without making the function a member of
the class. This is particularly useful when overloading binary operators, as it allows symmetrical behavior
between different types.
To overload an operator using a friend function, you declare the function as a friend of the class in which
you want to overload the operator. Then, you define the function normally, taking two arguments: the
operands of the operator. Inside the function, you can access the private or protected members of the
class as if it were a member function.
Overloading insertion (<<) and extraction (>>) operators in C++ allows you to define custom behavior for
input and output operations involving objects of your own classes. This allows you to use the familiar
syntax of stream insertion and extraction with objects of your custom types.
To overload these operators, you define them as friend functions or as regular member functions of your
class. For input (extraction), the >> operator should be overloaded, and for output (insertion), the <<
operator should be overloaded.
String manipulation
String manipulation using operator overloading involves defining custom behavior for operators to
perform operations on strings, such as concatenation, comparison, and other transformations. By
overloading operators, you can create more expressive and convenient interfaces for string manipulation
operations.
Runtime Polymorphism
This type of polymorphism is achieved by Function Overriding. Late binding and dynamic polymorphism
are other names for runtime polymorphism. The function call is resolved at runtime in runtime
polymorphism. In contrast, with compile time polymorphism, the compiler determines which function
call to bind to the object after deducing it at runtime.
this pointer:
1.The this pointer is a pointer available in every member function of a class, pointing to the object for
which the member function is called.
2.It allows member functions to access the object's data members and call other member functions.
3.When a non-static member function is called on an object, a hidden this pointer is passed as an
implicit argument to the function, pointing to the object on which the function was called.
Pointers to objects:
1.Pointers to objects are pointers that store the memory address of objects.
2.They allow dynamic allocation of memory for objects on the heap and provide a way to access and
manipulate objects indirectly.
3.Pointers to objects can be used to invoke member functions and access member variables of the object
they point to.
1.Pointers to derived classes are pointers that store the memory address of objects of derived classes.
2.They can be used to achieve polymorphic behavior when combined with virtual functions and
inheritance.
3.When a pointer to a base class is used to point to an object of a derived class, and virtual functions are
called through that pointer, the behavior is determined at runtime based on the actual type of the object
being pointed to.
Virtual Functions:
1.A virtual function is a member function of a base class that is declared with the virtual keyword and
can be overridden by derived classes.
2.When a virtual function is called using a pointer or reference to a base class, the actual function that
gets executed is determined dynamically at runtime based on the type of the object being pointed to or
referenced.
3.This mechanism is known as dynamic dispatch or late binding, and it enables polymorphic behavior,
where different derived classes can provide their own implementations of the same function declared in
the base class.
4.Virtual functions allow for runtime polymorphism, allowing code to be written based on interfaces
rather than specific implementations.
1.A pure virtual function is a virtual function that is declared in a base class but has no implementation
(no function body).
2.Pure virtual functions are declared using the virtual keyword followed by = 0.
3.A class containing at least one pure virtual function is known as an abstract class, and objects of
abstract classes cannot be instantiated.
4.Derived classes must override pure virtual functions to provide their own implementations; otherwise,
they will also be abstract classes.
5.Pure virtual functions define an interface that derived classes must implement, ensuring that they
provide specific functionality.
6.Pure virtual functions are useful for defining abstract interfaces and enforcing a common interface for
derived classes while allowing flexibility in implementation details.
Chapter 5 inheritance
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class (called a
derived class or subclass) to inherit properties and behaviors from another class (called a base class or
superclass). Inheritance forms an "is-a" relationship between classes, where the derived class is a
specialized version of the base class.
1.The class whose members are inherited by another class is called the base class or superclass.
2.It defines common properties and behaviors shared by its derived classes.
1.The class that inherits properties and behaviors from a base class is called the derived class or subclass.
3.It inherits data members and member functions from the base class and can add its own members.
Types of Inheritance:
1.Single Inheritance: A derived class inherits from only one base class.
4.Hierarchical Inheritance: Multiple classes are derived from a single base class.
A virtual base class is a class that serves as a base class for multiple derived classes, and it resolves the
ambiguity that arises when a class is derived from multiple instances of the same base class. When a
base class is declared as virtual, the derived classes share a single instance of the base class.
Abstract Class:
An abstract class is a class that contains at least one pure virtual function. Abstract classes cannot be
instantiated directly; instead, they serve as base classes from which other classes can be derived. The
pure virtual functions in abstract classes define an interface that derived classes must implement.
Constructors in derived classes are special member functions that are used to initialize objects of the
derived class. They are responsible for initializing the base class subobject and the additional members
introduced by the derived class.
Chapter 4 constructors
Constructors are special member functions in C++ that are automatically called when an object of a class
is created. Their primary purpose is to initialize the newly created object. Constructors have the same
name as the class and do not have a return type, not even void. They are essential for setting up the
initial state of objects and ensuring they are in a valid state after creation.
Types of Constructors:
Default Constructor:
1.A default constructor is a constructor that takes no arguments or has all its parameters provided with
default values.
2.If a class does not explicitly define any constructors, the compiler provides a default constructor
automatically.
3.The default constructor initializes data members to their default values (zero-initialized for built-in
types, default-constructed for class types).
Parameterized Constructor:
1.A parameterized constructor is a constructor that accepts one or more parameters to initialize data
members based on user-provided values.
2.It allows objects to be initialized with specific values at the time of creation.
Copy Constructor:
1.A copy constructor is a constructor that initializes an object using another object of the same class.
2.It is called when an object is copied, such as when passing objects by value, returning objects from
functions by value, or initializing one object with another.
3.If a class does not explicitly define a copy constructor, the compiler provides a default copy constructor,
which performs a member-wise copy of the object.
Multiple constructors
Multiple constructors in a class refer to having more than one constructor defined within the same class.
Each constructor can have a different parameter list or different initialization logic, allowing objects to be
created in various ways.
Constructors with default arguments are a feature of C++ that allows parameters of a constructor to
have default values. This means that if the constructor is called without providing values for these
parameters, the default values will be used instead.
Dynamic initialization of constructors - typically refers to a scenario where the constructor of an object is
executed during runtime, rather than at compile time. However, in C++, constructors are always
executed at the time of object creation, whether it's a static or dynamic allocation.
dynamic constructor
In C++, there isn't a concept known as a "dynamic constructor" in the language itself. However, the term
might be used informally to refer to constructors that are invoked dynamically during runtime.
Constructors in C++ are special member functions that are automatically called when an object is
created, regardless of whether the object is created statically (at compile time) or dynamically (at
runtime using new).
destructor
In C++, a destructor is a special member function of a class that is automatically called when an object of
that class goes out of scope or is explicitly deleted. Its primary purpose is to release resources or
perform cleanup tasks associated with the object before it is destroyed.
Chapter 3 classes and ojects
Structure:
1.A structure is a user-defined data type in C++ that groups together related data items under a single
name.
2.Members of a structure can be a mixture of data types, including other structures, arrays, pointers, etc.
4.Structures are typically used for grouping related data that do not require complex member functions
or encapsulation.
Class:
1.A class is also a user-defined data type in C++ that groups together related data and functions under a
single name.
2.Members of a class can include data members (variables) and member functions (methods).
4.Classes support encapsulation, inheritance, and polymorphism, making them suitable for modeling
complex real-world entities and implementing object-oriented programming concepts.
Access specifiers
Access specifiers in C++ are keywords used to control the accessibility of class members (data members
and member functions) from outside the class. There are three access specifiers:
public:
1.Members declared as public are accessible from outside the class through object instances.
2.They can be accessed by any function or object, whether it's a member of the class or not.
3.Public members are commonly used to define the interface of the class, allowing users to interact with
it.
private:
1.Members declared as private are accessible only from within the class itself.
2.They cannot be accessed directly from outside the class, even by object instances.
3.Private members are hidden from the outside world and are typically used for internal implementation
details.
protected:
1.Similar to private members, protected members are also accessible only from within the class itself.
Defining data members involves declaring variables within the body of the class. These data members
represent the attributes or properties of the objects created from the class.
When you define a member function inside the class, it is considered an inline function. This means that
the function code is included directly at the call site, potentially improving performance.
The member function is declared inside the class like a normal function
Defining a member function outside the class requires the function declaration (function prototype) to
be provided inside the class definition.
The member function is declared inside the class like a normal function, but its definition is outside the
class.
In C++, member functions are created and placed in the memory space only once when they
are defined as a part of a class specification. Since all the objects belonging to that class use
the same member functions, no separate space is allocated for member functions. Only space
for member variables is allocated separately for each object. Separate memory locations for the
objects are essential because the member variable will hold different data values for different
objects. Memory for objects can be allocated on the stack or the heap area.
A static data member is a data member that gets memory only once for the whole class, no
matter how many objects of the class are created. It is shared among all objects of the
class. Static member functions are special functions used to access static data members or
other static member functions. They share a single copy of the member function across all
class objects. The static data member is always initialized to zero when the first class object is
created.
An array of objects
An array of objects in C++ is simply an array where each element is an object of a particular class. It
allows you to store multiple objects of the same class sequentially in memory as elements of the array.
In C++, you can declare a function (including member functions) as a friend of a class. When a function is
declared as a friend of a class, it can access the private and protected members of that class. This is
useful when you need to grant access to private or protected members of a class to specific functions or
other classes without making those members public.
An object as a friend function argument refers to passing an object of a class as an argument to a friend
function. This allows the friend function to operate on the private or protected members of the class
using the provided object.
Friend Function:
A friend function is a standalone function (not a member function of the class) that is granted access to
the private and protected members of a class. It is declared as a friend of the class within the class
definition.
Friend Class:
A friend class is a class that is granted access to the private and protected members of another class. It is
declared as a friend of the class within the class definition.
function returning an object is a function in C++ that returns an instance of a class as its result. This
allows you to create and return objects of a particular class from a function, enabling flexible and
reusable code.
Chapter 2 beginning with c++
Data types
1.Integer: Represents whole numbers without fractional parts (e.g., int, short, long).
1.Array: Represents a collection of elements of the same type arranged in contiguous memory locations.
2.Structure: Represents a collection of variables (of different types) under a single name.
3.Union: Represents a data structure that can hold different data types in the same memory location.
keywords
Keywords are used to define the syntax and structure of a program, and they typically represent
language constructs, control flow statements, data types, and other language features.
It is used to access members (variables or functions) of a class, especially when you are defining member
functions outside the class definition.
It can be used to access global variables from within a function, especially when there is a local variable
with the same name.
In C++, memory management operators are used to allocate and deallocate dynamic memory during
program execution. They provide the programmer with explicit control over memory allocation and
deallocation, which is particularly useful when working with data structures that require dynamic
memory allocation, such as arrays and linked lists.
Manipulators
In C++, manipulators are functions or objects used to modify the behavior of input/output streams, such
as std::cin and std::cout. They are part of the <iomanip> header in the C++ Standard Library and are used
to format input and output data in a specific way.
Function Prototyping:
Call by Reference:
Call by reference is a method of passing arguments to functions in which the actual memory address
(reference) of the arguments is passed to the function parameters. This allows the function to directly
access and modify the original variables that were passed as arguments.
Return by Reference:
Return by reference is a method of returning values from functions in which the function returns a
reference to a variable. This allows the function to return a value that can be directly assigned to another
variable and modified.
Inline functions
Inline functions are a feature in C++ that suggests the compiler to insert the function's code directly at
the point of each function call, rather than generating a separate function call mechanism. This can
result in faster execution of the program by avoiding the overhead of function call overhead, such as
pushing arguments onto the stack, creating a new stack frame, and returning from the function.