Skip to content

Commit 82434a8

Browse files
author
Cory Frontin
committed
Some. Got the basics for C++. More later...
1 parent fb1ce60 commit 82434a8

3 files changed

Lines changed: 263 additions & 0 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
syntax
3+

syntax.cpp

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
2+
// this is a .cpp file, which is just a file for C++ source code
3+
4+
// header: functions, classes, and objects included from either from connected
5+
// programs, or declared in a separate file for convenience
6+
#include <stdio.h> // this is needed for output to terminal ("printf")
7+
#include "syntax.h"
8+
9+
// this is a syntactical program for C++, which should stand as a standalone
10+
// cheatsheet for programming in C++ for experienced programmers
11+
int main() {
12+
13+
// prints output to screen
14+
printf("Hello, world!\n\n");
15+
16+
// FUNDAMENTAL VARIABLE TYPES
17+
18+
bool boolean= false; // this type will store true or false values
19+
20+
char character_letter= 'a'; // this type will store either a "character"
21+
char character_number= 48; // or an integer of most efficient size
22+
unsigned char character_unsigned= 21; // it can be either signed
23+
signed char character_signed= -21; // or unsigned
24+
25+
int integer= 2; // this type will store a 2+ byte integer
26+
// this can also have both signed and unsigned variants
27+
short int integer_short= 1000; // short is guaranteed to have 2 bytes
28+
long int integer_long= 100000; // long is guaranteed to have 4 bytes
29+
// long long also possible for 64 bit integers!
30+
31+
float floatingpoint_single= 0.5; // this type is a single-precision "decimal"
32+
double floatingpoint_double= 0.25;// this type is a double-precision "decimal"
33+
// long double is also possible!
34+
35+
// there is also a "void type", which is a type with an empty set of values.
36+
// it is an "incomplete type" and cannot be completed- so void objects are not
37+
// allowed. no void arrays, no void references. pointers to void and functions
38+
// that return void are allowed, and quite useful!
39+
40+
// ARRAYS
41+
42+
// an array is an object that consists of N contiguously allocated objects of
43+
// a given type T. below, we take T to be double, but double could easily be
44+
// replaced below with any of the fundamental variables types (excl. void),
45+
// pointers, pointers to memebers, classes, enumerations, etc.
46+
int N= 5;
47+
48+
double array[N];
49+
50+
double array_onconstruction[N]= {1.0, 0.5, 0.25, 0.125, 0.0625};
51+
52+
// CONTROL STATEMENTS
53+
54+
// if statement
55+
bool should_do= true;
56+
bool otherwise_todo= true;
57+
if (!should_do) {
58+
printf("I should not have done this.\n\n");
59+
} else if(otherwise_todo) {
60+
printf("If I should do this thing only if I didn't do the "
61+
"first and you also told me to do this one as well.\n\n");
62+
}
63+
else {
64+
printf("I only do this if I didn't do any of the other things.\n\n");
65+
}
66+
67+
// for statement does something:
68+
// for(some variable, initialized; while some condition holds;
69+
// changing some variable at the end of each iteration)
70+
for (int k= 0; k < N; k++) {
71+
printf("%f\t", array_onconstruction[k]);
72+
}
73+
printf("\n\n");
74+
75+
// while statement does something while a statement is true
76+
int counter= 2;
77+
while (counter < 1000) {
78+
counter= counter*counter;
79+
printf("%d, ", counter);
80+
}
81+
printf("\n\n");
82+
83+
// switch/case statements take some condition
84+
int switch_variable= 2;
85+
switch (switch_variable) {
86+
case 1: // jumps here if switch_variable == 1 and continues til break
87+
printf("ah yes. ");
88+
break; // leaves switch/case statment
89+
case 2: // jumps here if switch_variable == 2 and continues
90+
printf("oh, no! ");
91+
case 3: // jumps here if switch_variable == 3 and continues
92+
printf("nope. nope. nope. ");
93+
default: // runs if it gets here
94+
printf("this is very bad indeed.\n\n");
95+
}
96+
// note that, in the absence of break statements, switch/case will continue to
97+
// run through all of the options
98+
99+
// CLASSES
100+
101+
// we can declare a class here, which is defined in the header file
102+
aClass cls= aClass();
103+
// you can interact with the public members of a class, but not private ones
104+
int x_cls= 4;
105+
double c_cls= 5.2;
106+
107+
cls.setX(x_cls);
108+
cls.zEqualsProduct(c_cls);
109+
printf("z= %f.\n", cls.getZ());
110+
111+
// each instance of a class has a separate set of variables so we can
112+
// instantiate a second and see a completely secondary result!
113+
aClass cls2= aClass();
114+
int x_cls2= 8;
115+
double c_cls2= 1.703333;
116+
cls2.setX(x_cls2);
117+
cls2.zEqualsProduct(c_cls2);
118+
printf("z= %f.\tz2= %f!\n\n", cls.getZ(), cls2.getZ());
119+
120+
// the next thing you can do is derive classes. we have in the header file a
121+
// sample class that holds two values and can perform an operation on them.
122+
// class derivation allows us to use the same "base class"
123+
124+
operation op= operation();
125+
summer smr= summer();
126+
multiplier mlt= multiplier();
127+
128+
printf("summer gives: %f\nmultiplier gives: %f\n\n",
129+
smr.getSum(), mlt.getProd());
130+
131+
// typedefs are aliases that can be used anywhere in place of type names,
132+
// which can frequently get annoyingly complex; they're also useful when the
133+
// type of something is passed as an input enum, for example. we create a
134+
// typedef for an unsigned int here:
135+
typedef unsigned int unsgn;
136+
137+
// now we can create an unsigned int this way:
138+
unsgn xTD= 5;
139+
140+
printf("%d\n\n", xTD);
141+
142+
// function pointers are particularly useful too.
143+
// let's make a function in the header file
144+
145+
// we create a typedef for integer-returning function pointers with
146+
// one parameter of type character with:
147+
typedef int (* funcptr)(double);
148+
149+
// now we create a function pointer!!!
150+
funcptr fpt= function; // note that CPP compilers will implicitly convert
151+
// function names to function pointers!
152+
153+
// now call function via the pointer!
154+
double pi= 3.1415926535;
155+
printf("%f is approximated by %d.\n\n", pi, (*fpt)(pi));
156+
157+
// last but not least, we can use class templates to build generic classes. in
158+
// the header file, we make a template to find y= m*x + b, regardless of the
159+
// type that is used for x, y, m, and b.
160+
line<double> pointslopedouble= line<double>(2*pi, 1.0);
161+
line<int> pointslopeinteger= line<int>(2*pi, 1.0);
162+
163+
printf("(x, y)= (%f, %f)\n", 1.0, pointslopedouble.compute(1.0));
164+
printf("(x, y)= (%d, %d)\n", 1, pointslopeinteger.compute(1.0));
165+
166+
// every function has to return something unless it is a void function
167+
// this function returns an integer and standard practice for a main is to
168+
// return zero if everything went to plan
169+
return 0;
170+
171+
}

syntax.h

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
2+
// this declares a class
3+
class aClass {
4+
private: // private members are only accessible by other members of this class
5+
int x;
6+
double z;
7+
public: // public members are globally accessible!
8+
void zEqualsProduct(double c) {
9+
z= c*x;
10+
}
11+
double getZ() {
12+
return z;
13+
}
14+
void setX(int xIn) {
15+
x= xIn;
16+
}
17+
};
18+
19+
// this declares a base class
20+
class operation {
21+
private: // private members can't be accessed by derived classes
22+
void print() {
23+
printf("this does an operation f(x, y), where f and x are doubles.\n\n");
24+
}
25+
protected: // protected members, are only accessible from other members of this
26+
// class OR by members of derived classes!
27+
double x;
28+
double y;
29+
public:
30+
operation() {
31+
x= 0;
32+
y= 0;
33+
}
34+
operation(double xi, double yi) {
35+
x= xi;
36+
y= yi;
37+
}
38+
};
39+
40+
// this declares a derived class
41+
class summer : public operation {
42+
public:
43+
// while we are here, we'll also go ahead and do a default constructor here.
44+
summer() {
45+
x= 1;
46+
y= 2;
47+
}
48+
double getSum() {
49+
return x*y;
50+
}
51+
};
52+
53+
// we can derive a multiplication class from operation, like addition, such that
54+
class multiplier : public operation {
55+
public:
56+
multiplier() {
57+
x= 3;
58+
y= 4;
59+
}
60+
double getProd() {
61+
return x*y;
62+
}
63+
};
64+
65+
// let's make a function that converts doubles to ints
66+
int function(double x) {
67+
return (int) x;
68+
};
69+
70+
// let's make a function that computes y= m*x + b where each of x, y, m, and b
71+
// aren't of a defined type, but of a type to be later determined...
72+
template <typename T>
73+
class line {
74+
private:
75+
T m;
76+
T b;
77+
public:
78+
line() {
79+
m= 1;
80+
b= 1;
81+
}
82+
line(T mIn, T bIn) {
83+
m= mIn;
84+
b= bIn;
85+
}
86+
T compute(T x) {
87+
return m*x + b;
88+
}
89+
};

0 commit comments

Comments
 (0)