Chap 0: Overview Basics - 1 � Comments � Overview of basic C++ syntax � single line: // � Refresh programming basics � multi- ������������� � C++ Vs. Java differences � Identifiers and keywords � Coding conventions used � {_a-zA-Z}{_a-zA-Z0-9}* � keywords are reserved words � Fundamental data types � bool, char, int, float, double � modifiers: signed/unsigned, short/long EECS 268 Programming II 1 EECS 268 Programming II 2 Basics - 2 Basics - 3 � Assignments and expressions � Variables � arithmetic expressions � double radius; int count, i; � relational and logical expressions � Constants � Implicit type conversion � ����������������� � automatic type conversion with no loss of � named: const double PI = 3.14159; precision � typedef statement � Explicit type conversion � typedef double Real; � static_cast<type>(expression) � int ivol = static_cast<int>(volume); EECS 268 Programming II 3 EECS 268 Programming II 4
Basics - 4 Basics - 5 � Functions � Input type name (formal argument list) � int a; cin >> a; { � ����������������������� body } � Output � Selection statements � �������������������������������� \ ��� � if, if-else, if-elseif-else, switch-case � ��������������������� \ ������� � Iteration statements � while, do-while, for � break, continue see A1-basics.cpp EECS 268 Programming II 5 EECS 268 Programming II 6 Basics - 6 Basics - 7 � Arrays � Structures � to group data items � one dimensional: int arr[100]; arr[i] = 10; struct Person{ � multi-dimensional: int arr[100][10]; arr[i][j] = 10; string name; � arrays are passed to functions by reference int age; � C++ strings double gpa; � string str ������������� }; � size(), length(), compare, concatenate, index, etc. � C strings struct Person students[100]; � char str[100]; �������������������������������� � ���������������� \ ������������������������ students[0].age = 20; � strlen(), strcpy(), strcmp(), strcat(), index, etc. � structs are passed by value to another function see A1-CppJava.cpp EECS 268 Programming II 7 EECS 268 Programming II 8
Basics - 8 C++ (Compared to Java) � structs used to group data variables. � File input / output � provide persistent storage � C++ uses preprocessor for macros, file-inclusion. � ifstream, ofstream, fstream � C++ can have stand-alone functions. � Constants/variables can be defined globally, in classes, ifstream inFile; ofstream ofile; or methods. inFile.open ������������� ofile.open(filename); � bool (vs. boolean) inFile >> ch; ofile << ch; � Explicit memory deallocation ( delete) � See: inFile.get(ch); ofile.put(ch); � http://people.eecs.ku.edu/~miller/Courses/JavaToC++.html ch = inFile.peek(); ofile.open ��������� � Appendix A.12 in textbook ios::app); inFile.ignore(n); see A1-BookStoreCustomer.cpp EECS 268 Programming II 9 EECS 268 Programming II see A1-fcopy.cpp 10 Examples Coding Conventions Used � See A1-basics.cpp � Need � to make it easier to read and maintain code � See A1-fcopy.cpp � very important for large code bases � See A1-CppJava -- .cpp / .java � when multiple contributors � See A1-BookStoreCustomer -- .cpp / .java � Multiple coding styles are prevalent � people have differing tastes and preferences � We impose some common coding practices for this class EECS 268 Programming II 11 EECS 268 Programming II 12
Coding Conventions Used Coding Conventions Used � Indentation � Make Files � 2 or 4 spaces � to keep list of dependencies and to build all your � For function definitions project files � open/close brace should be on its own line � discussed further in Lab-1 � block comment before each function tells what it does � Header (.h) and implementation (.cpp) files � For braces within functions (loops/branches) � ADT interface (class definition) should be in .h file � open brace should be on same line as construct � Comments � close brace should be on its own line � Line width � �������� -- for block (multi-line) comments � a maximum of 80 characters on a single line � ������ -- for single-line comments EECS 268 Programming II 13 EECS 268 Programming II 14 Coding Conventions Used � Vertical white space and comments � blank line between consecutive code constructs � comments before important code constructs � Horizontal white space � make it readable! if((a==b) || (c<a)) over if( ( a == b ) || ( c < a ) ) and if((a==b)||(c<a)) EECS 268 Programming II 15
Recommend
More recommend