input/output, data types and variables and type casting. · § input/output, § data types and...

13
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

Upload: others

Post on 21-May-2020

36 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: input/output, data types and variables and type casting. · § input/output, § data types and variables § and type casting. ... § Input using cin § Output using cout See helloclass.cpp

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

Page 2: input/output, data types and variables and type casting. · § input/output, § data types and variables § and type casting. ... § Input using cin § Output using cout See helloclass.cpp

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

Page 3: input/output, data types and variables and type casting. · § input/output, § data types and variables § and type casting. ... § Input using cin § Output using cout See helloclass.cpp

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

Page 4: input/output, data types and variables and type casting. · § input/output, § data types and variables § and type casting. ... § Input using cin § Output using cout See helloclass.cpp

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

Page 5: input/output, data types and variables and type casting. · § input/output, § data types and variables § and type casting. ... § Input using cin § Output using cout See helloclass.cpp

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

Page 6: input/output, data types and variables and type casting. · § input/output, § data types and variables § and type casting. ... § Input using cin § Output using cout See helloclass.cpp

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.

Page 7: input/output, data types and variables and type casting. · § input/output, § data types and variables § and type casting. ... § Input using cin § Output using cout See helloclass.cpp

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…

Page 8: input/output, data types and variables and type casting. · § input/output, § data types and variables § and type casting. ... § Input using cin § Output using cout See helloclass.cpp

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;

Page 9: input/output, data types and variables and type casting. · § input/output, § data types and variables § and type casting. ... § Input using cin § Output using cout See helloclass.cpp

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;

Page 10: input/output, data types and variables and type casting. · § input/output, § data types and variables § and type casting. ... § Input using cin § Output using cout See helloclass.cpp

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

Page 11: input/output, data types and variables and type casting. · § input/output, § data types and variables § and type casting. ... § Input using cin § Output using cout See helloclass.cpp

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;

}

Page 12: input/output, data types and variables and type casting. · § input/output, § data types and variables § and type casting. ... § Input using cin § Output using cout See helloclass.cpp

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.

Page 13: input/output, data types and variables and type casting. · § input/output, § data types and variables § and type casting. ... § Input using cin § Output using cout See helloclass.cpp

Data Types & Variables

Summary§ We covered the basics of writing C++ programs

including § input/output, § data types and variables § and type casting.

Next Time§ We will learn about branching and looping.

© J.S. Bradbury CSCI 1060U Lecture 2 Slide 13