-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUD TO UD.cpp
69 lines (63 loc) · 1.58 KB
/
UD TO UD.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*Problem3: Define a class Item1 with data members code, qty and price and class Item2 with data members code and value.
(Hint: code = code, value = qty*price). Use the constructor in Item2 class to change the Item1 type data to Item2 type.
Your code should be able to execute the statement I2=I1 (where I1 and DI2 are objects of respective item class).*/
#include <iostream>
using namespace std;
class Item1
{
float code, qty, price;
public:
Item1()
{
code = 0;
qty = 0;
price = 0;
}
void input()
{
cout << "Enter the code" << endl;
cin >> code;
cout << "Enter the qty" << endl;
cin >> qty;
cout << "Enter the price" << endl;
cin >> price;
}
void display()
{
cout << "code is" << code << "\n";
cout << "qty is" << qty << "\n";
cout << "price is" << price << "\n";
}
operator float() { return qty * price; }
float getcode() { return code; }
float getqty() { return qty; }
float getprice() { return price; }
};
class Item2
{
float code, value;
public:
Item2()
{
code = 0;
value = 0;
}
Item2(Item1 a)
{
code = a.getcode();
value = a.getqty() * a.getprice();
}
void display()
{
cout << "Code is : " << code << "\n";
cout << "Value is : " << value << "\n";
}
};
int main()
{
Item1 I1;
Item2 I2;
I1.input();
I2=I1; //data type conversion from user-deined to user-defined
I2.display();
}