Unit1 Type Casting This Pointer-Part-2
Unit1 Type Casting This Pointer-Part-2
#include <iostream>
using namespace std;
int main()
{
int x = 10; // integer x
char y = 'a'; // character c
return 0;
}
Output:
x = 107
y=a
z = 108
P a g e 1 | 10
2
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.
Example:
int main()
{
double x = 1.2;
// Explicit conversion from double to int
int sum = (int)x + 1;
cout << "Sum = " << sum;
return 0;
}
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
Example: 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:
3
Advantages of Type Conversion:
• This is done to take advantage of certain features of type hierarchies or type
representations.
• It helps to compute expressions containing variables of different data types.
P a g e 2 | 10
3
“this” Pointer
Every object in C++ has access to its own address through an important pointer
called this pointer. The this pointer is an implicit parameter to all member functions.
Therefore, inside a member function, this may be used to refer to the invoking object.
The this pointer holds the address of current object, in simple words you can say that
this pointer points to the current object of the class.
Friend functions do not have a this pointer, because friends are not members of a class. Only
member functions have a this pointer.
Let us try the following example to understand the concept of this pointer −
Example-1
#include <iostream>
using namespace std;
class Demo {
private:
int num;
char ch;
public:
void setMyValues(int num, char ch){
this->num =num;
this->ch=ch;
}
void displayMyValues(){
cout<<num<<endl;
cout<<ch;
}
};
int main(){
Demo obj;
obj.setMyValues(100, 'A');
obj.displayMyValues();
return 0;
}
Output
100
A
#include<iostream.h>
#include<conio.h>
class SYIT // class definition
{
private:
int Roll;
P a g e 3 | 10
char name[20];
public:
void getdata(); // function declaration
void putdata(); // function declaration
}s1; // end of class
void SYIT :: getdata ( ) //function definition outside class
{
cout << “enter your Roll no and name : ”;
cin >> Roll>>name;
}
void SYIT :: putdata ( ) //function definition outside class
{
this →getdata(); // calling function with “this”
cout<< “ your roll no = “ <<Roll;
cout<< “ your Name = “ <<name;
}
void main ( )
{
clrscr(0;
s1.putdata(); // function call
getch();
}
P a g e 2 | 10
4
Array of object
An object of class represents a single record in memory, if we want more than one record of
class type, we have to create an array of class or object. As we know, an array is a collection
of similar type, therefore an array can be a collection of class type.
Syntax for Array of object
class class-name
{
datatype var1;
datatype var2;
----------
datatype varN;
method1();
method2();
----------
methodN();
};
In the following example, we shall declare an array of type Student, with size of five. Student
is a class that we defined in the program. Then we shall assign objects using index of array.
#include <iostream>
using namespace std;
class Student {
public:
string name;
int rollno;
Student() {}
Student(string x, int y) {
name = x;
rollno = y;
}
void printDetails() {
cout << rollno << " - " << name << endl;
}
};
int main() {
//declare array with specific size
Student students[5];
//assign objects
students[0] = Student("Ram", 5);
students[1] = Student("Alex", 1);
P a g e 5 | 10
students[2] = Student("Lesha", 4);
5
students[3]
students[4] = Student("Emily", 3);
= Student("Anita", 2);
class Student {
public:
string name;
int rollno;
Student() {}
Student(string x, int y) {
name = x;
rollno = y;
}
void printDetails() {
cout << rollno << " - " << name << endl;
}
};
int main() {
//declare array with specific size
Student students[] = {Student("Ram", 5), Student("Lesha", 4), Student("Anita", 2)};
class Employee
{
int Id;
char Name[25];
int Age;
long Salary;
public:
void GetData() //Statement 1 : Defining GetData()
{
cout<<"\n\tEnter Employee Id : ";
cin>>Id;
for(i=0;i<3;i++)
{
cout<<"\nEnter details of "<<i+1<<" Employee";
E[i].GetData();
}
cout<<"\nDetails of Employees";
for(i=0;i<3;i++)
E[i].PutData();
}
Output :
P a g e 8 | 10
7
Details of Employees
101 Suresh 29 45000
102 Mukesh 31 51000
103 Ramesh 28 47000
In the above example, we are getting and displaying the data of 3 employee using array of
object. Statement 1 is creating an array of Employee Emp to store the records of 3 employees.
Namespace
Defining a Namespace
A namespace definition begins with the keyword namespace followed by the namespace
name as follows −
namespace namespace_name {
// code declarations
}
To call the namespace-enabled version of either function or variable, prepend (::) thenamespace
name as follows −
name::code; // code could be variable or function.
Let us see how namespace scope the entities including variable and functions −
P a g e 9 | 10
#include <iostream>
using namespace std;
P a g e 10 | 10
8
namespace first_space {
void func() {
cout << "Inside first_space" << endl;
}
}
int main () {
// Calls function from first name space.
first_space::func();
return 0;
}
If we compile and run above code, this would produce the following result −
Inside first_space Inside
second_space
You can also avoid prepending of namespaces with the using namespace directive. This
directive tells the compiler that the subsequent code is making use of names in the specified
namespace. The namespace is thus implied for the following code −
#include <iostream>
using namespace std;
P a g e 11 | 10
9
return 0;
}
If we compile and run above code, this would produce the following result −
Inside first_space
The ‘using’ directive can also be used to refer to a particular item within a namespace. For
example, if the only part of the std namespace that you intend to use is cout, you can refer to it
as follows −
using std::cout;
Subsequent code can refer to cout without prepending the namespace, but other items in the
std namespace will still need to be explicit as follows −
#include <iostream>
using std::cout;
int main () {
cout << "std::endl is used with std!" << std::endl;
return 0;
}
If we compile and run above code, this would produce the following result −
std::endl is used with std!
Names introduced in a using directive obey normal scope rules. The name is visible from the
point of the using directive to the end of the scope in which the directive is found. Entities
with the same name defined in an outer scope are hidden.
Nested Classes in C++
• Difficulty Level : Easy
• Last Updated : 04 Jan, 2019
A nested class is a class which is declared in another enclosing class. A nested class is a member
and as such has the same access rights as any other member. The members of an enclosing
class have no special access to members of a nested class; the usual access rules shall be
obeyed.
For example, program 1 compiles without any error and program 2 fails in compilation.
Program 1
#include<iostream>
P a g e 12 | 10
10
int main ()
{
}
Program 2
#include<iostream>
using namespace std;
int x;
int main ()
{
}
P a g e 13 | 10