C++ Basics
C++ Basics
Slide 2 C++ C is a programming language developed in the 1970s with the UNIX operating system C is procedural, and efficient and portable across different hardware platforms C++ is a better C. and c is a subset of c++ C programs should run in C++ C++ supports Data abstraction object-oriented programming Generic programming
Slide 2 C++ • C is a programming language developed in the 1970s with the UNIX operating system • C is ‘procedural’, and efficient and portable across different hardware platforms • C++ is a better C, and C is a subset of C++ • C programs should run in C++ • C++ supports • Data abstraction • “object-oriented” programming • Generic programming
Slide 3 The first program #include using namespace stdi int main(i cout < Hi!<< endl
Slide 3 The first program #include using namespace std; int main(){ cout << “Hi!” << endl; }
Slide 4 Variables and assignments The most fundamental programming language constructs!
Slide 4 Variables and Assignments The most fundamental programming language constructs!
Slide 5 What is a variable? A variable(also an object) is a named memory location for a value (that we can write to, retrieve, and manipulate) X 1000 Name -n identifier Type - size It can be best thought of as a container/box for a value
Slide 5 What is a variable? Name --- identifier Type --- size A variable (also an object) is a named memory location for a value (that we can write to, retrieve, and manipulate) 1000 X It can be best thought of as a container/box for a value
Slide 6 Variable names -Identifiers An identifier is a name for variables constants functions etc It consists of a letter(including the underscore)followed by any sequence of letters digits or underscores letter underscore() Names are case-sensitive. The following are unique identifiers Hello, hello, whoami, whoAMi, WhoAmI Names cannot have special characters in them e.g., X=Y, J-20,#007, etc are invalid identifiers C++ keywords cannot be used as identifiers Choose identifiers that are meaningful and easy to remember
Slide 6 Variable names --Identifiers • An identifier is a name for variables, constants, functions, etc. • It consists of a letter (including the underscore) followed by any sequence of letters, digits or underscores • Names are case-sensitive. The following are unique identifiers: Hello, hello, whoami, whoAMI, WhoAmI • Names cannot have special characters in them e.g., X=Y, J-20, #007, etc. are invalid identifiers. • C++ keywords cannot be used as identifiers. • Choose identifiers that are meaningful and easy to remember
Slide 7 Keywords(reserved words) Reserved words have a special meaning in C++ The list of reserved words asm auto, bool, break, case, catch, char class, const, continue, default, delete do double, else enum, extern, float, for friend, goto, ifr include, inline, int, long, namespace, new, operator, private, protected, public, register, return, short, signed sizeof, static, struct, switch, template this, throw, try, typedef, union, unsigned, using, virtual, void, volatile, while
Slide 7 Keywords (Reserved words) • Reserved words have a special meaning in C++. • The list of reserved words: asm, auto, bool, break, case, catch, char, class, const, continue, default, delete, do, double, else, enum, extern, float, for, friend, goto, if, include, inline, int, long, namespace, new, operator, private, protected, public, register, return, short, signed, sizeof, static, struct, switch, template, this, throw, try, typedef, union, unsigned, using, virtual, void, volatile, while
Slide 8 Variable declarations Variable declaration syntax <identifier Examples int nickel; A variable must be declared before it can be used! int main(t 5;// illegal: x was not declared what is syntax, semantics?
Slide 8 Variable Declarations Variable declaration syntax: ; Examples: int nickel; int penny; A variable must be declared before it can be used! int main(){ x = 5; // illegal: x was not declared } what is syntax, semantics?
Slide 9 a variable can be initialized in a declaration int x=3 Several variables of the same type can be declared in the same declaration( though it is better to put them on separate lines) int height, width; A variable must have only one type. For example, a variable of the type int can only hold integer values
Slide 9 • A variable can be initialized in a declaration: int x = 3; • Several variables of the same type can be declared in the same declaration (though it is better to put them on separate lines): int height, width; • A variable must have only one type. For example, a variable of the type int can only hold integer values
Slide 10 Variable initialization a variable can be initialized in a declaration int x=3 Always initialize your variables, which are not always automatically initialized Different ways of initialization int x=3 int x(3)i
Slide 10 Variable initialization • A variable can be initialized in a declaration: int x = 3; • Always initialize your variables, which are not always automatically initialized!!! • Different ways of initialization int x = 3; int x(3);