C Notes
C Notes
functionality can vary depending on the language. I'll provide a general explanation, but
keep in mind that the details may differ based on the programming language you're
using.
In many object-oriented programming languages, such as Java, C++, and JavaScript, the
"new" keyword is used to create an instance of a class or to instantiate an object. When
you use "new," it typically allocates memory for the object, initializes its attributes (if
any), and returns a reference to the newly created object.
It's important to note that the use and behavior of the "new" keyword can vary between
programming languages, and some languages may not use "new" at all for object instantiation.
Always refer to the documentation of the specific programming language you are working with
for accurate and detailed information.
In C++, the delete keyword is primarily used in conjunction with dynamic memory
allocation to free memory that was previously allocated with the new keyword. This is a
key aspect of managing memory in C++ when dealing with pointers and dynamic
memory.
delete is an operator that is used to destroy array and non-
array(pointer) objects which are dynamically created by the new operator.
delete can be used by either using the delete operator or delete [ ]
operator.
The new operator is used for dynamic memory allocation which stores
variables on heap memory.
This means the delete operator deallocates memory from the heap.
The pointer to the object is not destroyed, the value or memory block pointed
by the pointer is destroyed.
The delete operator has void return type which means it does not return any
value.
VIRTUAL FUNCTION
A virtual function (also known as virtual methods) is a member function that is
declared within a base class and is re-defined (overridden) by a derived class.
When you refer to a derived class object using a pointer or a reference to the
base class, you can call a virtual function for that object and execute the
derived class’s version of the method.
Virtual functions ensure that the correct function is called for an object,
regardless of the type of reference (or pointer) used for the function call.
They are mainly used to achieve Runtime polymorphism.
Functions are declared with a virtual keyword in a base class.
The resolving of a function call is done at runtime.
Rules for Virtual Functions
The rules for the virtual functions in C++ are as follows:
1. Virtual functions cannot be static.
2. A virtual function can be a friend function of another class.
3. Virtual functions should be accessed using a pointer or reference of base
class type to achieve runtime polymorphism.
4. The prototype of virtual functions should be the same in the base as well as
the derived class.
5. They are always defined in the base class and overridden in a derived class.
It is not mandatory for the derived class to override (or re-define the virtual
function), in that case, the base class version of the function is used.
6. A class may have a virtual destructor but it cannot have a virtual constructor.
STL
The Standard Template Library (STL) is a set of C++ template classes to
provide common programming data structures and functions such as lists,
stacks, arrays, etc. It is a library of container classes, algorithms, and iterators.
It is a generalized library and so, its components are parameterized. Working
knowledge of template classes is a prerequisite for working with STL.
The C++ Standard Template Library (STL) is a collection of algorithms, data
structures, and other components that can be used to simplify the development
of C++ programs. The STL provides a range of containers, such as vectors,
lists, and maps, as well as algorithms for searching, sorting and manipulating
data.
One of the key benefits of the STL is that it provides a way to write generic,
reusable code that can be applied to different data types. This means that you
can write an algorithm once, and then use it with different types of data without
having to write separate code for each type.
The STL also provides a way to write efficient code. Many of the algorithms and
data structures in the STL are implemented using optimized algorithms, which
can result in faster execution times compared to custom code.
Class Template:
A "class template" refers to a template that defines a blueprint for creating
classes. It is a template that can be parameterized with one or more types or non-
type parameters.
Class templates are used to create generic classes that can work with different
data types without sacrificing type safety.
Template Class:
On the other hand, "template class" can be a more general term
that refers to any class instantiated from a template, whether it's a
class template or a function template.
When you create a specific class using a template, you are
instantiating a template to form a "template class."
Using the previous example, if you create a class like
MyContainer<int>, it would be referred to as a "template class"
because it's an instantiation of the class template MyContainer with
the type int.
STREAM
In C++ there are number of stream classes for defining various streams related
with files and for doing input-output operations. All these classes are defined in
the file iostream.h.
Classes for File stream operations :-
The I/O system of C++ contains a set of classes which define the file
handling methods. These include ifstream, ofstream and fstream classes.
These classes are derived from fstream and from the corresponding
iostream class. These classes, designed to manage the disk files, are
declared in fstream and therefore we must include this file in any program
that uses files.
1. ios:- ios stands for input output stream.
This class is the base class for other classes in this class hierarchy.
This class contains the necessary facilities that are used by all the other
derived classes for input and output operations.
2. istream:-
istream stands for input stream.
This class is derived from the class ‘ios’.
This class handle input stream.
The extraction operator(>>) is overloaded in this class to handle input
streams from files to the program execution.
This class declares input functions such as get(), getline() and read().
3. ostream:-
ostream stands for output stream.
This class is derived from the class ‘ios’.
This class handle output stream.
The insertion operator(<<) is overloaded in this class to handle output
streams to files from the program execution.
This class declares output functions such as put() and write().
4. streambuf:-
This class contains a pointer which points to the buffer which is used to
manage the input and output streams.
5. fstreambase:-
This class provides operations common to the file streams. Serves as a base
for fstream, ifstream and ofstream class.
This class contains open() and close() function.
6. ifstream:-
This class provides input operations.
It contains open() function with default input mode.
Inherits the functions get(), getline(), read(), seekg() and tellg() functions
from the istream.
7. ofstream:-
This class provides output operations.
It contains open() function with default output mode.
Inherits the functions put(), write(), seekp() and tellp() functions from the
ostream.
8. fstream:-
This class provides support for simultaneous input and output operations.
Inherits all the functions from istream and ostream classes through iostream.
9. filebuf:-
Its purpose is to set the file buffers to read and write.
We can also use file buffer member function to determine the length of the
file.
In C++, files are mainly dealt by using three classes fstream, ifstream, ofstream
available in fstream headerfile.
ofstream: Stream class to write on files
ifstream: Stream class to read from files
fstream: Stream class to both read and write from/to files.
1. The ios class: The ios class is responsible for providing all input and output
facilities to all other stream classes.
2. The istream class: This class is responsible for handling input stream. It
provides number of function for handling chars, strings and objects such
as get, getline, read, ignore, putback etc..
REUSABILITY
Reusability in C++ is a key concept in object-oriented programming that emphasizes
writing code in a way that allows components (such as classes and functions) to be used
in multiple contexts and applications. C++ supports reusability through several
mechanisms:
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.
Function Overriding
Function overriding is the mechanism using which a function defined in the base class
is once again defined in the derived class. In this case, we say the function is
overridden in the derived class.
We should remember that function overriding cannot be done within a class. The
function is overridden in the derived class only. Hence inheritance should be present
for function overriding.
The second thing is that the function from a base class that we are overriding should
have the same signature or prototype i.e. it should have the same name, same return
type and same argument list.
What is Memory Management?
Memory management is a process of managing computer memory, assigning the
memory space to the programs to improve the overall system performance.
Delete operator
When memory is no longer required, then it needs to be deallocated so that the
memory can be used for another purpose.
What is a destructor?
Destructor is an instance member function that is invoked automatically
whenever an object is going to be destroyed. Meaning, a destructor is the last
function that is going to be called before an object is destroyed.
A destructor is also a special member function like a constructor. Destructor
destroys the class objects created by the constructor.
Destructor has the same name as their class name preceded by a tilde (~)
symbol.
It is not possible to define more than one destructor.
The destructor is only one way to destroy the object created by the
constructor. Hence destructor can-not be overloaded.
Destructor neither requires any argument nor returns any value.
It is automatically called when an object goes out of scope.
Destructor release memory space occupied by the objects created by the
constructor.
In destructor, objects are destroyed in the reverse of an object creation.
The thing is to be noted here if the object is created by using new or the
constructor uses new to allocate memory that resides in the heap memory or
the free store, the destructor should use delete to free the memory.
The syntax for defining the destructor within the class:
~ <class-name>() {
// some instructions
}
The syntax for defining the destructor outside the class:
<class-name> :: ~<class-name>() {
// some instructions
}
Access Modifiers or Access Specifiers in a class are used to assign the
accessibility to the class members, i.e., they set some restrictions on the class
members so that they can’t be directly accessed by the outside functions.
There are 3 types of access modifiers available in C++:
1. Public
2. Private
3. Protected
1. Public: All the class members declared under the public specifier will be
available to everyone. The data members and member functions declared as
public can be accessed by other classes and functions too. The public members
of a class can be accessed from anywhere in the program using the direct
member access operator (.) with the object of that class.
2. Private: The class members declared as private can be accessed only by the
member functions inside the class. They are not allowed to be accessed directly
by any object or function outside the class. Only the member functions or
the friend functions are allowed to access the private data members of the
class.