160 likes | 298 Views
C++ / G4MICE Course. Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++ input and output streams. First exposure to simple C/C++ variables. Session 1 - Introduction. Course Format.
E N D
C++ / G4MICE Course Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++ input and output streams. First exposure to simple C/C++ variables. Session 1 - Introduction
Course Format • 12 Sessions (each one and a half hours) over three days. • Each session consists of a 20-30 minute “lecture” followed by guided hands-on work.
Editing Text Files • Hopefully everyone is already familiar with at least one UNIX/linux text editor. • If not, there are several options, including: • vi / vim • emacs • pico • Use whichever editor you prefer. • If you aren’t already familiar with one, we will start with that in the hands-on part of this session.
g++ compiler • The standard G4MICE installation currently comes with version 3.2.3 or g++. • In a future release we will migrate to version 4. • The way that the compiler works is the same in both cases.
helloworld.cc // helloworld.cc // // Written for the C++ / G4MICE course // // M. Ellis - March 2008 #include <iostream> int main() { std::cout << "Hello World!" << std::endl; exit(1); }
How to Compile • g++ -o helloworld helloworld.cc • This will produce an output executable file “helloworld” (helloworld.exe if you are using cygwin) from the source code helloworld.cc which can be run: • ./helloworld
helloworld.cc - explanation // helloworld.cc // // Written for the C++ / G4MICE course // // M. Ellis - March 2008 #include <iostream> int main() { std::cout << "Hello World!" << std::endl; exit(1); } C++ allows two types of comments, this example uses the characters // at the point in each line where the comments begin. This is a pre-processor command that Tells the compiler to insert code from The file iostream.
C++ Streams • C++ streams can be used to send or receive data to/from files, the keyboard, terminal, other processes, devices, ... • There are two key operators for streams: • << • >> • These can be thought of as reading or writing.
cout / cin / cerr • There are 3 variables defined for you that refer to specific streams that are commonly used: • cout is the “standard output”. • cin is the “standard input”. • cerr is the “standard error”. • Writing to the cout stream will result in text appearing on the screen. • Reading from the cin stream will store whatever is typed on the keyboard in some variable(s).
echo.cc This is an example of the other kind of comment which can cover more than one line. // echo.cc /* Written for the C++ / G4MICE course M. Ellis - March 2008 */ #include <iostream> #include <string> int main() { std::string message; std::cout << "Please enter a message for me: "; std::cin >> message; std::cout << "Thank you, I have received your message: " << message << std::endl; exit(1); } Additional #include so that we can use strings
How to Compile • As with the first example: • g++ -o echo echo.cc
Some C/C++ Variables • int (unsigned int, long int, short int, ... ) • Integers of varying size (number of bits) • bool • Boolean variable can only be true or false • char • A single character (8 bits) • double (float, long double, ... ) • Floating point numbers • string • String of characters
Variable Declaration • <type> <variable name>; • <type> <variable name> = <value>; • int count; • int numPlanes = 3; • double pi = 3.141592654; • bool ok; • std::string name = “MICE”;
What are ; and std:: ??? • Commands in C/C++ must end with a ; • C/C++ is free format, so you can have as much or as little white space as you like. • Later on in the course we will look at namespaces and scope. • For now, it is sufficient to know that a number of types and variables that we are using are in a namespace called “std”: • std::cout (the standard output stream variable) • std::string (the string type)
variables.cc // variables.cc /* Written for the C++ / G4MICE course M. Ellis - March 2008 */ #include <iostream> #include <string> int main() { int age; double height; std::string name; std::cout << "Please tell me your name: "; std::cin >> name; std::cout << "Thank you " << name << ", what is your age? "; std::cin >> age; std::cout << "Finally, how tall are you in inches? "; std::cin >> height; std::cout << std::endl << std::endl; std::cout << name << " is " << age << " years old and is " << height << " inches tall" << std::endl; exit(1); }
Exercises • Compile and run helloworld.cc • Compile and run echo.cc • Compile and run variables.cc • Create your own code from scratch, have it use the standard input and standard output and at least 3 variables of different types.