- // This is the main project file for VC++ application project
- // generated using an Application Wizard.
- #include "stdafx.h"
- #using <mscorlib.dll>
- #include <tchar.h>
- using namespace System;
- __value struct Dbl
- {
- public:
- double val;
- Dbl(double v) { val = v; }
- double getVal() { return val; }
- // Arithmetic operators
- static Dbl op_Addition(Dbl lhs, Dbl rhs)
- {
- Console::WriteLine("op_Addition(Dbl,Dbl)");
- Dbl result(lhs.val + rhs.val);
- return result;
- }
- static Dbl op_Subtraction(Dbl lhs, Dbl rhs)
- {
- Dbl result(lhs.val - rhs.val);
- return result;
- }
- static Dbl op_Multiply(Dbl lhs, Dbl rhs)
- {
- Dbl result(lhs.val * rhs.val);
- return result;
- }
- static Dbl op_Division(Dbl lhs, Dbl rhs)
- {
- Dbl result(lhs.val * rhs.val);
- return result;
- }
- // Logical and equality
- static bool op_Equality(Dbl lhs, Dbl rhs)
- {
- Console::WriteLine("op_Equality");
- return lhs.val == rhs.val;
- }
- static bool op_Inequality(Dbl lhs, Dbl rhs)
- {
- Console::WriteLine("op_Inequality");
- return !(lhs == rhs);
- }
- // Increment and decrement
- static Dbl op_Increment(Dbl d)
- {
- Console::WriteLine("op_Increment");
- d.val += 1;
- return d;
- }
- bool Equals(Object* pOther)
- {
- Dbl* s = dynamic_cast<Dbl*>(pOther);
- if (s == 0) return false;
- return s->val == val;
- }
- };
- Dbl Incr(Dbl d) {
- d.val += 1;
- return d;
- }
- // This is the entry point for this application
- int _tmain(void)
- {
- // TODO: Please replace the sample code below with your own.
- Console::WriteLine(S"Dbl Example");
- Dbl d1(10.0);
- Dbl d2(20.0);
- Dbl d3(0.0);
- // Arithmetic operators
- d3 = d1 + d2;
- // Equivalent direct call to operator function
- d3 = Dbl::op_Addition(d1, d2);
- Console::Write("Value of d3 is ");
- Console::WriteLine(d3.getVal());
- // Addition using integers
- d3 = d1 + 5;
- Console::Write("Value of d3 is now ");
- Console::WriteLine(d3.getVal());
- d3 = 5 + d1;
- Console::Write("Value of d3 is now ");
- Console::WriteLine(d3.getVal());
- // This does integer addition and then casts to a Dbl
- d3 = 5 + 5;
- // Logical operators
- if (d1 == d2)
- Console::WriteLine("Equal");
- else
- Console::WriteLine("Not equal");
- if (d1 != d2)
- Console::WriteLine("Not Equal");
- else
- Console::WriteLine("Equal");
- __box Dbl* pd2 = __box(d2);
- if (d1.Equals(pd2))
- Console::WriteLine("Equals: Equal");
- else
- Console::WriteLine("Equals: Not Equal");
- Dbl d4(10.0);
- if (d1.Equals(__box(d4)))
- Console::WriteLine("Equals: Equal");
- else
- Console::WriteLine("Equals: Not Equal");
- // Increment and decrement
- Dbl d5(1.0);
- Console::Write("Inc: initial d5=");
- Console::WriteLine(d5.getVal());
- d5++;
- Console::Write("Inc: d5++ =");
- Console::WriteLine(d5.getVal());
- Incr(d5);
- Console::Write("Inc: after Incr, d5 =");
- Console::WriteLine(d5.getVal());
- return 0;
- }