C++ Basics CSE250 Derived from slides provided by Dr. Hung Ngo Logistics • Office hours – Tuesday 2-3 – Thursday 2-3 – Wednesday 4-5? • Course repository link on Piazza C++ • There will be a lot of information in this lecture • You should practice writing C++ How and Where to compile • Command line • IDE of choice • Up to you, but your submissions must compile and run on the CSE servers The procedural parts of c++ C-style strings Pointers and references GETTING STARTED IN C++ Agenda • The main body and cout • Fundamental data types • Declarations and definitions • Control structures • //References, pass-by-value vs pass-by-reference 5/28/2016 CSE 250, SUNY Buffalo, © Hung Q. Ngo 6 C++ is an OO extension of C C++ has both procedural and object-oriented features We will use C++ as a procedural language with objects THE MAIN BODY AND COUT 5/28/2016 CSE 250, SUNY Buffalo, © Hung Q. Ngo 7 Every C++ program must have a main() // hw.cpp Somewhat similar to Java’s import #include <iostream> int main() { std::cout << "Hello world" << std::endl; return 0; } Similar to Java’s namespace • iostream is a C++ abstraction to perform input/output operations on media using “streams” Important in system programming, not in CSE 250. – Media as character streams: keyboard, files, console, network sockets – cout is an output stream object associated with the console – “writing to cout” using << will print to the console 5/28/2016 CSE 250, SUNY Buffalo, © Hung Q. Ngo 8 cout is pretty easy to use #include <iostream> using namespace std; // questionable int main() string string string { my_name = "David Blaine"; my_text_editor = "Emacs"; my_home_os = "Windows"; cout << << << << << return 0; "My name is " << my_name << endl "I was able to install and test g++ and " "the text editor " << my_text_editor << '\n' "in my home computer/laptop, which runs " my_home_os << endl; } 5/28/2016 CSE 250, SUNY Buffalo, © Hung Q. Ngo 9 They are what you expect them to be, similar to those in Java FUNDAMENTAL DATA TYPES 5/28/2016 CSE 250, SUNY Buffalo, © Hung Q. Ngo 10 The fundamental data types Boolean, characters, integers, floats, and void • bool : true or false • char : a character • int : an integer • float : a floating-point real number • wchar_t, char16_t, char32_t, short, long, signed, unsigned, float, double, … • And cout will output a variable with the appropriate format for the above types 5/28/2016 CSE 250, SUNY Buffalo, © Hung Q. Ngo 11 Cout is easy to use on basic types #include <iostream> using namespace std; // bad practice! int main() { string name char c int i bool smart double avg = = = = = cout << "I am << "smart << "c = " << "i = " << "avg = return 0; "David Blaine"; 'H'; 12345; true; 3.5; " << name << endl = " << smart << endl << c << endl << i << endl " << avg << endl; } 5/28/2016 CSE 250, SUNY Buffalo, © Hung Q. Ngo 12 Every name must be declared before used There must always be at most one definition for each named entity There can be many declarations Many declarations are also definitions DECLARATIONS AND DEFINITIONS 5/28/2016 CSE 250, SUNY Buffalo, © Hung Q. Ngo 13 Declarations // variable declarations string name; char c; int i; bool smart; double avg; // a type declaration (the type is a struct) struct Date; // a const bool declaration const bool i_am_smart = true; // a function declaration int foo(int); 5/28/2016 CSE 250, SUNY Buffalo, © Hung Q. Ngo 14 Definitions // definition of function foo() int foo(int x) { return x*x; } // definition of struct Date struct Date { int d; int m; int y; }; 5/28/2016 CSE 250, SUNY Buffalo, © Hung Q. Ngo 15 Many declarations are also definitions // these declarations are *also* definitions string name; char c; int i; bool smart; double avg; // these declarations are *not* definitions int foo(int); struct Date; extern int age; typedef vector<int> Int_Vector; 5/28/2016 CSE 250, SUNY Buffalo, © Hung Q. Ngo 16 Again • Every name (identifier) must be declared before use • A name can be declared multiples times – Helps with separate compilation. – Defined in one file, declared and used in others – More about this in a couple of lectures • There is ≤ 1 definition per name • Some declarations are also definitions 5/28/2016 CSE 250, SUNY Buffalo, © Hung Q. Ngo 17