[go: up one dir, main page]

0% found this document useful (0 votes)
29 views3 pages

BSCS 2

Destructors in C++ are special methods that are called when an object is deleted. They are typically used to free dynamically allocated memory for an object's internal data. A destructor name matches the class name prefixed with a tilde. Private destructors prevent automatic object creation, static objects, and global objects by controlling their destruction. Type conversion in C++ can be done implicitly by the compiler or explicitly through casting. Explicit casting allows converting between types like int, float, and strings. Dynamic memory allocation allows programs to request memory at runtime as needed.

Uploaded by

tahir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views3 pages

BSCS 2

Destructors in C++ are special methods that are called when an object is deleted. They are typically used to free dynamically allocated memory for an object's internal data. A destructor name matches the class name prefixed with a tilde. Private destructors prevent automatic object creation, static objects, and global objects by controlling their destruction. Type conversion in C++ can be done implicitly by the compiler or explicitly through casting. Explicit casting allows converting between types like int, float, and strings. Dynamic memory allocation allows programs to request memory at runtime as needed.

Uploaded by

tahir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Destructors in C++

The destructor, like the constructor, belongs to the special functions of the class. A destructor is a special
method that is called when an object is deleted. Typically, the destructor is used to free memory
dynamically allocated for the internal data of the class. There may be other cases of using the destructor,
for example: if a le is created in the class and it is open, then obviously the destructor will perform the
operation of closing the le.

A destructor is a function inverse to the constructor. The destructor name is the same as the class name,
beginned by the character name tilde ‘~’.

For example, if the class has the name CMyClass, then the name of the destructor will be ~CMyClass().

Private Destructor

The use of private destructors is advisable in those cases when the usual methods are forbidden to free
memory for previously created objects. Whenever we want to control destruction of objects of a class, we
make the destructor private.

If a private destructor is declared in the class, then the following restrictions occur:

 it is impossible to create an automatic object of a class (an object of a class that is declared in some
method);
 it is impossible to create a static object;
 it is impossible to create a global class object.

This is due to the fact that such objects can not be destroyed in the future.

Type Conversion in C++

A type cast is basically a conversion from one type to another. There are two types of type conversion:

1. Implicit Type Conversion

Also known as ‘automatic type conversion’.

 Done by the compiler on its own, without any external trigger from the user.
 Generally takes place when in an expression more than one data type is present. In such condition
type conversion (type promotion) takes place to avoid lose of data.
 All the data types of the variables are upgraded to the data type of the variable with largest data
type.
 It is possible for implicit conversions to lose information, signs can be lost (when signed is
implicitly converted to unsigned), and overflow can occur (when long long is implicitly converted
to float).

2. Explicit Type Conversion:

This process is also called type casting and it is user-defined. Here the user can typecast the result to
make it of a particular data type. In C++, it can be done by two ways:
Converting by assignment: This is done by explicitly defining the required type in front of the
expression in parenthesis. This can be also considered as forceful casting.

Syntax:

(type) expression

where type indicates the data type to which the final result is converted.

#include <iostream>

using namespace std;

int main()

double x = 1.2; // Explicit conversion from double to int

int sum = (int)x + 1;

cout << "Sum = " << sum;

Output:

Sum = 2

Conversion using Cast operator: A Cast operator is an unary operator which forces one data type to be
converted into another data type. C++ supports four types of casting:

1. Static Cast

2. Dynamic Cast

3. Const Cast

4. Reinterpret Cast

#include <iostream>

using namespace std;

int main()

float f = 3.5; // using cast operator

int b = static_cast<int>(f);

cout << b;
}

Output:

Type conversion functions

Sometimes it is necessary to convert values from one type to another. Python provides a few simple
functions that will allow us to do that. The functions int , float and str will (attempt to) convert their
arguments into types int, float and str respectively. We call these type conversion functions. The int
function can take a floating point number or a string, and turn it into an int. For floating point numbers, it
discards the decimal portion of the number - a process we call truncation towards zero on the number
line.

Dynamic Memory

The mechanism by which storage/memory/cells can be allocated to variables during the run time is
called Dynamic Memory Allocation. it allocates the memory during the run time which enables us to use
as much storage as we want, without worrying about any wastage. Dynamic memory is something that is
controlled by the program during execution.

You might also like