Académique Documents
Professionnel Documents
Culture Documents
The type conversions are automatic only when the data types involved are built-in types. int m; float x = 3.14159; m = x; // convert x to integer before its value is assigned to m.
class type.
s2 = name2;
First converts name2 from char* type to string type and then assigns the string type value to the object s2.
First converts name1 from char* type to string type and then assigns the string type value to the object s1.
double sum = 0;
for (int i=0; i < size ; i++) sum = sum + v[i] * v[i];
The casting operator function should satisfy the following conditions: It must be a class member. It must not specify a return type. It must not have any arguments. vector : : operator double( ) { double sum = 0; for (int i=0; i < size ; i++) sum = sum + v[i] * v[i]; return sqrt (sum); }
conversion function. Choosing of constructor or the conversion function depends upon where we want the type-conversion function to be located in the source class or in the destination class.
The argument belongs to the source class and is passed to the destination class for conversion. The conversion constructor destination class. be placed in the
#include<iostream.h> class invent2; class invent1 { int code; int items; float price; public: invent1(int a,int b,float c) { code=a; items=b; price=c; }
Void putdata() { cout<<code:<<code; cout<<itmes:<<items; cout<<price:<<price; } Int getcode() { return code; Int getitems() { return items; Float getprice() { return price; Operator float() { return (items*price); } } } }
operator invent2()
{ invent2 temp;
temp.code=code;
temp.value=price*items; Return temp; } };
class invent2 { int code; float value; public: invent 2() { code=0; value=0; } invent1(int x,float y) { code=x; value=y; }
{
code=p.getcode(); value=p.getitems()*p.getprice(); } };
void main() { invent1 s1(100,5,140.0); invent2 d1; float total_value; total_value=s1; d1=s1; cout<<product details invent1 <<\n; s1.putdata(); cout<<stock value<<\n; cout<<value<<total_value<<\n; cout<<product details invent2 <<\n; d1.putdata(); }