1 / 16

C++ / G4MICE Course

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.

xanthe
Télécharger la présentation

C++ / G4MICE Course

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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.

  3. 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.

  4. 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.

  5. 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); }

  6. 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

  7. 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.

  8. 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.

  9. 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).

  10. 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

  11. How to Compile • As with the first example: • g++ -o echo echo.cc

  12. 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

  13. Variable Declaration • <type> <variable name>; • <type> <variable name> = <value>; • int count; • int numPlanes = 3; • double pi = 3.141592654; • bool ok; • std::string name = “MICE”;

  14. 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)

  15. 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); }

  16. 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.

More Related