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
|
// testingcode.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include <iostream>
#include <istream>
#include <Windows.h>
#include <new>
using namespace std;
struct v3 {
float X;
float Y;
float Z;
}Vector3;
class test {
private:
int x;
int y;
int z;
char *poot;
public:
static int myglobal;
void SetX(int xx) { x = xx; }
void SetY(int yy) { y = yy; }
void SetZ(int zz) { z = zz; }
void SetCHAR(char *p) { poot = p; }
int GetX() { return x; }
int GetY() { return y; }
int GetZ() { return z; }
char *GetCHAR() { return poot; }
test() { x=0;y=0;z=0; }
test(int xx, int yy, int zz) { x=xx;y=yy;z=zz;}
operator int();
operator char*();
operator v3();
test operator+ (test);
test operator- (test);
}icles;
test test::operator+ (test b) {
test temp;
temp.x = x + b.x;
temp.y = y + b.y;
temp.z = z + b.z;
return (temp);
}
test test::operator- (test b) {
// a little joke :)
test temp;
temp.x = 999;
temp.y = 888;
temp.z = 777;
return (temp);
}
test::operator int() {
return x;
}
test::operator v3() {
v3 poot;
poot.X = x;
poot.Y = y;
poot.Z = z;
return poot;
// I know im assigning int to a float
// which would cause trunication if this was reversed
// i.e. assigning a floating point to an integer variable
// although compiler is still throwing possible loss of data warnings
//Question that has arisen:
// If a signed int is approx 2million negative through 2million positive
// and unsigned is approx 0 to 4million positive
// what if you need negative 4 million to 0?
//Also, why is there a 'long' type of variable
// when it's identical to the use of int?
// they're both 4 bytes, and either signed or unsigned, have the same reach
}
test::operator char*() {
return poot;
}
//********** PROTOTYPE FUNCTIONS ***********
int main();
void dynamic_memory_example();
int multiple(int x, int y);
float multiple(float x, float y);
double multiple(double x, double y);
int divide(int x, int y);
float divide(float x, float y);
double divide(double x, double y);
int subtract(int x, int y);
float subtract(float x, float y);
double subtract(double x, double y);
int addition(int x, int y);
float addition(float x, float y);
double addition(double x, double y);
void outresult(char * message, int (*multiple)(int,int));
void SetV(float xx, float yy, float zz);
void GetV(float *xx, float *yy, float *zz);
//********* END PROTOTYPE FUNCTIONS ***********
//********* DEFINED FUNCTIONS *************
int multiple(int x, int y) { return x*y; }
float multiple(float x, float y) { return x*y; }
double multiple(double x, double y) { return x*y; }
int divide(int x, int y) { return x/y; }
float divide(float x, float y) { return x/y; }
double divide(double x, double y) { return x/y; }
int subtract(int x, int y) { return x-y; }
float subtract(float x, float y) { return x-y; }
double subtract(double x, double y) { return x-y; }
int addition(int x, int y) { return x+y; }
float addition(float x, float y) { return x+y; }
double addition(double x, double y) { return x+y; }
void doublethenumber(int *num2double);
void doublethenumber(int *num2double) { //argument takes address of an already defined integer
*num2double *= 2; //dereferences the address received and multiples by 2
// function doesn't need to return
// the value of the variable in which we received the address of has changed
// playing with pointer/reference/dereference operators
}
void outresult(char * message, int x, int y, int (*functocall)(int,int)) {
int b;
b = (*functocall)(x,y);
cout << message << " " << b;
}
void SetV(float xx, float yy, float zz) {
Vector3.X = xx; Vector3.Y = yy; Vector3.Z = zz;
}
void GetV(float *xx, float *yy, float *zz) {
*xx = Vector3.X;
*yy = Vector3.Y;
*zz = Vector3.Z;
}
void dynamic_memory_example() {
int i, n;
int * p;
cout << "How many numbers would you like to type?";
cin >> i;
p = new (nothrow) int[i];
if(p == 0) {
cout << "Error: Memory could not be allocated." << endl;
}
else
{
for ( n=0; n<i; n++ ) {
cout << "Enter a number: ";
cin >> p[n];
}
cout << "You have entered: ";
for ( n=0; n<i;n++ ) {
if(n != (i - 1)) {
cout << p[n] << ", ";
}else{
cout << p[n] << endl;
}
}
delete[] p;
}
}
//****************** END OF DEFINED FUNCTIONS *************************
int main() {
int br = 10;
doublethenumber(&br);
cout << br;
return 0;
}
|