input/output, data types and variables and type · pdf filedata types & variables overview...
TRANSCRIPT
Data Types & Variables
Overview§ We’ll look at the basics of writing C++ programs
including § input/output, § data types and variables § and type casting.
© J.S. Bradbury CSCI 1060U Lecture 2 Slide 1
Command-line compilation with g++
Last Lecture§ G++ is the C++ compiler in the GNU Compiler
Collection (GCC)§ We will use G++ in this class in the Linux operating system.
CSCI 1060U Lecture 2 Slide 2© J.S. Bradbury
G++C++
source code
Machine code
Source: http://opensourcepedia.com
Command-line compilation with g++
Last Lecture§ The first step in learning how to use g++ is to
compile a single file program
§ The above command will compile the program and create a machine code executable call a.out which can be run by typing:
CSCI 1060U Lecture 2 Slide 3© J.S. Bradbury
g++ example_program.cpp
./a.out
Command-line compilation with g++
Last Lecture§ If you want to name the executable something other
than a.out you can use the –o option
§ The above command will compile the program and links it to create the executable. This is the same as:
CSCI 1060U Lecture 2 Slide 4© J.S. Bradbury
g++ example_program.cpp –o ex_prog
g++ -c example_program.cppg++ example_program.o –o ex_prog
The Basic Structure of a C++ Program
© J.S. Bradbury CSCI 1060U Lecture 2 Slide 5
//libraries needed by your program are listed first#include <iostream>
//the next line is needed – will explain later Jusing namespace std;
//the main function is where we will write our codeint main() {
…return 0;
}
Last Lecture
C++ Input and Output
© J.S. Bradbury CSCI 1060U Lecture 2 Slide 6
§ Input using cin§ Output using cout
See helloclass.cpp for an example of output using cout.
See hello_double.cpp for an example of formatting output using cout.
C++ Data Types
© J.S. Bradbury CSCI 1060U Lecture 2 Slide 7
§ C++ contains many different types of data including:§ Character types – char
§ ‘A’, ‘B’, ‘C’, ‘1’§ Integer types – int
§ 1, 423, -6§ Floating-point types – float, double
§ 1.0, 0.54, 43.4§ Boolean type – bool
§ true, false§ and more…
C++ Variables
© J.S. Bradbury CSCI 1060U Lecture 2 Slide 8
§ Variable declaration§ In C++ variables must be declared before being used!
(strongly typed)
§ Why?
int x;float y;int a, b, c;
C++ Variables
© J.S. Bradbury CSCI 1060U Lecture 2 Slide 9
§ Variable declaration§ In C++ variables must be declared before being used!
(strongly typed)
§ Why?§ So that the compiler knows how much space to
allocate each variable!
int x;float y;int a, b, c;
C++ Variables
© J.S. Bradbury CSCI 1060U Lecture 2 Slide 10
§ Variable initialization
int x = 6; //c-like initialization//most common
int x (6); //constructor intialization
int x {6}; //uniform initialization
C++ Strings
© J.S. Bradbury CSCI 1060U Lecture 2 Slide 11
§ In addition to fundamental data types (bool, int) there are also compound types (string)§ Why is string a compound type? It is a sequence of chars
§ Requires the string library to work
#include <string>using namespace std;
int main() {string hello = “Hello class”; return 0;
}
C++ Type Casting
§ In C++ you can convert data of one type to another type using type casting
CSCI 1060U Lecture 2 Slide 12© J.S. Bradbury
See type_casting_ex.cpp for examples of casting ints, floats and chars.