C++ Basics & OOP
Kuan-Ting Lai 2019/3/14
OOP
Class Abstra ction Inheri
- tance
En- capsu- lation Poly- mor- phism
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
Class Abstra ction Inheri
En- capsu- lation Poly- mor- phism
starting in 1979 at Bell Labs
but renamed C++ in 1983
general-purpose, case-sensitive, free-form programming language
Linux
$ g++ hello.cpp $ ./a.out Hello World /* This is Your First C Program: Hello World */ #include <iostream> using namespace std; // main() is where program execution begins. int main() { cout << "Hello World"; // prints Hello World return 0; }
x = y; y = y + 1; add(x, y); { cout << "Hello World"; return 0; }
asm else new this auto enum
throw bool explicit private TRUE break export protected try case extern public typedef catch FALSE register typeid char float reinterpret_ cast typename class for return union 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
− signed − unsigned − long − short
− Const − volatile − restrict
Type Typical Bit Width Typical Range char 1byte
unsigned char 1byte 0 to 255 int 4bytes
unsigned int 4bytes 0 to 4294967295 short int 2bytes
unsigned short int 2bytes 0 to 65,535 long int 8bytes
unsigned long int 8bytes 0 to 4,294,967,295 long long int 8bytes
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
INT_MAX Overflow
#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; }
#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; }
/* 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
Condition Conditional Code True False // if ... else ... if (score > 90) { grade = 'A'; } else if (score > 80) { grade = 'B'; } else grade = 'F'; // Switch case switch (grade) { 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; }
// 1. for Loop, sum = 1 + 2 + … + 10 sum = 0; for (int i = 1; i <= 10; i++) sum += i; // 2. while Loop sum = 0; i = 1; while(i <= 10) { sum += i++; } // 3. do { ...} while (...); sum = 0; i = 1; do { sum += i++; } while (i <= 10); Condition Conditional Code True False
and parameters
return_type function_name(parameter list) { ... (code) ... }
#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; }
#include <iostream> using namespace std; // Call by Pointer void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } // 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; }
double balance[] = { 1000.0, 2.0, 3.4, 17.0, 50.0 };
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];
during runtime
int *pData; pData = new int[10000]; //... // Do something using pData //... delete[] pData;
int main() { char hello[6] = { 'H', 'e', 'l', 'l', 'o', '\0' }; char world[] = "World"; cout << hello << " " << world; return 0; }
− 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; }
− 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; }
class Box { public: double length; // Length of a box double width; // Width of a box double height; // Height of a box };
Box Box1; // Declare Box1 of type Box Box Box2; // Declare Box2 of type Box
− public, private, protected
#include <iostream> #include <vector> #include <string> using namespace std; 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]; };
− class derived_class : public base_class
− Data: width, height − Functions: setWidth, setHeight
“Rectangle”
#ifndef _SHAPES_H_ #define _SHAPES_H_ // Base class class Shape { public: void setWidth(int w) { width = w; } void setHeight(int h) { height = h; } protected: int width; int height; }; // Derived class class Rectangle : public Shape { public: int area() { return (width * height); } }; #endif
shapes.h
− Rectangle Rect;
set width & height
− Rect.setWidth(5); − Rect.setHeight(7);
− Rect.area()
#include <iostream> using namespace std; #include “shapes.h” int main() { Rectangle Rect; Rect.setWidth(5); Rect.setHeight(7); // Print the area of the object. cout << "Total area: " << Rect.area(); cout << endl; return 0; }
main.cpp
// 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; };
class Rectangle : public Shape { public: Rectangle(int a = 0, int b = 0) :Shape(a, b) { } float area() { cout << "Rectangle class area :" << endl; return (width * height); } }; class Triangle : public Shape { public: Triangle(int a = 0, int b = 0) :Shape(a, b) { } float area() { cout << "Triangle class area :" << endl; return (width * height / 2); } };
− Shape *shape;
classes
− Rectangle rec(10, 7); − Triangle tri(10, 5);
− shape = &rec;
− shape->area();
#include <iostream> using namespace std; #include “shapes.h” int main() { Shape *shape; Rectangle rec(10, 7); Triangle tri(10, 5); // store the address of Rectangle shape = &rec; // call rectangle area. shape->area(); // store the address of Triangle shape = &tri; // call triangle area. shape->area(); return 0; }
− Called when an object is created − Initialize data
− Called when an object is deleted − Can be used to clean data
// Base class class Shape { public: Shape(int a=0, int b=0) { width = a; height = b; } ~Shape(){} void setWidth(int w) { width = w; } void setHeight(int h) { height = h; } virtual float area() = 0; protected: int width; int height; };
Chapter 2, 1994
You-Get” (WYSIWYG) editor called Lexi.
transparent enclosure
different OS may have different class structure.
without complicating the document structure's implementation.
Patterns,” 1994