Docsity

Object-Oriented Programming in C++: Lecture Notes for ECE 575 at University of Arizona, Papers of Electrical and Electronics Engineering

Partial preview of the text

Download Object-Oriented Programming in C++: Lecture Notes for ECE 575 at University of Arizona and more Papers Electrical and Electronics Engineering in PDF only on Docsity!

1

ECE 575/ Chapter 2.Object Oriented Modeling andProgramming (in C++)

Moon Ho Hwang [email protected]

2006 Fall ECE Department,University of Arizona

Part1: System Modeling

© 2006 Moon Ho Hwang

2

Categorization ofProgram Languages^ † ECE 575, Dept. of ECE,

Structured Programming Language^ „^

FORTURAN, COBOL, „^

C, Pascal †^

Object-Oriented ProgrammingLanguage „^

C++ „^

Java, C# „^

Python

Modeling OOM/P

© 2006 Moon Ho Hwang

4

Object Class ECE 575, Dept. of ECE,

Modeling OOM/P

†^
Ex: Class Student „^
Data: †^
Name
†^
Student_ID
„^
Function: †^
Do_Take_Class()

class Student{

string Name;string ST_id;void Do_Take_Class() const{ cout << “take a class.”<< endl;} }; Figure1. C++ code for Student class.

© 2006 Moon Ho Hwang

5

Object Class ECE 575, Dept. of ECE,

Modeling OOM/P

†^
Ex: Class Professor „^
Data: †^
Name
†^
Employee_ID
„^
Function: †^
Do_Teach_Class()

class Professor{

string Name;string EP_id;void Do_Teach_Class() const{ cout << “teach a class.”<< endl;} }; Figure2. C++ code for Professor class.

© 2006 Moon Ho Hwang

7

Base Class ECE 575, Dept. of ECE,

Modeling OOM/P

†^

If we make a class Person as a

base

class

of Student and Professor, it can

inherit

its common features to them.

†^

Common features: „^

Data: string Name;

„^

Function: Do();

© 2006 Moon Ho Hwang

8

Inheritance ECE 575, Dept. of ECE,

Modeling OOM/P

†^

A base class

inherits

its feature to the derived

class.

Person String Name;void Do();

Student String ST_ID;void Take_Class();void Do();

Professor String EP_ID;void Teach_Class();void Do();

Base/parentclass

Derived/childclass

© 2006 Moon Ho Hwang

10

Making Abstract/Concreteclasses ECE 575, Dept. of ECE,

Modeling OOM/P

†^

Member Encapsulation:^ public, protect, private †^

Using encapsulation of

Constructor

„^

Non-public constructor => Abstract class

„^

Public constructor => Concrete class

class Person{ protected

: string Name;Person(string name): Name(name){}};

class Student: public Person{ public

: Student(string name):Person(name){} };

© 2006 Moon Ho Hwang

11

Making an Abstract/Concreteclass (cont.) ECE 575, Dept. of ECE,

Modeling OOM/P

†^

Using

pure virtual

functions

„^

If a class having non-implemented purevirtual function => Abstract class

„^

If not => Can be a Concrete class^ class Person{ … public:

virtual void Do() = 0; };

© 2006 Moon Ho Hwang

13

Destructor (in C++) ECE 575, Dept. of ECE,

Modeling OOM/P

†^

The destructor of a class is the function deletingan instance of the class.

†^

All destructor functions start with “~” and havethe same name as the class name. „^

For class Person, ~Person ();

†^

No arguments allowed, so only one destructor canbe defined for each class.

†^

If we don’t need a special function of destructor,we don’t have to define the destructor of a class.

†^

Usually, destructor is defined as a

virtual

function.

(Why?)

© 2006 Moon Ho Hwang

14

virtual vs non-virtual ECE 575, Dept. of ECE,

Modeling OOM/P

†^

A virtual function is a function whichperforms different ways according to its specific instance

class Student: public Person{…/virtual/ void Do(){ cout << Name << " takes ECE575." << endl;} };

class Professor: public Person{ public:/virtual/ void Do(){ cout << Name << " teachs ECE575." << endl;} };

© 2006 Moon Ho Hwang

16

virtual vs non-virtual (con.)ECE 575, Dept. of ECE,

Modeling OOM/P

†^

A function with its body of a derived classis called a

overriding

function.

„^

void Student::Do() { /* … */ } †^

Notice that

overloading

functions are a

set of functions whose names areidentical,

with different arguments.

„^

void Student::Do(int n){ /* … */ }

© 2006 Moon Ho Hwang

17

virtual vs non-virtual (con.)ECE 575, Dept. of ECE,

Modeling OOM/P

†^

In Java, all functions are virtual. †^

In C++ and C#, only functions having amodifier

virtual

are virtual.

†^

If we define

all

functions as

virtual

(or

overriding

) functions, what can be

advantage or disadvantage? †^

Advantage: Easy to use †^

Disadvantage: Slower and bigger memorystorage †^

See

VirtualFuncs

Example.

© 2006 Moon Ho Hwang

19

Static vs Instance ECE 575, Dept. of ECE,

Modeling OOM/P

†^

Since a static function is independentfrom a class instance, it cannot be avirtual function which is related to theinstance.

class Person{ public:

virtual static Do(); };

© 2006 Moon Ho Hwang

20

constant function (in C++)ECE 575, Dept. of ECE,

Modeling OOM/P

†^

A constant function is a function in whichdata of an instance cannot change. Wecan access data for

reading

but not

writing

.

class Person{ string Name;string get_name() const { return Name; }void set_name(string n) const {Name

= n; }//<-wrong

}