forked from matkatmusic/PFMCPP_Project4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
905 lines (747 loc) · 26.2 KB
/
Copy pathmain.cpp
File metadata and controls
905 lines (747 loc) · 26.2 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
/*
Project 4: Part 7 / 9
Video: Chapter 5 Part 4
Create a branch named Part7
Do not delete your previous main. you will be adding to it.
Templates and Containers
Build/Run often with this task to make sure you're not breaking the code with each step.
I recommend committing after you get each step working so you can revert to a working version easily if needed.
it's very easy to mess this task up.
0) comment out part6(), both the function and where it is called.
#1) if you're not using std::unique_ptr to own your heap-allocated type as a member variable,
replace your manual memory management techniques with a private std::unique_ptr member variable.
#2) replace your Heap-Allocated Numeric Type-owning classes with a single templated class called 'Numeric'.
replace all instances of your previous classes (IntType, etc) with this templated class.
#3) add a 'using Type = <your class template parameter>;'
treat this type declaration via 'using' as a static member variable
use this Type alias as the argument everywhere you previously used the template argument.
this will make it very easy to change the type if needed.
i.e. if you have 'std::unique_ptr<NumericType> value;' before
you'd replace NumericType in that variable declaration with 'Type'
#4) you'll need to pair it with decltype() to help the compiler figure out the type of the object
your class owns when you make your lambda and free function that takes your unique_ptr.
i.e. like this for determining the template parameter of the unique_ptr function argument to your class's apply() function
std::unique_ptr< decltype( <instance of class> )::Type >
#5) template your free function for the apply() that takes a function pointer so it can be used with your Wrapper class's apply() function
#6) add an explicit template specialization for 'double' of your wrapper class
this template specialization will have one apply() function instead of 2.
#7) this apply() function will be templated, and expect a Callable object, not std::function<>.
the function should allow for chaining.
the callable object should return void, like the function pointer-based apply() function in the primary class template
#8) instantiate your explicit template specialization
#9) call the apply function twice, once with a lambda and once with the free function
the free function is templated, so you might need to call it including the template arguments.
#10) in addition to using the lambda argument to modify the owned object: (your previous task's lambdas did this same thing)
make the lambda use your explicit template instance (maybe via a std::cout),
#11) now that your class is templated, you'll need to adjust your logic in your division function to handle if your input is a zero or not, based on your templated type.
- look up how to use std::is_same<>::value on cppreference to determine the type of your template parameter.
- look up how to use std::numeric_limits<>::epsilon() to determine if you're dividing by a floating point 0
- read about Knuth's algorithm here: https://www.learncpp.com/cpp-tutorial/relational-operators-and-floating-point-comparisons/
- in plain-english, you'll need to implement this logic:
if your class template type is an int
if your parameter's type is also an int
if your parameter's value is 0
don't do the division
else if your parameter's value is less than epsilon
dont do the divison
else if your parameter's value is less than epsilon
warn about doing the division
- to make these checks work during compilation, your if() statements will need to be 'constexpr': if constexpr (expression)
- pay attention to the 2nd line in the plain-english logic.
most people don't get this part of the assignment correct.
12) move part7() to before main()
call part7() in main(), after where you were calling part6()
If you need to view an example, see: https://bitbucket.org/MatkatMusic/pfmcpptasks/src/master/Projects/Project4/Part7Example.cpp
*/
#include <iostream>
void part7()
{
Numeric ft3(3.0f);
Numeric dt3(4.0);
Numeric it3(5);
std::cout << "Calling Numeric<float>::apply() using a lambda (adds 7.0f) and Numeric<float> as return type:" << std::endl;
std::cout << "ft3 before: " << ft3 << std::endl;
{
using Type = #4;
ft3.apply( [](std::unique...){} );
}
std::cout << "ft3 after: " << ft3 << std::endl;
std::cout << "Calling Numeric<float>::apply() twice using a free function (adds 7.0f) and void as return type:" << std::endl;
std::cout << "ft3 before: " << ft3 << std::endl;
ft3.apply(myNumericFreeFunct).apply(myNumericFreeFunct);
std::cout << "ft3 after: " << ft3 << std::endl;
std::cout << "---------------------\n" << std::endl;
std::cout << "Calling Numeric<double>::apply() using a lambda (adds 6.0) and Numeric<double> as return type:" << std::endl;
std::cout << "dt3 before: " << dt3 << std::endl;
{
using Type = #4;
dt3.apply( [](std::unique...){} ); // This calls the templated apply fcn
}
std::cout << "dt3 after: " << dt3 << std::endl;
std::cout << "Calling Numeric<double>::apply() twice using a free function (adds 7.0) and void as return type:" << std::endl;
std::cout << "dt3 before: " << dt3 << std::endl;
dt3.apply(myNumericFreeFunct<double>).apply(myNumericFreeFunct<double>); // This calls the templated apply fcn
std::cout << "dt3 after: " << dt3 << std::endl;
std::cout << "---------------------\n" << std::endl;
std::cout << "Calling Numeric<int>::apply() using a lambda (adds 5) and Numeric<int> as return type:" << std::endl;
std::cout << "it3 before: " << it3 << std::endl;
{
using Type = #4;
it3.apply( [](std::unique...){} );
}
std::cout << "it3 after: " << it3 << std::endl;
std::cout << "Calling Numeric<int>::apply() twice using a free function (adds 7) and void as return type:" << std::endl;
std::cout << "it3 before: " << it3 << std::endl;
it3.apply(myNumericFreeFunct).apply(myNumericFreeFunct);
std::cout << "it3 after: " << it3 << std::endl;
std::cout << "---------------------\n" << std::endl;
}
/*
your program should generate the following output EXACTLY.
This includes the warnings.
The output should have zero warnings.
FloatType add result=4
FloatType subtract result=2
FloatType multiply result=4
FloatType divide result=0.25
DoubleType add result=4
DoubleType subtract result=2
DoubleType multiply result=4
DoubleType divide result=0.8
IntType add result=4
IntType subtract result=2
IntType multiply result=4
IntType divide result=1
Chain calculation = 590
New value of ft = (ft + 3.0f) * 1.5f / 5.0f = 0.975
---------------------
Initial value of dt: 0.8
Initial value of it: 590
Use of function concatenation (mixed type arguments)
New value of dt = (dt * it) / 5.0f + ft = 95.375
---------------------
Intercept division by 0
New value of it = it / 0 = error: integer division by zero is an error and will crash the program!
590
New value of ft = ft / 0 = warning: floating point division by zero!
inf
New value of dt = dt / 0 = warning: floating point division by zero!
inf
---------------------
The result of FloatType^4 divided by IntType is: 26.9136
The result of DoubleType times 3 plus IntType is : 67.3
The result of IntType divided by 3.14 multiplied by DoubleType minus FloatType is: 711
An operation followed by attempts to divide by 0, which are ignored and warns user:
error: integer division by zero is an error and will crash the program!
can't divide integers by zero!
can't divide integers by zero!
505521
FloatType x IntType = 13143546
(IntType + DoubleType + FloatType) x 24 = 315447336
Power tests with FloatType
pow(ft1, floatExp) = 2^2 = 4
pow(ft1, itExp) = 4^2 = 16
pow(ft1, ftExp) = 16^2 = 256
pow(ft1, dtExp) = 256^2 = 65536
---------------------
Power tests with DoubleType
pow(dt1, doubleExp) = 2^2 = 4
pow(dt1, itExp) = 4^2 = 16
pow(dt1, ftExp) = 16^2 = 256
pow(dt1, dtExp) = 256^2 = 65536
---------------------
Power tests with IntType
pow(it1, intExp) = 2^2 = 4
pow(it1, itExp) = 4^2 = 16
pow(it1, ftExp) = 16^2 = 256
pow(it1, dtExp) = 256^2 = 65536
===============================
Point tests with float argument:
Point { x: 3, y: 6 }
Multiplication factor: 6
Point { x: 18, y: 36 }
---------------------
Point tests with FloatType argument:
Point { x: 3, y: 3 }
Multiplication factor: 3
Point { x: 9, y: 9 }
---------------------
Point tests with DoubleType argument:
Point { x: 3, y: 4 }
Multiplication factor: 4
Point { x: 12, y: 16 }
---------------------
Point tests with IntType argument:
Point { x: 3, y: 4 }
Multiplication factor: 5
Point { x: 15, y: 20 }
---------------------
Calling Numeric<float>::apply() using a lambda (adds 7.0f) and Numeric<float> as return type:
ft3 before: 3
ft3 after: 10
Calling Numeric<float>::apply() twice using a free function (adds 7.0f) and void as return type:
ft3 before: 10
ft3 after: 24
---------------------
Calling Numeric<double>::apply() using a lambda (adds 6.0) and Numeric<double> as return type:
dt3 before: 4
dt3 after: 10
Calling Numeric<double>::apply() twice using a free function (adds 7.0) and void as return type:
dt3 before: 10
dt3 after: 24
---------------------
Calling Numeric<int>::apply() using a lambda (adds 5) and Numeric<int> as return type:
it3 before: 5
it3 after: 10
Calling Numeric<int>::apply() twice using a free function (adds 7) and void as return type:
it3 before: 10
it3 after: 24
---------------------
good to go!
Use a service like https://www.diffchecker.com/diff to compare your output.
*/
struct A {};
struct HeapA
{
A *ptrToA;
HeapA() : ptrToA(new A) {}
~HeapA()
{
delete ptrToA;
ptrToA = nullptr;
}
};
#include <iostream>
#include <cmath>
#include <functional>
//need to declare first
struct FloatType;
struct DoubleType;
struct IntType;
struct FloatType
{
explicit FloatType(float floatV) : value(new float(floatV)) {}
~FloatType()
{
delete value;
value = nullptr;
}
FloatType& operator+=(float rhs);
FloatType& operator-=(float rhs);
FloatType& operator*=(float rhs);
FloatType& operator/=(float rhs);
FloatType& pow(float exp);
FloatType& pow(const FloatType& exp);
FloatType& pow(const DoubleType& exp);
FloatType& pow(const IntType& exp);
FloatType& apply(std::function<FloatType&(float&)> func);
FloatType& apply(void(*funcPtr)(float&));
operator float() const { return *value;}
private:
float *value;
FloatType& powInternal(float exp);
};
struct DoubleType
{
explicit DoubleType(double doubleV) : value(new double(doubleV)){}
~DoubleType()
{
delete value;
value = nullptr;
}
DoubleType& operator+=(double rhs);
DoubleType& operator-=(double rhs);
DoubleType& operator*=(double rhs);
DoubleType& operator/=(double rhs);
DoubleType& pow(double exp);
DoubleType& pow(const FloatType&exp);
DoubleType& pow(const DoubleType& exp);
DoubleType& pow(const IntType& exp);
DoubleType& apply(std::function<DoubleType&(double&)> func);
DoubleType& apply(void(*funcPtr)(double&));
operator double() const { return *value;}
private:
double *value;
DoubleType& powInternal(double exp);
};
struct IntType
{
explicit IntType(int intV) : value(new int(intV)) {}
~IntType()
{
delete value;
value = nullptr;
}
IntType& operator+=(int rhs);
IntType& operator-=(int rhs);
IntType& operator*=(int rhs);
IntType& operator/=(int rhs);
IntType& pow(int exp);
IntType& pow(const FloatType& exp);
IntType& pow(const DoubleType& exp);
IntType& pow(const IntType& exp);
IntType& apply(std::function<IntType&(int&)> func);
IntType& apply(void(*funcPtr)(int&));
operator int() const { return *value;}
private:
int *value;
IntType& powInternal(int exp);
};
// Float
FloatType& FloatType::operator+=(float rhs)
{
*value += rhs;
return *this;
}
FloatType& FloatType::operator-=(float rhs)
{
*value -= rhs;
return *this;
}
FloatType& FloatType::operator*=(float rhs)
{
*value *= rhs;
return *this;
}
FloatType& FloatType::operator/=(float rhs)
{
if (rhs == 0.f)
{
std::cout << "warning: floating point division by zero!" << std::endl;
}
*value /= rhs;
return *this;
}
FloatType& FloatType::pow(float exp)
{
return powInternal(exp);
}
FloatType& FloatType::pow(const FloatType &exp)
{
return powInternal(static_cast<float>(exp));
}
FloatType& FloatType::pow(const DoubleType &exp)
{
return powInternal(static_cast<float>(exp));
}
FloatType& FloatType::pow(const IntType &exp)
{
return powInternal(static_cast<float>(exp));
}
FloatType& FloatType::powInternal(float exp)
{
*value = std::pow(*value, exp);
return *this;
}
FloatType& FloatType::apply(std::function<FloatType&(float&)> func)
{
if(func)
{
return func(*value);
}
return *this;
}
FloatType& FloatType::apply(void(*funcPtr)(float&))
{
if(funcPtr)
{
funcPtr(*value);
}
return *this;
}
// Double
DoubleType& DoubleType::operator+=(double rhs)
{
*value += rhs;
return *this;
}
DoubleType& DoubleType::operator-=(double rhs)
{
*value -= rhs;
return *this;
}
DoubleType& DoubleType::operator*=(double rhs)
{
*value *= rhs;
return *this;
}
DoubleType& DoubleType::operator/=(double rhs)
{
if (rhs == 0.0)
{
std::cout << "warning: floating point division by zero!" << std::endl;
}
*value /= rhs;
return *this;
}
DoubleType& DoubleType::pow(double exp)
{
return powInternal(exp);
}
DoubleType& DoubleType::pow(const FloatType& exp)
{
return powInternal(static_cast<double>(exp));
}
DoubleType& DoubleType::pow(const DoubleType& exp)
{
return powInternal(static_cast<double>(exp));
}
DoubleType& DoubleType::pow(const IntType& exp)
{
return powInternal(static_cast<double>(exp));
}
DoubleType& DoubleType::powInternal(double exp)
{
*value = std::pow(*value, exp);
return *this;
}
DoubleType& DoubleType::apply(std::function<DoubleType&(double&)> func)
{
if (func)
{
return func(*value);
}
return *this;
}
DoubleType& DoubleType::apply(void(*funcPtr)(double&))
{
if (funcPtr)
{
funcPtr(*value);
}
return *this;
}
// Int
IntType& IntType::operator+=(int rhs)
{
*value += rhs;
return *this;
}
IntType& IntType::operator-=(int rhs)
{
*value -= rhs;
return *this;
}
IntType& IntType::operator*=(int rhs)
{
*value *= rhs;
return *this;
}
IntType& IntType::operator/=(int rhs)
{
if (rhs != 0)
{
*value /= rhs;
return *this;
}
std::cout << "error: integer division by zero is an error and will crash the program!" << std::endl;
return *this;
}
IntType& IntType::pow(int exp)
{
return powInternal(exp);
}
IntType& IntType::pow(const FloatType& exp)
{
return powInternal(static_cast<int>(exp));
}
IntType& IntType::pow(const DoubleType& exp)
{
return powInternal(static_cast<int>(exp));
}
IntType& IntType::pow(const IntType& exp)
{
return powInternal(static_cast<int>(exp));
}
IntType& IntType::powInternal(int exp)
{
*value = static_cast<int>(std::pow(*value, exp));
return *this;
}
IntType& IntType::apply(std::function<IntType&(int&)> func)
{
if(func)
{
return func(*value);
}
return *this;
}
IntType& IntType::apply(void(*funcPtr)(int&))
{
if(funcPtr)
{
funcPtr(*value);
}
return *this;
}
// Point
struct Point
{
Point(float a, float b);
Point(const FloatType& a, const FloatType& b);
Point(const DoubleType& a, const DoubleType& b);
Point(const IntType& a, const IntType& b);
Point& multiply(float m)
{
x *= m;
y *= m;
return *this;
}
Point& multiply(const FloatType& m);
Point& multiply(const DoubleType& m);
Point& multiply(const IntType& m);
void toString();
private:
float x{0}, y{0};
};
Point::Point(float a, float b) : x(a), y(b){}
Point::Point(const FloatType& a, const FloatType& b) : x(static_cast<float>(a)), y(static_cast<float>(b)){}
Point::Point(const DoubleType& a, const DoubleType& b) : x(static_cast<float>(a)), y(static_cast<float>(b)){}
Point::Point(const IntType& a, const IntType& b) : x(static_cast<float>(a)), y(static_cast<float>(b)){}
Point& Point::multiply(const FloatType& m)
{
return multiply(static_cast<float>(m));
}
Point& Point::multiply(const DoubleType& m)
{
return multiply(static_cast<float>(m));
}
Point& Point::multiply(const IntType& m)
{
return multiply(static_cast<float>(m));
}
void Point::toString()
{
std::cout << "Point { x: " << x << ", y: " << y << " }" << std::endl;
}
//Free Functions
void myFloatFreeFunct(float& ft)
{
ft += 7.0f;
}
void myDoubleFreeFunct(double& dt)
{
dt += 6.0;
}
void myIntFreeFunct(int& it)
{
it += 5;
}
void part3()
{
FloatType ft( 5.5f );
DoubleType dt( 11.1 );
IntType it ( 34 );
DoubleType pi( 3.14 );
ft *= ft;
ft *= ft;
ft /= it;
std::cout << "The result of FloatType^4 divided by IntType is: " << ft << std::endl;
dt *= 3;
dt += it;
std::cout << "The result of DoubleType times 3 plus IntType is : " << dt << std::endl;
it /= static_cast<int>(pi);
it *= static_cast<int>(dt);
it -= static_cast<int>(ft);
std::cout << "The result of IntType divided by 3.14 multiplied by DoubleType minus FloatType is: " << it << std::endl;
std::cout << "An operation followed by attempts to divide by 0, which are ignored and warns user: " << std::endl;
it *= it;
it /= 0;
it /= 0.0f;
it /= 0.0;
std::cout << it << std::endl;
it *= static_cast<int>(ft);
std::cout << "FloatType x IntType = " << it << std::endl;
it += static_cast<int>(dt);
it += static_cast<int>(ft);
it *= 24;
std::cout << "(IntType + DoubleType + FloatType) x 24 = " << it << std::endl;
}
void part4()
{
// ------------------------------------------------------------
// Power tests
// ------------------------------------------------------------
FloatType ft1(2);
DoubleType dt1(2);
IntType it1(2);
float floatExp = 2.0f;
double doubleExp = 2.0;
int intExp = 2;
IntType itExp(2);
FloatType ftExp(2.0f);
DoubleType dtExp(2.0);
// Power tests with FloatType
std::cout << "Power tests with FloatType " << std::endl;
std::cout << "pow(ft1, floatExp) = " << ft1 << "^" << floatExp << " = " << ft1.pow(floatExp) << std::endl;
std::cout << "pow(ft1, itExp) = " << ft1 << "^" << itExp << " = " << ft1.pow(itExp) << std::endl;
std::cout << "pow(ft1, ftExp) = " << ft1 << "^" << ftExp << " = " << ft1.pow(ftExp) << std::endl;
std::cout << "pow(ft1, dtExp) = " << ft1 << "^" << dtExp << " = " << ft1.pow(dtExp) << std::endl;
std::cout << "---------------------\n" << std::endl;
// Power tests with DoubleType
std::cout << "Power tests with DoubleType " << std::endl;
std::cout << "pow(dt1, doubleExp) = " << dt1 << "^" << doubleExp << " = " << dt1.pow(intExp) << std::endl;
std::cout << "pow(dt1, itExp) = " << dt1 << "^" << itExp << " = " << dt1.pow(itExp) << std::endl;
std::cout << "pow(dt1, ftExp) = " << dt1 << "^" << ftExp << " = " << dt1.pow(ftExp) << std::endl;
std::cout << "pow(dt1, dtExp) = " << dt1 << "^" << dtExp << " = " << dt1.pow(dtExp) << std::endl;
std::cout << "---------------------\n" << std::endl;
// Power tests with IntType
std::cout << "Power tests with IntType " << std::endl;
std::cout << "pow(it1, intExp) = " << it1 << "^" << intExp << " = " << it1.pow(intExp) << std::endl;
std::cout << "pow(it1, itExp) = " << it1 << "^" << itExp << " = " << it1.pow(itExp) << std::endl;
std::cout << "pow(it1, ftExp) = " << it1 << "^" << ftExp << " = " << it1.pow(ftExp) << std::endl;
std::cout << "pow(it1, dtExp) = " << it1 << "^" << dtExp << " = " << it1.pow(dtExp) << std::endl;
std::cout << "===============================\n" << std::endl;
// ------------------------------------------------------------
// Point tests
// ------------------------------------------------------------
FloatType ft2(3.0f);
DoubleType dt2(4.0);
IntType it2(5);
float floatMul = 6.0f;
// Point tests with float
std::cout << "Point tests with float argument:" << std::endl;
Point p0(ft2, floatMul);
p0.toString();
std::cout << "Multiplication factor: " << floatMul << std::endl;
p0.multiply(floatMul);
p0.toString();
std::cout << "---------------------\n" << std::endl;
// Point tests with FloatType
std::cout << "Point tests with FloatType argument:" << std::endl;
Point p1(ft2, ft2);
p1.toString();
std::cout << "Multiplication factor: " << ft2 << std::endl;
p1.multiply(ft2);
p1.toString();
std::cout << "---------------------\n" << std::endl;
// Point tests with DoubleType
std::cout << "Point tests with DoubleType argument:" << std::endl;
Point p2(ft2, static_cast<float>(dt2));
p2.toString();
std::cout << "Multiplication factor: " << dt2 << std::endl;
p2.multiply(dt2);
p2.toString();
std::cout << "---------------------\n" << std::endl;
// Point tests with IntType
std::cout << "Point tests with IntType argument:" << std::endl;
Point p3(ft2, static_cast<float>(dt2));
p3.toString();
std::cout << "Multiplication factor: " << it2 << std::endl;
p3.multiply(it2);
p3.toString();
std::cout << "---------------------\n" << std::endl;
}
void part6()
{
FloatType ft3(3.0f);
DoubleType dt3(4.0);
IntType it3(5);
std::cout << "Calling FloatType::apply() using a lambda (adds 7.0f) and FloatType as return type:" << std::endl;
std::cout << "ft3 before: " << ft3 << std::endl;
ft3.apply( [&]( float&)->FloatType&
{
ft3 += 7.0f;
return ft3;
} );
std::cout << "ft3 after: " << ft3 << std::endl;
std::cout << "Calling FloatType::apply() using a free function (adds 7.0f) and void as return type:" << std::endl;
std::cout << "ft3 before: " << ft3 << std::endl;
ft3.apply(myFloatFreeFunct);
std::cout << "ft3 after: " << ft3 << std::endl;
std::cout << "---------------------\n" << std::endl;
std::cout << "Calling DoubleType::apply() using a lambda (adds 6.0) and DoubleType as return type:" << std::endl;
std::cout << "dt3 before: " << dt3 << std::endl;
dt3.apply( [&](double&)->DoubleType&
{
dt3 +=6.0;
return dt3;
} );
std::cout << "dt3 after: " << dt3 << std::endl;
std::cout << "Calling DoubleType::apply() using a free function (adds 6.0) and void as return type:" << std::endl;
std::cout << "dt3 before: " << dt3 << std::endl;
dt3.apply(myDoubleFreeFunct);
std::cout << "dt3 after: " << dt3 << std::endl;
std::cout << "---------------------\n" << std::endl;
std::cout << "Calling IntType::apply() using a lambda (adds 5) and IntType as return type:" << std::endl;
std::cout << "it3 before: " << it3 << std::endl;
it3.apply( [&](int&)->IntType&
{
it3 += 5;
return it3;
} );
std::cout << "it3 after: " << it3 << std::endl;
std::cout << "Calling IntType::apply() using a free function (adds 5) and void as return type:" << std::endl;
std::cout << "it3 before: " << it3 << std::endl;
it3.apply(myIntFreeFunct);
std::cout << "it3 after: " << it3 << std::endl;
std::cout << "---------------------\n" << std::endl;
}
int main()
{
// testing instruction 0
HeapA heapA;
// assign heap primitives
FloatType ft(2.0f);
DoubleType dt(2);
IntType it(2);
ft += 2.0f;
std::cout << "FloatType add result=" << ft << std::endl;
ft -= 2.0f;
std::cout << "FloatType subtract result=" << ft << std::endl;
ft *= 2.0f;
std::cout << "FloatType multiply result=" << ft << std::endl;
ft /= 16.0f;
std::cout << "FloatType divide result=" << ft << std::endl << std::endl;
dt +=2.0;
std::cout << "DoubleType add result=" << dt << std::endl;
dt -= 2.0;
std::cout << "DoubleType subtract result=" << dt << std::endl;
dt *= 2.0;
std::cout << "DoubleType multiply result=" << dt << std::endl;
dt /= static_cast<double>(5.f);
std::cout << "DoubleType divide result=" << dt << std::endl << std::endl;
it += 2;
std::cout << "IntType add result=" << it << std::endl;
it -= 2;
std::cout << "IntType subtract result=" << it << std::endl;
it *= 2;
std::cout << "IntType multiply result=" << it << std::endl;
it /= 3;
std::cout << "IntType divide result=" << it << std::endl << std::endl;
it *= 1000;
it /= 2;
it -= 10;
it +=100;
std::cout << "Chain calculation = " << it << std::endl;
// FloatType object instanciation and method tests
// --------
ft += 3.0f;
ft *= 1.5f;
ft /= 5.0f;
std::cout << "New value of ft = (ft + 3.0f) * 1.5f / 5.0f = " << ft << std::endl;
std::cout << "---------------------\n" << std::endl;
// DoubleType/IntType object instanciation and method tests
// --------
std::cout << "Initial value of dt: " << dt << std::endl;
std::cout << "Initial value of it: " << it << std::endl;
// --------
std::cout << "Use of function concatenation (mixed type arguments) " << std::endl;
dt *= it;
dt /= static_cast<double>(5.0f);
dt += static_cast<double>(ft);
std::cout << "New value of dt = (dt * it) / 5.0f + ft = " << dt << std::endl;
std::cout << "---------------------\n" << std::endl;
// Intercept division by 0
// --------
std::cout << "Intercept division by 0 " << std::endl;
std::cout << "New value of it = it / 0 = " << (it /= 0) << std::endl;
std::cout << "New value of ft = ft / 0 = " << (ft /= 0) << std::endl;
std::cout << "New value of dt = dt / 0 = " << (dt /= 0) << std::endl;
std::cout << "---------------------\n" << std::endl;
part3();
part4();
part6();
std::cout << "good to go!\n";
return 0;
}