chapter 8 object-oriented programming (oop) dr. Đào trung kiên – hanoi univ. of science and...

24
Chapter 8: Object-Oriented Programming (OOP) 1 EE3490E: Programming – T1 2015/2016 Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Upload: automoto-aficionado-peru

Post on 14-Apr-2016

229 views

Category:

Documents


1 download

DESCRIPTION

Chapter 8 Object-Oriented Programming (OOP) Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

TRANSCRIPT

Page 1: Chapter 8 Object-Oriented Programming (OOP) Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Chapter 8: Object-Oriented Programming (OOP)

1 EE3490E: Programming – T1 2015/2016Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Page 2: Chapter 8 Object-Oriented Programming (OOP) Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Introduction to C++

2 EE3490E: Programming – T1 2015/2016Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Page 3: Chapter 8 Object-Oriented Programming (OOP) Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Overview Additional features compared to C:

Object-oriented programming (OOP) Generic programming (template) Many other small changes

Small changes: Conventional source files with .cpp extension main() function can be declared with void return type:

void main() { … }

Use // to comment until end of line area = PI*r*r; // PI = 3.14

Built-in bool type and false, true boolean constants : bool b1 = true, b2 = false;

Variables and constants in C++ can be declared anywhere in functions (not limited to function beginning only), even in for loops

New function-like syntax for type casting: int(5.32) enum, struct, union keywords become optional in variable declaration

3 EE3490E: Programming – T1 2015/2016Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Page 4: Chapter 8 Object-Oriented Programming (OOP) Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Some more significant features… Reference types: are pointers by nature

int a = 5;int& b = a;b = 10; // a = 10

int& foo(int& x){ x = 2; return x; }

int y = 1;foo(y); // y = 2foo(y) = 3; // y = 3

Namespace namespace ABC {

int x;int setX(int y) { x = y; }

}

ABC::setX(20);int z = ABC::x;

using namespace ABC;setX(40);

4 EE3490E: Programming – T1 2015/2016Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Page 5: Chapter 8 Object-Oriented Programming (OOP) Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Some more significant features… (cont.) Dynamic memory allocation Allocation: new and new[] operators

int* a = new int;float* b = new float(5.23f);long* c = new long[5];

Deallocation: delete and delete[] operators delete a;delete[] c;

Attn: do not mix malloc()/free() and new/delete: Memory allocated by malloc() must be deallocated by free() Memory allocated by new must be deallocated by delete

Function overload (functions with same name but different parameters): int sum(int a, int b) {...}int sum(int a, int b, int c) {...}double sum(double a, double b) {...}double sum(double a, double b, double c) {...}

Exception handling try ... catch: self-study5 EE3490E: Programming – T1 2015/2016

Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Page 6: Chapter 8 Object-Oriented Programming (OOP) Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

The first C++ program Example:

#include <iostream>

using namespace std;

void main() {

int n;

cout << "Enter n: ";

cin >> n;

cout << "n = " << n << endl;

}

Input/output with C++: Using iostream library “cout <<” used for output to stdout (console by default) “cin >>” used for input from stdin (keyboard by default) Objects of C++ standard library defined in a namespace named std. If “using

…” is not used, one must use std::cout, std::cin, std::endl instead

6 EE3490E: Programming – T1 2015/2016Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Page 7: Chapter 8 Object-Oriented Programming (OOP) Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Classes and objects

7 EE3490E: Programming – T1 2015/2016Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Page 8: Chapter 8 Object-Oriented Programming (OOP) Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Introduction From real world… Objects are things, facts, entities,… which are characterized by

properties and can realize certain operations. Ex: Each student is an object characterized by: name, age,

department, class, year,… and can realizes: study, doing exercises, going to class, doing tests,…

Each cellphone is an object characterized by: SIM number, model, size,… and can realizes: making call, texting, answering to calls, denying calls,…

Classes are description of properties and operations of each type of objects Simpler consideration: student are objects while the notion of

student is a class, similarly for cellphones and the notion of cellphone

8 EE3490E: Programming – T1 2015/2016Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Page 9: Chapter 8 Object-Oriented Programming (OOP) Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Introduction (cont.) … to programming:

Class is a data type extended from structure type (struct). Apart fromfields that correspond to object properties, methods which are similar to functions are added to realize operations

Object is a variable declared with type of the defined class

From structural programming: function-centered… struct Student {

char name[20];int year;

};void go_to_class(Student& s, int room) { ... }void do_test(Student& s) { ... }

Student s = { ... };go_to_class(s, 103);do_test(s);

9 EE3490E: Programming – T1 2015/2016Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Page 10: Chapter 8 Object-Oriented Programming (OOP) Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Introduction (cont.) …to object-oriented programming:

struct Student {char name[20];int year;

void go_to_class(int room) { ... }void do_test() { ... }

};

Student s = { ... };s.go_to_class(103);s.do_test();

Functions become methods of class and can access directly to properties (member variables) of called object

Objects (variables) become subject of called methods (functions) instead of being passed as parameters objects play a central role in programming

10 EE3490E: Programming – T1 2015/2016Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Page 11: Chapter 8 Object-Oriented Programming (OOP) Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Scope of members public properties and methods are accessible from outside of object, private ones are limited to internal uses struct Student {

public:

char name[20];

void go_to_class(int room) { ... }

private:

int year;

void do_test() { ... }

};

strcpy(s.name, "Nguyen Hung Long"); // OK

s.go_to_class(103); // OK

s.year = 2010; // error

s.do_test(); // error

11 EE3490E: Programming – T1 2015/2016Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Page 12: Chapter 8 Object-Oriented Programming (OOP) Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

class and struct In C++, to avoid confusion with traditional structure types, use class

keyword to declare classes: class Student { ... };

class and struct types differ only in default member scopes: public for struct and private for class struct A {

int a; // public

};

class B {

int b; // private

};

Very often, member variables are defined as private, and are accessed via member methods hide internal data, better encapsulation

12 EE3490E: Programming – T1 2015/2016Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Page 13: Chapter 8 Object-Oriented Programming (OOP) Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Class example#include <iostream>

class Circle {

private:

double r;

public:

void setR(double rr) { r = rr; }

double area() { return 3.14*r*r; }

double perimeter() { return 3.14*2.*r; }

};

void main() {

Circle c;

c.setR(1.23);

std::cout << "Area: " << c.area() << std::endl

<< "Perimeter: " << c.perimeter() << std::endl;

}

13 EE3490E: Programming – T1 2015/2016Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Page 14: Chapter 8 Object-Oriented Programming (OOP) Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Constructors Are initialization methods for objects. They do not return values, and invoked

automatically every times an object is created. Possible to define many with different parameters, compiler knows which one

to call based on given parameters. If none defined, a default one is generated. class Circle {

public:Circle() { r = 0.; } // default constructor with no parameter)Circle(double rr) { r = rr; }...

};

Circle c1, c2(), c3[3]; // constructor 1Circle c4(1.23); // constructor 2c1 = Circle; // errorc1 = Circle(); // constructor 1c1 = Circle(2.33); // constructor 2

Circle* c5 = new Circle; // constructor 1Circle* c6 = new Circle(); // constructor 1Circle* c7 = new Circle(3.22); // constructor 2Circle* c8 = new Circle[3]; // constructor 1

14 EE3490E: Programming – T1 2015/2016Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Page 15: Chapter 8 Object-Oriented Programming (OOP) Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Copy constructors Used in creation of new objects from existing of the same class. Two

syntaxes for invoking copy constructors: class Circle {

...Circle(const Circle& c) { r = c.r; }

};

Circle c1(2.5); // do not use copy constructorCircle c2 = c1, c3(c1); // use copy constructorc2 = c1; // assignment, not copy constructor

Also used in creation of temporary objects for function calls: void func1(Circle c) { ... }

void func2(const Circle& c) { ... }func1(c1); // create temp. obj. c by copying from c1 with CSCfunc2(c1); // no obj. created, CSC is not invoked

should use “const Circle& c” for function parameter instead of “Circle& c” for better performance

If no copy constructor defined, compiler generates a default one which copies all member variables from original object to the new object

15 EE3490E: Programming – T1 2015/2016Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Page 16: Chapter 8 Object-Oriented Programming (OOP) Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Cast constructors Used in creation of new objects from existing objects of different classes.

Two syntaxes for invoking cast constructors: class Ellipse {

...Ellipse(const Circle& c) { rx = ry = c.r; }

};

Ellipse e1(c1), e2 = c2; // use cast constructor: Circle -> Ellipsee1 = c1; // create a temp. object by implicit cast: Circle -> Ellipse

// then invoke assignment: Ellipse -> Ellipse

Also used in creation of temporary objects for type casting: void func3(const Ellipse& e) { ... }

func3(c1); // implicit use of cast constructorcout << ((Ellipse)c1).area(); // explicit use of cast constructor

Possible to add “explicit” keyword in declaration of cast constructor to avoid being used in implicit casts

Review Circle class: class Circle {

...Circle(double rr) {r = rr; } // is cast constructor: int -> Circle

};

16 EE3490E: Programming – T1 2015/2016Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Page 17: Chapter 8 Object-Oriented Programming (OOP) Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Initialization lists There might be an initialization list following a constructor’s signature for member

variables that need to be initialized class Person {

private:string name;int age;

public:Person(const char* n): name(n) { ... }Person(const char* n, int a): name(n), age(a) { ...}// ...

};

Above constructors can be interpreted as Person(const char* n) {

name = n;// ...

}Person(const char* n, int a) {

name = n;age = a;// ...

}

Must be used for constant and reference member variables, and any member variable of class that does not have default constructor

17 EE3490E: Programming – T1 2015/2016Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Page 18: Chapter 8 Object-Oriented Programming (OOP) Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Destructors Destructor: method to clean objects before being removed from memory. It

doesn’t return value, and is called implicitly every time an object is destroyed. Do not use malloc()/free() to create or delete C++ objects At most one destructor for each class

class String {private:

char* str;public:

String() { str = NULL; }String(const char* s)

{ str = new char[strlen(s)+1]; strcpy(str, s); }~String() { if (str) delete str; }...

};String* abc() {

String s1, s2("Hello!"), s3[3];String *s4 = new String("xyz"), *s5 = new String[3];String *s6 = new String("abc");delete s4; // destroy 1 objectdelete[] s5; // destroy 3 objs, don’t use: delete s5return s6; // destroy s1, s2, s3[3] but s6 is still there

}18 EE3490E: Programming – T1 2015/2016Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Page 19: Chapter 8 Object-Oriented Programming (OOP) Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Separating declaration and definition of methodsstring.h string.cppclass String {private:char* str;

public:String();String(const char* s);~String();void set(const char* s);const char* get();

};

String::String()

{ str = NULL; }

String::String(const char* s)

{ str = NULL; set(s); }

String::~String()

{ if (str) delete str; }

void String::set(const char* s) {

if (str) delete str;

if (!s) { str = NULL; return; }

str = new char[strlen(s)+1];

strcpy(str, s);

}

const char* String::get()

{ return str; }

19 EE3490E: Programming – T1 2015/2016Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Page 20: Chapter 8 Object-Oriented Programming (OOP) Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Friend functions and classes If a function or class is defined as friend of some class, than it has access to

private member variables and methods of that class class Circle {

private:double r;

public:...friend void printCircle(Circle c);friend class Ellipse;

};

void printCircle(Circle c){ cout << "Radius: " << c.r; }

class Ellipse {private:

double rx, ry;public:

void convert(Circle c) {rx = ry = c.r;

}};

20 EE3490E: Programming – T1 2015/2016Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Page 21: Chapter 8 Object-Oriented Programming (OOP) Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

“this” pointer Is pointer accessible only within the non-static member functions of objects,

points to the object for which the member function being called class Buffer;void do_smth(Buffer* buf);

class Buffer {private:

char* b; int n;public:

Buffer(int n){ this->n = n; this->b = new char[n]; }

~Buffer(){ delete this->b; }

void some_method(){ do_smth(this); }

};

Buffer buf(4096);buf.some_method();

Texts in red are optional (implicit), in blue are mandatory21 EE3490E: Programming – T1 2015/2016

Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Page 22: Chapter 8 Object-Oriented Programming (OOP) Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Static variables and methods of classes Static member variables are ones that are unique for all objects of

same classes (even when no object exists) Static methods are ones that make access only to static member

variables

22 EE3490E: Programming – T1 2015/2016Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

class C {public:

static int count;C() { count++; }~C() { count--; }static int getCount()

{ return count; }...

};int C::count = 0;

C c1, c2, c3[10],*c4 = new C(),*c5 = new C[20];

delete c4;

cout << "Number of C objects: "<< C::getCount() << endl;

// cout << C::count << endl;// cout << c1.count << endl;// cout << c2.getCount() << endl;

It’s possible to access static members via class name and scope operator “::”, or via objects as normal members of that class

“this” pointer is not defined in static methods

Page 23: Chapter 8 Object-Oriented Programming (OOP) Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Constant methods In a method declared as constant, all member variables are constant.

Consequences: Impossible to assign or change member variables in constant methods Impossible to call non-constant methods from constant ones Within a constant method, only constant member methods of that object are

accessible One would rather declare all methods that does not change member variables as

constant

23 EE3490E: Programming – T1 2015/2016Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

class Circle {...void setR(double r) { this->r = r; }double getR() const { return r; }double area() const { return PI*r*r; }void cf(double r) const {

area(); // OKsetR(r); // errorthis->r = r; // error

}};

Circle c1;const Circle c2(2.333);

c1.setR(1.22); // OKc2.setR(1.22); // error

c1.area(); // OKc2.area(); // OK

cout << c1.getR() // OK<< c2.getR(); // OK

Page 24: Chapter 8 Object-Oriented Programming (OOP) Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology

Problems1. Write a class (String) to encapsulate string-of-characters type

char* with its typical methods2. Write a class (File) to encapsulate FILE* type with its typical

methods3. Write a class (LList) to manipulate linked lists on the basis of the

library written in C4. Write a class (Fraction) to manipulate fractions with its typical

methods5. Complete two class Circle and Ellipse with learnt notions in this

lesson: private variable, constructor, copy constructor, cast constructor, destructor, static variable to count number of instances, friend class, const method,…

6. Write two class Complex and Vector with necessary methods to work with complex numbers and 3D vectors

24 EE3490E: Programming – T1 2015/2016Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology