c basics oop
play

C++ Basics & OOP 2019/3/14 History of C++ Developed by Bjarne - PowerPoint PPT Presentation

Poly- mor- phism Abstra ction Class OOP Inheri -tance En- capsu- lation Kuan-Ting Lai C++ Basics & OOP 2019/3/14 History of C++ Developed by Bjarne Stroustrup starting in 1979 at Bell Labs Originally named C with Classes


  1. Poly- mor- phism Abstra ction Class OOP Inheri -tance En- capsu- lation Kuan-Ting Lai C++ Basics & OOP 2019/3/14

  2. History of C++ • Developed by Bjarne Stroustrup starting in 1979 at Bell Labs • Originally named C with Classes but renamed C++ in 1983 • C++ is a statically typed, compiled, general-purpose, case-sensitive, free-form programming language

  3. C++ Hello World • Compile and Run on /* Linux This is Your First C Program: Hello World */ $ g++ hello.cpp #include <iostream> $ ./a.out using namespace std; Hello World // main() is where program execution begins. int main() { cout << "Hello World"; // prints Hello World return 0; }

  4. Semicolons and Blocks in C++ • Use semincolon ; as a statement terminator x = y; y = y + 1; add(x, y); • Blocks { } { cout << "Hello World"; return 0; }

  5. C++ Keywords asm else new this auto enum operator throw bool explicit private TRUE break export protected try case extern public typedef catch FALSE register typeid reinterpret_ char float typename class for return union cast const friend short unsigned const_cast goto signed using continue if sizeof virtual default inline static void delete int static_cast volatile do long struct wchar_t double mutable switch while dynamic_cast namespace template

  6. C++ Data Types Type Keyword Boolean bool Character char Integer int Floating point float Double floating point double Valueless void Wide character wchar_t

  7. C++ Modifiers • Data Type modifiers − signed − unsigned − long − short • Type qualifiers − Const − volatile − restrict

  8. Data Range Type Typical Bit Width Typical Range char 1byte -128 to 127 or 0 to 255 unsigned char 1byte 0 to 255 int 4bytes -2147483648 to 2147483647 INT_MAX Overflow unsigned int 4bytes 0 to 4294967295 short int 2bytes -32768 to 32767 unsigned short int 2bytes 0 to 65,535 long int 8bytes -2,147,483,648 to 2,147,483,647 unsigned long int 8bytes 0 to 4,294,967,295 long long int 8bytes -(2^63) to (2^63)-1 unsigned long long int 8bytes 0 to 18,446,744,073,709,551,615 float 4bytes double 8bytes long double 12bytes wchar_t 2 or 4 bytes 1 wide character

  9. Enumerated Types • enum enum-name { list of names } var-list; #include <stdio.h> enum SHAPE { CIRCLE, RECT = 3, TRIANGLE }; int main() { SHAPE sp = RECT; printf("%d\n", sp); sp = TRIANGLE; printf("%d\n", sp); sp = CIRCLE; printf("%d\n", sp); return 0; }

  10. Variable Scope #include <iostream> using namespace std; // Global variable declaration: int g; int main() { // Local variable declaration: int a, b; // actual initialization a = 10; b = 20; g = a + b; cout << g; return 0; }

  11. Literals /* Integer Literals */ 212 // Legal 215u // Legal 0xFeeL // Legal 078 // Illegal: 8 is not an octal digit 032UU // Illegal: cannot repeat a suffix /* Floating point literals */ 3.14159 // Legal 314159E-5L // Legal 510E // Illegal: incomplete exponent 210f // Illegal: no decimal or exponent .e55 // Illegal: missing integer or fraction

  12. Decision Making // if ... else ... if (score > 90) { grade = 'A'; } else if (score > 80) { False grade = 'B'; Condition } else grade = 'F'; True // Switch case Conditional switch (grade) Code { case 'A': printf('Great!\n'); break; case 'B': printf('OK!\ n’ ); break; case 'C': printf('Work harder!\ n’ ); break; default: printf('Game over\ n’ ); break; }

  13. C++ Loop // 1. for Loop, sum = 1 + 2 + … + 10 sum = 0; for (int i = 1; i <= 10; i++) sum += i; // 2. while Loop Conditional sum = 0; i = 1; Code while(i <= 10) { sum += i++; True } Condition // 3. do { ...} while (...); sum = 0; i = 1; do { False sum += i++; } while (i <= 10);

  14. C++ Functions • Declaration tells the compiler about a function's name, return type, and parameters • Definition provides the actual body of the function. return_type function_name ( parameter list ) { ... (code) ... }

  15. Max Function Example #include <iostream> using namespace std; // function declaration int max(int num1, int num2); int main() { // local variable declaration: int a = 100; int b = 200; cout << "Max value is : " << max(a, b) << endl; return 0; } // function returning the max between two numbers int max(int num1, int num2) { // local variable declaration int result; (num1 > num2) ? result = num1 : result = num2; return result; }

  16. #include <iostream> using namespace std; Function Arguments // Call by Pointer void swap(int *a, int *b) { int temp = *a; *a = *b; • Call by Value *b = temp; } • Call by Pointer // Call by Reference • Call by Reference void swap(int &a, int &b) { int temp = a; a = b; b = temp; } int main() { int a = 5, b = 23; swap(&a, &b); // Call by Pointer cout << "a = " << a << ", b = " << b << endl; swap(a, b); // Call by Reference cout << "a = " << a << ", b = " << b << endl; return 0; }

  17. C++ Arrays • Initialize an array double balance[] = { 1000.0, 2.0, 3.4, 17.0, 50.0 }; • Access elements float val[10]; // Initialize an array for (int i = 0; i < 10; i++) val[i] = (float)i; // Multi-dimensional array unsigned char img[256][256];

  18. C++ new & delete • Dynamic memory is allocated during runtime int *pData; pData = new int[10000]; //... // Do something using pData //... delete[] pData;

  19. C-Style Character String • 1D character array terminated by a null character '\0' int main() { char hello[6] = { 'H', 'e', 'l', 'l', 'o', '\0' }; char world[] = "World"; cout << hello << " " << world; return 0; }

  20. C++ Pointers • Pointer is a variable which saves the memory address of another variable − type *var-name; int main() { int var = 20; // actual variable declaration. int *p_var; // pointer variable p_var = &var; // store address of var in pointer variable cout << "Value of var variable: "; cout << var << endl; // print the address stored in ip pointer variable cout << "Address stored in p_var variable: "; cout << p_var << endl; // access the value at the address available in pointer cout << "Value of *p_var variable: "; cout << *p_var << endl; return 0; }

  21. C++ Reference • Reference is an alias of an existing variable − Cannot be NULL − Cannot be changed after initalized int main() { // declare simple variables int i; // declare reference variables int &r = i; i = 5; cout << "Value of i : " << i << endl; cout << "Value of i reference : " << r << endl; return 0; }

  22. C++ Class • Define a Box class with length, width, height class Box { public: double length; // Length of a box double width; // Width of a box double height; // Height of a box }; • Create instances Box Box1; // Declare Box1 of type Box Box Box2; // Declare Box2 of type Box

  23. #include <iostream> C++ Encapsulation #include <vector> #include <string> using namespace std; • Access control modifiers − public, private, protected class Box { public: double length; // Length of a box double width; // Width of a box double height; // Height of a box protected: vector<string> barcodes; private: char owner_name[64]; };

  24. shapes.h #ifndef _SHAPES_H_ C++ Inheritance (2-1) #define _SHAPES_H_ // Base class class Shape { public: • Syntax void setWidth(int w) { − class derived_class : width = w; } public base_class void setHeight(int h) { height = h; } protected: • Declare a base class “Shape” int width; int height; − Data: width, height }; − Functions: setWidth, setHeight // Derived class class Rectangle : public Shape { public: • Declare a derived class int area() { return (width * height); “Rectangle” } }; #endif

  25. C++ Inheritance (2-2) • Create a instance of Rectangle main.cpp #include <iostream> − Rectangle Rect; using namespace std; #include “ shapes.h ” • Call the functions of base class to set width & height int main() { Rectangle Rect; − Rect.setWidth(5); − Rect.setHeight(7); Rect.setWidth(5); Rect.setHeight(7); • Call the function of derived class // Print the area of the object. − Rect.area() cout << "Total area: " << Rect.area(); cout << endl; return 0; }

  26. C++ Abstraction • Force derived classes to implement a specific function • A virtual function “= 0” is a pure virtual function • In C++, pure virtual function is also called interface // Base class class Shape { public: void setWidth(int w) { width = w; } void setHeight(int h) { height = h; } virtual float area() = 0; protected: int width; int height; };

Recommend


More recommend