0% found this document useful (0 votes)
56 views

Explanation: Nested Class Is A Class Defined Inside A Class, That Can Be Used Within The Scope of The Class in

The document provides examples and explanations of different type of casting in C++. It discusses nested class, local class, static cast, const cast, dynamic cast and reinterpret cast. For each type of cast it provides a code example to demonstrate how it works and the output. The examples show how to cast between different data types both implicitly and explicitly using the different cast operators.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Explanation: Nested Class Is A Class Defined Inside A Class, That Can Be Used Within The Scope of The Class in

The document provides examples and explanations of different type of casting in C++. It discusses nested class, local class, static cast, const cast, dynamic cast and reinterpret cast. For each type of cast it provides a code example to demonstrate how it works and the output. The examples show how to cast between different data types both implicitly and explicitly using the different cast operators.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Explanation

Nested class is a class defined inside a class, that can be used within the scope of the class in which it is defined. In C++ nested classes are not given importance because of the strong and flexible usage of inheritance. Its objects are accessed using "Nest::Display". Example:

#include <iostream.h> class Nest { public: class Display { private: int s; public: void sum( int a, int b) { s =a+b; } void show( ) { cout << "\nSum of a and b is:: " << s;} }; }; void main() { Nest::Display x; x.sum(12, 10); x.show(); }
Result:

Sum of a and b is::22


In the above example, the nested class "Display" is given as "public" member of the class "Nest".

Explanation
Local class is a class defined inside a function. Following are some of the rules for using these classes.

Global variables declared above the function can be used with the scope operator "::". Static variables declared inside the function can also be used. Automatic local variables cannot be used. It cannot have static data member objects. Member functions must be defined inside the local classes. Enclosing functions cannot access the private member objects of a local class.

Example:

#include <iostream.h> int y; void g(); int main()

{ g(); return 0; } void g() { class local { public: void put( int n) {::y=n;} int get() {return ::y;} }ab; ab.put(20); cout << "The value assigned to y is::"<< ab.get(); }
Result:

The value assigned to y is::20


In the above example, the local class "local" uses the variable "y" which is declared globally. Inside the function it is used using the "::" operator. The object "ab" is used to set, get the assigned values in the local class.

Explanation "Type Casting" is method using which a variable of one datatype is converted to another datatype, to do it simple the datatype is specified using parenthesis in front of the value. Example:

#include <iostream.h> using namespace std; int main() { short x=40; int y; y = (int)x; cout << "Value of y is:: "<< y <<"\nSize is::"<< sizeof(y); return 0; }

Result: Value of y is:: 40 Size is::4

Explanation Static_Cast operator is used to convert a given expression to the specified type. It is used convert base class pointers to the derived class as well as vice versa. This is type casting in C++.

Example:

#include <iostream.h> using namespace std; int main() { int a = 31; int b = 3; float x = a/b; float y = static_cast<float>(a)/b; cout << "Output without static_cast = " << x << endl; cout << "Output with static_cast = " << y << endl; return 0; }

Result: Output without static_cast = 10 Output with static_cast = 10.3333 In the above type casting example, using the static_cast to an integer as "float" returns a float value.

Explanation

const_cast operator is used to add or remove a "const" modifier from a type. Example:

#include <iostream.h> using namespace std; void example(const float &a) { float &b = const_cast (a); b = 20.0; } int main() { float c = 17.5; cout << "The value before const_cast is:: " << c << endl; example(c); cout << "The value after const_cast is::" << c << endl; return 0; }
Result:

The value before const_cast is:: 17.5 The value after const_cast is:: 20
In the above example the const_cast operator is used in a function to assign a constant value as "20.0". So that whenever the function is called the "20.0" is returned.

Dynamic_Cast Operator is used with pointers and objects to do casting at the runtime unlike other cast operators. In order for this operation, the typeid operator or exceptions to work in C++, RunTime Type Information (RTTI) must be enabled. Example:

#include <iostream.h> using namespace std; struct X { virtual ~X() { }; }; struct Y : X { }; int main() { Y a; X * ptr1 = &a; void * ptr2 = dynamic_cast(ptr1); cout << "Address of ptr2:" << ptr2 << endl; cout << "Address of a:" << &a << endl; }
Result: Address of ptr2: 0012FF88 Address of a: 0012FF88 In the above example the dynamic_cast operator is used on a "ptr1", so that the address of the object "a" is assigned to "ptr2" at the runtime. This operator is a part of Run-Time Type Information (RTTI). Reinterpret_cast operator is used convert a pointer of one type to another type even that of an unrelated class. This is type casting in C++. Example:

#include <iostream.h> int main() { int *ptr = new int(20); cout << "First value = " << *ptr << endl; void *ptr2 = reinterpret_cast(ptr); int *ptr3 = reinterpret_cast(ptr2); cout << "Final reinterpret cast value is::" << *ptr3 << endl; }
Result of Type Casting: First value = 20 Final reinterpret cast value is:: 20 In the above example, the reinterpret cast operator is used convert a integer pointer to a pointer of type "void" then converted into a integer pointer.

You might also like