PRACTICAL – 11.1
Write a program to convert an object of one class to another class..
#include <iostream>using namespace std;class Oneclass{int a;public:int transfer_data(){return a;}void getdata(){cout << "age is " << a << endl;}Oneclass(int add){a = add;}};class Secondclass{int a;public:void operator=(Oneclass &obj1){a = obj1.transfer_data();}void getdata(){cout << "age is " << a << endl;}};int main(){Oneclass r(10);Secondclass j;j = r;j.getdata();r.getdata();return 0;}
PRACTICAL – 11.2
Design a class Polar which describes a point in the plane using polar coordinates radius and angle. Use overloaded + operator to add two polar objects.
#include <iostream>using namespace std;class Point{int radius, angle;public:Point operator+(Point &one){Point temp;temp.radius = radius + one.radius;temp.angle = angle + one.angle;return temp;}void getdata(){cout << "Point is " << radius << " " << angle << "deg." << endl;}void setdata(){cin >> radius;cin >> angle;}};int main(){Point p1, p2, p3;p1.setdata();p2.setdata();p3 = p1 + p2;p1.getdata();p2.getdata();p3.getdata();return 0;}
PRACTICAL – 11.3
Define two classes Polar and Rectangle to represent points in the polar and rectangular systems. Use conversion routines to convert from one system to the other.
#include <iostream>#include <math.h>using namespace std;class Regular;class Polar;class Regular{public:int r, theta;Regular(int num1, int num2){r = num1;theta = num2;}Regular() {}void getdata(){cout << "Regular Form is " << r << " < " << theta << endl;}};class Polar{public:int x, y;Polar(int num1, int num2){x = num1;y = num2;}Polar() {}void getdata(){cout << "Ploar Form is (" << x << "," << y << ")" << endl;}void operator=(Regular &obj1){x = obj1.r * cos(obj1.theta);y = obj1.r * sin(obj1.theta);}};int main(){Regular r(2, 30);Polar j;j = r;j.getdata();r.getdata();return 0;}
0 Comments