Monday 14 April 2014

Write A C++ Program To Implement The Program For Type Conversion And Overloading Assignment Operator


Write A C++ Program To Implement The Program For Type Conversion And Overloading Assignment Operator


Ex: No: (a) OVERLOADING ASSIGNMENT OPERATOR

Aim:To write a c ++ program to implement the overloading assignment operator

Algorithm:

Step 1: Start the program.
Step 2: Create a different classes.
Step 3: Declare the assignment operator.
Step 4: Declare the operator function for various task.
Step 5: Declare the main function and collect the all data and display it.
Step 6: Stop the program.


Program:

#include
#include
using namespace std;
struct A {
A& operator=(const A&) {
cout << "A::operator=(const A&)" << endl;
return *this;
}
A& operator=(A&) {
cout << "A::operator=(A&)" << endl;
return *this;
}
};
class B {
A a;
};
struct C {
C& operator=(C&) {
cout << "C::operator=(C&)" << endl;
return *this;
}
C() { }
};
void main()
{
clrscr();
B x, y;
x = y;
A w, z;
w = z;
C i;
const C j();
// i = j;
getch();
}

Output:
A::operator= (const a&)
A::operator (A&)

Result:

Thus the implementation of c ++ program for overloading assignment operator is executed and the output has been verified.


Ex: No: (b) TYPE CONVERSION

Aim:To write a c ++ program to implement the program for type conversion

Algorithm:

Step 1: Start the program.
Step 2: Declare the main function and get the float value.
Step 3: We can invoke the different type conversion classes.
Step 4: Then display the variables of x1,x2,x3.
Step 5: Stop the program.


Program:

#include
#include
void main()
{
clrscr();
float num=98.76;
int x1=(int)num;
int x2=int(num);
int x3=static_cast(num);
cout<<”x1=”<
cout<<”x2=”<
cout<<”x3=”<
getch();
}

Output:
X1=98
X2=98
X3=98

Result:

Thus the implementation of c ++ program for type conversion is executed and the output has been verified.

0 comments:

Post a Comment