29
Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath [email protected]

Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath [email protected]

Embed Size (px)

Citation preview

Page 1: Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath yharrath@uob.edu.bh

Chapter 2

Object-Oriented Design and C++

Dr. Youssef Harrath

[email protected]

Page 2: Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath yharrath@uob.edu.bh

Outline

1. Inheritance

2. Composition

3. Polymorphism

• Function Overloading

• Templates

2Dr. Youssef Harrath – University of Bahrain – 2010/2011

Page 3: Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath yharrath@uob.edu.bh

Inheritance Inheritance is an Object-Oriented Design concept

which relates many classes together to allow specific classes using members of general classes.

It’s a kind of a hierarchy relation between different classes of different levels.

The general syntax to define a derived class is:

Where memberAccessSpecifier is public, protected, or private (by default).

class className: memberAccessSpecifier baseClassName

{

member list

};

3Dr. Youssef Harrath – University of Bahrain – 2010/2011

Page 4: Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath yharrath@uob.edu.bh

Inheritance

private members of the base can not be directly

accessed by members of the derived class.

public members of a base class can be inherited either

as public or private members by the derived class.

The derived class can include additional data/functions

The derived class can redefine the public functions of

the base class.

All the data/function members of the base class are

members of the derived class (except the redefined

functions).

4Dr. Youssef Harrath – University of Bahrain – 2010/2011

Page 5: Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath yharrath@uob.edu.bh

Inheritance: Example

shape

circle rectangle

square

“is a”

“is a”

“is a”

5Dr. Youssef Harrath – University of Bahrain – 2010/2011

Page 6: Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath yharrath@uob.edu.bh

Inheritance: Example

Suppose that we have the class shape:

class circle: public shape

{ // public inheritance

.

.

.

};

class circle: private shape //by default

{ // private inheritance

.

.

.

};

// the public members of shape become private members of circle (any object of type circle can not directly access these members)

6Dr. Youssef Harrath – University of Bahrain – 2010/2011

Page 7: Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath yharrath@uob.edu.bh

Inheritance: Overriding

Public functions of the base class can be redefined

(overrided) in a derived class (subclass).

The redefined function in the subclass must have the

same name, number, and types of parameters (formal

parameter list).

If both functions in the base class and in the derived

class have the same name but different formal

parameter list, this is called overloading (allowed).

7Dr. Youssef Harrath – University of Bahrain – 2010/2011

Page 8: Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath yharrath@uob.edu.bh

Inheritance: Overriding

class baseClass{public:

void print () ;private:

int u, v; // not accessible directly in the derived classchar ch;

};

void baseClass::print() {

cout <<"Base Class: u = "<<u <<", v = "<<v <<" ch = "<<ch<<endl;

}

8Dr. Youssef Harrath – University of Bahrain – 2010/2011

Page 9: Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath yharrath@uob.edu.bh

Inheritance: Overriding

9

class derivedClass: public baseClass{public:

void print () ;private:

int first;double second;

};

void derivedClass::print(){

baseClass::print(); //call of the public print function of the // base class to print the private memberscout <<"Derived Class: first = "<<first

<<", second = "<<second<<endl;}

Dr. Youssef Harrath – University of Bahrain – 2010/2011

Page 10: Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath yharrath@uob.edu.bh

Inheritance: Constructors

10

class baseClass

{

public:

void print ();

baseClass(); // constructor 1

baseClass(int x, int y); // constructor 2

baseClass(int x, int y, char w); // constructor 3

private:

int u, v; // not accessible directly in the derived class

char ch;

};

Dr. Youssef Harrath – University of Bahrain – 2010/2011

Page 11: Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath yharrath@uob.edu.bh

Inheritance: Constructors

11

baseClass

-u: int-v: int-ch: char

+print(): void+baseClass()+baseClass(int, int)+baseClass(int, int, char)

UML diagram of the class baseClass

Dr. Youssef Harrath – University of Bahrain – 2010/2011

Page 12: Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath yharrath@uob.edu.bh

Inheritance: Constructors

12

void baseClass::print() { // see slide 8 }baseClass:: baseClass() // constructor 1{

u = 0;v = 0;ch = ‘*’;

}baseClass:: baseClass(int x, int y) // constructor 2{

u = x;v = y;ch = ‘*’;

}baseClass:: baseClass(int x, int y, char w) // constructor 3{

u = x;v = y;ch = w;

}

Dr. Youssef Harrath – University of Bahrain – 2010/2011

Page 13: Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath yharrath@uob.edu.bh

Inheritance: Constructors

13

class derivedClass: public baseClass

{

public:

void print ();

derivedClass(); // constructor 1

derivedClass(int x, int y, int one, double two); // constructor 2

derivedClass(int x, int y, char w, int one, double two); // constructor 3

private:

int first ;

double second;

};

Dr. Youssef Harrath – University of Bahrain – 2010/2011

Page 14: Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath yharrath@uob.edu.bh

Inheritance: Constructors

14

derivedClass

-first: int-second: double

+print(): void+derivedClass()+derivedClass(int, int, int, double)+derivedClass(int, int, char, int, double)

UML diagram of the class derivedClass and inheritance hierarchy

Dr. Youssef Harrath – University of Bahrain – 2010/2011

baseClass

derivedClass

Page 15: Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath yharrath@uob.edu.bh

Inheritance: Constructors

Dr. Youssef Harrath – University of Bahrain – 2010/2011 15

void derivedClass::print() { // see slide 9 }derivedClass::derivedClass() // default constructor{ // default constructor of the base class will be invoked

first = 0;second = 0;

}derivedClass::derivedClass(int x, int y, int one, double two) :baseClass(x, y) {

first = one;second = two;

}derivedClass::derivedClass(int x, int y, char w, int one, double two) :baseClass(x, y, w) {

first = one;second = two;

}

Page 16: Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath yharrath@uob.edu.bh

Inheritance: Header files

A C++ programming practice is to create header files

(.h extension) for the classes

Suppose that baseClass is placed in the header file

baseClass.h, the definition of the class derivedClass in

a new file must start with #include “baseClass.h”

16Dr. Youssef Harrath – University of Bahrain – 2010/2011

Page 17: Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath yharrath@uob.edu.bh

Dr. Youssef Harrath – University of Bahrain – 2010/2011 17

Composition

Composition is another way to relate two classes.

Composition is used for objects that have a “has-a”

relationship to each other.

In order to facilitate the building of complex classes

from simpler ones, C++ allows us to do object

composition in a very simple way — by using classes

as member variables in other classes.

Page 18: Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath yharrath@uob.edu.bh

Dr. Youssef Harrath – University of Bahrain – 2010/2011 18

class personType

{

public:

void print();

void setName(string first, string last);

void getName(string& first, string& last);

personType(string first, string last);

private:

string firstName;

string lastName;

};

Composition

Page 19: Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath yharrath@uob.edu.bh

Dr. Youssef Harrath – University of Bahrain – 2010/2011 19

Composition

class dateType

{

public:

void printDate();

void setDate(int month, int day, int year);

void getDate(int& month, int& day, int& year);

dateType(int month, int day, int year);

private:

int dMonth;

int dDay;

int dYear;

};

Page 20: Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath yharrath@uob.edu.bh

Dr. Youssef Harrath – University of Bahrain – 2010/2011 20

Composition

class personalInfoType

{

public:

void printpersonalInfo();

void setpersonalInfo(string first, string last, int month, int day, int year, int ID);

personalInfoType(string first, string last, int month, int day, int year, int ID);

private:

personType name;

dateType bDate;

int personID;

};

Page 21: Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath yharrath@uob.edu.bh

Dr. Youssef Harrath – University of Bahrain – 2010/2011 21

Composition

void personalInfoType::printpersonalInfo()

{

name.print();

cout<<“ ‘s date of birth is “;

bDay.printDate();

cout<<endl;

cout<<“and personal ID is “<<personID;

}

Page 22: Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath yharrath@uob.edu.bh

Dr. Youssef Harrath – University of Bahrain – 2010/2011 22

Composition

void personalInfoType::setpersonalInfo(string first, string last, int month, int day, int year, int

ID)

{

name.setName(first, last);

bDay.setDate(month, day, year);

personID = ID;

}

Page 23: Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath yharrath@uob.edu.bh

Dr. Youssef Harrath – University of Bahrain – 2010/2011 23

Composition

personalInfoType::personalInfoType(string first, string last, int month, int day,

int year, int ID)

:name(first, last), bDay(month, day, year)

{

personID = ID;

}

Page 24: Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath yharrath@uob.edu.bh

Dr. Youssef Harrath – University of Bahrain – 2010/2011 24

Polymorphism

Polymorphism is one of the principles of Object-Oriented

Design (OOD).

Polymorphism will be discussed via overloading and then

via templates.

Overloading is a OOD concept to allow the programmer

defining operators/functions with same name but different

formal parameter list.

Templates enable the programmer to write generic codes

for related function and classes.

Page 25: Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath yharrath@uob.edu.bh

Dr. Youssef Harrath – University of Bahrain – 2010/2011 25

Polymorphism: Function Overloading

int largerInt(int x, int y);

char largerChar(char first, char second);

double largerDouble(double u, double v);

string largerString(string first, string second);

int larger(int x, int y);

char larger(char first, char second);

double larger(double u, double v);

string larger(string first, string second);

Page 26: Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath yharrath@uob.edu.bh

Dr. Youssef Harrath – University of Bahrain – 2010/2011 26

Polymorphism: Templates

Templates are very powerful features in C++.

Templates enable the user writing a single code segment

for a set of related functions: function template, and for

related classes: class template.

The syntax for function templates is: template<class Type>

function definition;

The syntax for class templates is: template<class Type>

class declaration

Page 27: Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath yharrath@uob.edu.bh

Dr. Youssef Harrath – University of Bahrain – 2010/2011 27

Polymorphism: Function Templates

template<class Type> // Part 1

Type larger(Type x, Type y);

void main()

{

cout<<“Line 1: Larger of 5 and 6 = “<<larger(5, 6)<<endl;

cout<<“Line 2: Larger of A and B = “<<larger(‘A’, ‘B’)<<endl;

cout<<“Line 3: Larger of 5.6 and 3.2 = “<<larger(5.6, 3.2)<<endl;

string s1 = “Hello”, s2 = “Happy”;

cout<<“Line 4: Larger of “<<s1<<“ and “<<s2<<“ = “<<larger(s1,s2)<<endl;

}

Page 28: Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath yharrath@uob.edu.bh

Dr. Youssef Harrath – University of Bahrain – 2010/2011 28

Polymorphism: Function Templates

template<class Type> // Part 2

Type larger(Type x, Type y)

{

if( x >= y)

return x;

else

return y;

}

Output

Line 1: Larger of 5 and 6 = 6

Line 2: Larger of A and B = B

Line 3: Larger of 5.6 and 3.2 = 5.6

Line 4: Larger of Hello and Happy = Hello

Page 29: Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath yharrath@uob.edu.bh

Dr. Youssef Harrath – University of Bahrain – 2010/2011 29

Polymorphism: Class Templates

template<class elemType>

class listType

{

public:

bool isEmpty();

bool isFull();

void search(const elemType& searchItem, bool& found);

void insert(const elemType& newElement);

private:

elemType list[100];

int length;

};

listType<int> intList; // declares intList to be a list of 100 integers

listType<string> stringList; // declares stringList to be a list of 100 strings