C++ programming language
Learn from examples
1. Program to output a string, integer, and floating point
excp1.cpp source code
// excp1.cpp example print string, int, float, double
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) // standard main program definition
{
string mystr("my string");
int i = 31;
float x = 34.56;
double y = 1.23e+100;
cout << "C++ excp1.cpp running" << endl;
cout << mystr << endl;
cout << "i=" << i << endl;
cout << "x=" << x << endl;
cout << "y=" << y << endl;
cout << "excp1.cpp finished" << endl;
return 0;
} // end excp1.cpp
Execution output:
C++ excp1.cpp running
my string
i=31
x=34.56
y=1.23e+100
excp1.cpp finished
2. commands to execute the source code
at a minimum, Windows, Linux, MacOSX.
Windows: use nmake or cpp.bat
??? excp1.cpp
Linux:
g++ -o excpl excp1.cpp
excp1
MacOSX
g++ -o excp1 excp1.cpp
Sample Makefile:
# Makefile for C++ excp1_cpp.out file
all: excp1_cpp.out # can have many .out
excp1.out : excp1.cpp
g++ -o excp1 excp1.cpp
excp1 > excp1_cpp.out
cat excp1_cpp.out
3. You must be able to declare variables and arrays
and matrix of various types.
excp3.cpp source code
#include <iostream>
#include <vector> // C++ used below
#include <complex> // C++ used below
using namespace std;
typedef complex<double> cmplx; // just short type names
typedef vector<cmplx> c_vec; // complex vector
typedef vector<c_vec> c_mat; // complex matrix
int main(int argc, char *argv[]) // standard main program definition
{
// plain C vectors and matrix work
int vc1[10]; // C stuff works
int mc1[10][10];
double vcd1[10];
double mcd1[10][10];
cout << "excp3.cpp running" << endl;
vc1[0] = 13;
cout << "int vc1[] vc1[0]=" << vc1[0] << endl;
mc1[1][1] = 11;
cout << "int mc1[10][10] mc1[1][1]=" << mc1[1][1] << endl;
vcd1[0] = 45.67;
cout << "double vcd1[10] vcd1[0]=" << vcd1[0] << endl;
mcd1[2][2] = 22.22;
cout << "double mcd1[10][10] mcd1[2][2]=" << mcd1[2][2] << endl;
cout << endl;
// using include libraries
cmplx z;
cout << "test complex library" << endl;
z = cmplx(1.5,-3.5);
cout << "z = " << z << endl;
cout << "real(z) = " << real(z) << endl;
cout << "imag(z) = " << imag(z) << endl;
cout << "conj(z) = " << conj(z) << endl;
cout << "norm(z) = " << norm(z) << endl;
cout << "abs(z) = " << abs(z) << endl;
cout << "arg(z) = " << arg(z) << endl;
cout << "sqrt(norm(z)) = " << sqrt(norm(z)) << endl;
cout << "sqrt(z) = " << sqrt(z) << endl;
cout << "exp(z) = " << exp(z) << endl;
cout << "log(z) = " << log(z) << endl;
cout << "pow(z,2) = " << pow(z,2) << endl;
c_vec vcx1; // complex vector, initial size = zero
vcx1.reserve(4); // reserve space
vcx1[0] = cmplx(1.0,1.0);
vcx1[1] = cmplx(1.0,2.0);
vcx1[2] = cmplx(1.0,3.0);
vcx1[3] = cmplx(1.0,4.0);
cout << "vcx1[0]= " << vcx1[0] << endl;
cout << "vcx1[1]= " << vcx1[1] << endl;
cout << "vcx1[2]= " << vcx1[2] << endl;
cout << "vcx1[3]= " << vcx1[3] << endl;
cout << "vcx1.size() =" << vcx1.size() << " bad" << endl;
c_vec vcx2; // complex vector, initial size = zero
vcx2.push_back(cmplx(1.0,1.0)); // increase size
vcx2.push_back(cmplx(1.0,2.0));
vcx2.push_back(cmplx(1.0,3.0));
vcx2.push_back(cmplx(1.0,4.0));
cout << "vcx2[0]= " << vcx2[0] << endl;
cout << "vcx2[1]= " << vcx2[1] << endl;
cout << "vcx2[2]= " << vcx2[2] << endl;
cout << "vcx2[3]= " << vcx2[3] << endl;
cout << "vcx2.size() =" << vcx2.size() << endl;
c_mat mcx; // complex matrix, vector of vectors
c_vec mv; // need vector for matrix
for(int i=0; i<3; i++) // make 3 item vector
{
mv.push_back(cmplx(0.0,0.0)); // initialize to zero
}
for(int j=0; j<3; j++) // make each row of 3 by 3 matrix a vector
{
mcx.push_back(mv);
}
// initialize, identity matrix
for(int i=0; i<3; i++)
{
mcx[i][i] = cmplx(1.0,0.0);
}
// print matrix
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
cout << "mcx[" << i << "][" << j << "]=" << mcx[i][j] << endl;
}
}
cout << "excp3.cpp finished" << endl;
return 0;
} // end excp3.cpp
Execution output:
excp3.cpp running
int vc1[] vc1[0]=13
int mc1[10][10] mc1[1][1]=11
double vcd1[10] vcd1[0]=45.67
double mcd1[10][10] mcd1[2][2]=22.22
test complex library
z = (1.5,-3.5)
real(z) = 1.5
imag(z) = -3.5
conj(z) = (1.5,3.5)
norm(z) = 14.5
abs(z) = 3.80789
arg(z) = -1.1659
sqrt(norm(z)) = 3.80789
sqrt(z) = (1.62909,-1.07422)
exp(z) = (-4.19691,1.5721)
log(z) = (1.33707,-1.1659)
pow(z,2) = (-10,-10.5)
vcx1[0]= (1,1)
vcx1[1]= (1,2)
vcx1[2]= (1,3)
vcx1[3]= (1,4)
vcx1.size() =0 bad
vcx2[0]= (1,1)
vcx2[1]= (1,2)
vcx2[2]= (1,3)
vcx2[3]= (1,4)
vcx2.size() =4
mcx[0][0]=(1,0)
mcx[0][1]=(0,0)
mcx[0][2]=(0,0)
mcx[1][0]=(0,0)
mcx[1][1]=(1,0)
mcx[1][2]=(0,0)
mcx[2][0]=(0,0)
mcx[2][1]=(0,0)
mcx[2][2]=(1,0)
excp3.cpp finished
4. You need to be able to have loops, iteration statements
excp4.cpp source code
// excp4.cpp example loops, iteration
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) // standard main program definition
{
cout << "excp4.cpp running" << endl;
for(int i=3; i<15; i=i+2)
{
if(i==7) continue;
if(i>10) break;
cout << "i = " << i << endl;
}
char ch;
for(ch='Z'; ch&ge='A'; ch--)
{
cout << ch;
}
cout << endl;
for(ch='a'; ch&le='z'; ch++)
{
cout << ch;
}
cout << endl;
cout << "excp4.cpp finished" << endl;
return 0;
} // end excp4.cpp
Execution output:
excp4.cpp running
i = 3
i = 5
i = 9
ZYXWVUTSRQPONMLKJIHGFEDCBA
abcdefghijklmnopqrstuvwxyz
excp4.cpp finished
5. You need if then else conditional statements
excp5.cpp source code
// excp5.cpp example file if then else
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) // standard main program definition
{
cout << "excp5.cpp running" << endl;
double x = 2.0; // declare test variable
int i = 3;
if(x < 3.0) // < > &le= &ge= == != compare operations
{
printf("compare < > &le= &ge= == != x=%e \n",x);
}
if(x > 3.0 || i == 3 && i > 2) // || is or, && is and
{
printf("logic || is or, && is and i=%d \n", i);
}
if( x > 3.0)
{
printf("x > 3.0 \n");
}
else if(i < 3) // optional
{
printf("i < 3 \n");
}
else // optional, get here if none of the above true
{
printf("none of the above \n");
} // end if optional comment
cout << "excp5.cpp finished" << endl;
return 0;
} // end excp5.cpp
Execution output:
excp5.cpp running
compare < > &le= &ge= == != x=2.000000e+00
logic || is or, && is and i=3
none of the above
excp5.cpp finished
6. You need to be able to create functions, procedures,
subroutines.
excp6.cpp source code
// excp6.cpp example create functions and procedures
#include <iostream>
using namespace std;
// either put function definitions before main or put function prototypes
int add1(int i) // integer input, integer return
{
return i+1;
} // end add1
void proc1(int i) // void means no return value, this i is local
{ // could be at end of previous line
i = 10*i;
cout << "proc1 called, 10*i=" << i << endl;
} // end proc1 optional comment
// more common is to have function prototype here, function code below
double sum(int n, double A[]); // note ; not { here
int ret2(int i, int *j); // pass j by address
int main(int argc, char *argv[]) // standard main program definition
{
int j = 3;
int i = 1;
double arr[4]={1.0,2.0,3.0,4.0};
double val;
cout << "excp6.cpp running" << endl;
j = add1(j);
cout << "j = add1(j) returns " << j << endl;
proc1(i);
cout << "my i still= " << i << endl;
val = sum(4,arr); // code below
cout << "sum(4,arr)=" << val << endl;
cout << "arr[3] now =" << arr[3] << endl;
i = ret2(i, &j); // pass j by address
cout << "ret2 i=" << i << ", j=" << j << endl;
cout << "excp6.cpp finished" << endl;
return 0;
} // end main
// typical place for functions and procedures used only in this program
double sum(int n, double A[])
{
double asum = 0.0;
for(int i=0; i<n; i++) // for exactly one statement, no {} OK
asum += A[i];
A[n-1] = 0.0; // can change values in an array
return asum;
} // end sum
int ret2(int i, int *j) // all uses of j are *j
{
*j = i+2; // second formal parameter changed
return i+1;
} // end ret2
// end excp6.cpp
Execution output:
excp6.cpp running
j = add1(j) returns 4
proc1 called, 10*i=10
my i still= 1
sum(4,arr)=10
arr[3] now =0
ret2 i=2, j=3
excp6.cpp finished
7. You need to be able to read and write files in various formats.
excp7.cpp source code
// excp7.cpp example read and write files, "C" works, this is C++
#include <fstream> // this should also includes in iostream
#include <iostream> // but don't count on it, yet
#include <string> // for input buffer
using namespace std; // bring standard names into scope
int main(int argc, char *argv[]) // standard main program definition
{
fstream my_io; // a users variable for stream information
string word; // just a place for inputting
cout << "excp7.cpp running" << endl;
if(!my_io) // test, anything can go wrong
{
cout << "can not open junk_file for writing" << endl; // common error
}
my_io << "Some text" << endl ; // write to the file named "junk_file"
my_io << 42; // write more, int, string, float, etc.
my_io << " "; // need space between numbers
my_io << 123.456; // on same line
my_io << endl; // only end line '\n' when used
my_io << "more text" << endl; //
my_io << "line 4 " << endl; // last line
my_io.flush(); // force file to disk
my_io.close(); // close the file
cout << "junk_file written" << endl; // file should be written
my_io.open("junk_file", ios::in); // open an existing file for reading
if(!my_io) // test, anything can go wrong
{
cout << "can not open junk_file for reading" << endl; // common error
}
// notice that character translation is taking place,
// each word is input, not a line
while(!my_io.eof())
{
my_io >> word;
cout << word << endl;
}
// no my_io.getline(word);
my_io.close(); // just being neat, closing file being read
cout << "excp7.cpp finished" << endl;
return 0;
} // end excp7.cpp
Execution output:
excp7.cpp running
junk_file written
Some
text
42
123.456
more
text
line
4
4
excp7.cpp finished
8. You need to be able to use a number of files combined to
build a program. This may include packages, libraries,
operating system commands, header files, etc.
Other files needed:
math_64.cpp source code
math_64.cpp prototype file
main file: use link, web page formatted for web
excp8.cpp source code
#include <iostream>
using namespace std;
#include "math_64.cpp" // need function prototypes of
// separetely compiled functions and procedures
// compile g++ -o excp8 excp8.cpp math_64.cpp math_64.h
// or seperately compiled math_64.cpp math_64.o
int main(int argc, char *argv[]) // standard main program definition
{
long int n = 4;
double x[4] = {2.0, 3.0, 4.0, 5.0};
double y[4] = {5.0, -3.0, 2.0, 4.0};
double z[4] = {-1.0, -1.0, -1.0, 1.0}; // initial junk
double sum;
cout << "excp8.cpp running" << endl;
sum = dot(n, x, y);
printf("dot(n, x, y) = %f \n", sum);
printf(" \n");
printf("x = %f %f %f %f \n", x[0], x[1], x[2], x[3]);
printf("y = %f %f %f %f \n", y[0], y[1], y[2], y[3]);
cross(n, x, y, z);
printf("x cross y = z = %f, %f, %f, %f \n", z[0], z[1], z[2], z[3]);
cout << "excp8.cpp finished" << endl;
return 0;
} // end excp8.cpp
Execution output:
excp8.cpp running
x = 2, 3, 4, 5
y = 5, -3, 2, 4
dot(n, x, y) = 29.000000
x = 2.000000 3.000000 4.000000 5.000000
y = 5.000000 -3.000000 2.000000 4.000000
x cross y = z = 29.000000, -41.000000, 21.000000, -21.000000
excp8.cpp finished
Last updated 9/20/2019