-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntax.h
89 lines (83 loc) · 1.6 KB
/
syntax.h
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
// this declares a class
class aClass {
private: // private members are only accessible by other members of this class
int x;
double z;
public: // public members are globally accessible!
void zEqualsProduct(double c) {
z= c*x;
}
double getZ() {
return z;
}
void setX(int xIn) {
x= xIn;
}
};
// this declares a base class
class operation {
private: // private members can't be accessed by derived classes
void print() {
printf("this does an operation f(x, y), where f and x are doubles.\n\n");
}
protected: // protected members, are only accessible from other members of this
// class OR by members of derived classes!
double x;
double y;
public:
operation() {
x= 0;
y= 0;
}
operation(double xi, double yi) {
x= xi;
y= yi;
}
};
// this declares a derived class
class summer : public operation {
public:
// while we are here, we'll also go ahead and do a default constructor here.
summer() {
x= 1;
y= 2;
}
double getSum() {
return x*y;
}
};
// we can derive a multiplication class from operation, like addition, such that
class multiplier : public operation {
public:
multiplier() {
x= 3;
y= 4;
}
double getProd() {
return x*y;
}
};
// let's make a function that converts doubles to ints
int function(double x) {
return (int) x;
};
// let's make a function that computes y= m*x + b where each of x, y, m, and b
// aren't of a defined type, but of a type to be later determined...
template <typename T>
class line {
private:
T m;
T b;
public:
line() {
m= 1;
b= 1;
}
line(T mIn, T bIn) {
m= mIn;
b= bIn;
}
T compute(T x) {
return m*x + b;
}
};