Съществуват два основни метода за преобразуване на типа:Метод на отрязването (кастинг метод) – с помощта на cast оператор и метод, базиран на конструктор.
Преобразуване с метода на отрязването - с помощта на cast оператор
Преобразува се тип клас в базов тип или в друг тип клас.
Дефинира се винаги чрез операторна член-функция, в която не се задава тип на резултата.
Тази член-функция се извиква за обект, указан от this и няма явни параметри.
Извикването на тази функция може да бъде:
1) явно (при операция присвояване, при изчисление на израз);
2) неявно (при операция присвояване, при извикване на функция, при изчисление на израз)
#include <iostream.h> class example { int x; float y; public: example(int a=1,float b=2.3)
{ x=a; y=b; } operator int (){return x;}[color=blue]//операторна ф-ия за клас->int[/color] operator float(){return y;}[color=blue]//операторна ф-ия за клас->float };[/color] void f(int n) [color=blue]//дефиниция на функция[/color]
{ cout<<”параметър n=”<<n<<”n”; } void main()
{ example p,q; [color=blue]//създаване на два обекта – p,q[/color]
int i,j; float r; i=(int)p; [color=blue]//явно при присвояване[/color] r=(float)q; [color=blue]//явно при присвояване[/color]
cout<<”i=”<<i<<”,r=”<<r<<”n”;
//i=p+3; [color=blue]//нееднозначност - грешка[/color]
//i=(int)p+3; i=q; [color=blue]//неявно при присвояване[/color]
cout<<”i=”<<i<<”n”; f(p); [color=blue]//неявно при извикване на функция[/color] r=(float)p+(int)q; [color=blue]//явно при изчисление на израз[/color]
cout<<”r=”<<r<<”n”; }
1 посетител чете този урок (0 потребители и 1 гост)
Активни потребители:
---
Tired of defining five times the same function? One definition for int type parameters, one definition for double type parameters, one definition for float type parameters... Didn't you forget one type? What if a new data type is used? No problem: the C++ compiler is capable of generating automatically every version of the function that is necessary! Just tell him how the function looks like by declaring a template function:
using namespace std;#include <iostream>template <class ttype>ttype min (ttype a, ttype b){ ttype r; r = a; if (b < a) r = b; return r;}int main (){ int i1, i2, i3; i1 = 34; i2 = 6; i3 = min (i1, i2); cout << "Most little: " << i3 << endl; double d1, d2, d3; d1 = 7.9; d2 = 32.1; d3 = min (d1, d2); cout << "Most little: " << d3 << endl; cout << "Most little: " << min (d3, 3.5) << endl; return 0;}
The function min is used three times in above program yet the C++ compiler generates only two versions of it: int min (int a, int b) and double min (double a, double b). That does the job for the whole program.
Would you have tried something like calculating min (i1, d1) the compiler would have reported that as an error. Indeed the template tells both parameters are of the same type.
You can use a random number of different template data types in a template definition. And not all parameter types must be templates, some of them can be of standard types or user defined (char, int, double...). Here is an example where the min function takes parameters of any, possibly different, types and outputs a value that has the type of the first parameter:
using namespace std;#include <iostream>template <class type1, class type2>type1 min (type1 a, type2 b){ type1 r, b_converted; r = a; b_converted = (type1) b; if (b_converted < a) r = b_converted; return r;}int main (){ int i; double d; i = 45; d = 7.41; cout << "Most little: " << min (i, d) << endl; cout << "Most little: " << min (d, i) << endl; cout << "Most little: " << min ('A', i) << endl; return 0;}