spring 2008 mark fontenot cse 2341 - honors principles of computer science i note set 8 1

16
Spring 2008 Mark Fontenot [email protected] CSE 2341 - Honors Principles of Computer Science I Note Set 8 1

Upload: derek-cox

Post on 20-Jan-2018

212 views

Category:

Documents


0 download

DESCRIPTION

Class Diagrams 3 Filled in Diamond: Represents composition One or more students in a ClassSection Each student is in 0 or 1 class(es)

TRANSCRIPT

Page 1: Spring 2008 Mark Fontenot CSE 2341 - Honors Principles of Computer Science I Note Set 8 1

Spring 2008

Mark [email protected]

CSE 2341 - HonorsPrinciples of Computer Science I

Note Set 81

Page 2: Spring 2008 Mark Fontenot CSE 2341 - Honors Principles of Computer Science I Note Set 8 1

Quick Look

2

Class DiagramsMore inheritance

Page 3: Spring 2008 Mark Fontenot CSE 2341 - Honors Principles of Computer Science I Note Set 8 1

Class Diagrams

3

Filled in Diamond: Represents composition

One or more students in a ClassSection

Each student is in 0 or 1 class(es)

Page 4: Spring 2008 Mark Fontenot CSE 2341 - Honors Principles of Computer Science I Note Set 8 1

Class Diagrams - Inheritance

4

Generalization: Person is a general form of Student and Professor.

Student is personProfessor is person

No real idea of multiplicity in generalization

Page 5: Spring 2008 Mark Fontenot CSE 2341 - Honors Principles of Computer Science I Note Set 8 1

Overriding Member Functions

5

A member function of a derived class may have the same name and signature of a function in the base class

GradedActivity ClassNeed a CurvedActivity classmultiplies the raw score by a curve

value before setting the score

Page 6: Spring 2008 Mark Fontenot CSE 2341 - Honors Principles of Computer Science I Note Set 8 1

GradedActivity.h

6

#ifndef GRADEDACTIVITY_H#define GRADEDACTIVITY_H// Grade class declarationclass GradedActivity{ private: char Letter; float Score; void determineGrade();public: void SetScore(float S) { Score = S; determineGrade(); } float GetScore() { return Score; } char GetLetter() { return Letter; }}; #endif

Gra

dedA

ctiv

ity.

h

This function willbe overridden in the

derived classCurvedGrade

Page 7: Spring 2008 Mark Fontenot CSE 2341 - Honors Principles of Computer Science I Note Set 8 1

GradedActivity.h

7

#include “GradedActivity.h" void GradedActivity::determineGrade(void){

if (Score > 89)Letter = 'A';

else if (Score > 79)Letter = 'B';

else if (Score > 69)Letter = 'C';

else if (Score > 59)Letter = 'D';

elseLetter = 'F';

}

Gra

dedA

ctiv

ity.

cpp

Page 8: Spring 2008 Mark Fontenot CSE 2341 - Honors Principles of Computer Science I Note Set 8 1

CurvedActivity.h

8

#ifndef CURVEDACTIVITY_H#define CURVEDACTIVITY_H#include “Grade.h”class CurvedActivity: public GradedActivity{ protected: float rawScore; float percentage;public: // overridden in derived class void setScore (float s) {

rawScore = s; // base class setScore method GradedActivity::setScore(

rawScore * percentage); }

Curv

edAc

tivi

ty.h

Calls the base class member function

Page 9: Spring 2008 Mark Fontenot CSE 2341 - Honors Principles of Computer Science I Note Set 8 1

CurvedActivity.h

9

void setPercentage (float c) { percentage = c; }

float getPercentage() { return percentage; }

float getRawScore() { return rawScore; }

};

Curv

edAc

tivi

ty.h

Page 10: Spring 2008 Mark Fontenot CSE 2341 - Honors Principles of Computer Science I Note Set 8 1

gradeDriver.cpp

10

// main driver#include <iostream>#include “CurvedActivity.h”using namespace std;int main(){ CurvedActivity exam; float numericScore, percentage; cout << “Enter the student’s raw numeric score: “; cin >> numericScore; cout << “enter the curve percentage for this student:”; cin >> percentage; exam.setPercentage (percentage); exam.setScore(numericScore); cout << exam.getRawScore() << exam.getScore() << exam.getLetter(); return 0; }

grad

eDri

ver.c

pp

Page 11: Spring 2008 Mark Fontenot CSE 2341 - Honors Principles of Computer Science I Note Set 8 1

Overriding Functions

11

If a derived class overrides a base class member function objects of the base class call the base class version of the

member functionobjects of the derived class call the derived class version of

the member function

Page 12: Spring 2008 Mark Fontenot CSE 2341 - Honors Principles of Computer Science I Note Set 8 1

Overriding Functions

12

class Base{ public: void ShowMsg() { cout << "This is the Base class.\n"; }};

class Derived : public Base{public: void ShowMsg() { cout << "This is the Derived class.\n"; }};int main(void){ Base B; Derived D; B.ShowMsg(); // ShowMsg of base class called D.ShowMsg(); // ShowMsg of derived class called}

This is the Base class.This is the Derived class.

Page 13: Spring 2008 Mark Fontenot CSE 2341 - Honors Principles of Computer Science I Note Set 8 1

Polymorphism

13

Polymorphism - the ability to take many forms Occurs when member functions in a class

hierarchy behave differently depending on which object performed the function call

Redefinition of a function does not create polymorphism.

Page 14: Spring 2008 Mark Fontenot CSE 2341 - Honors Principles of Computer Science I Note Set 8 1

Polymorphism

14

class Base{public: doCalc(){//something;} calc() { doCalc(); }};

class Derived:public Base{public: doCalc() {//something different;}};

int main(){ Derived d; d.calc(); return 0}

Which doCalc()gets called?

Page 15: Spring 2008 Mark Fontenot CSE 2341 - Honors Principles of Computer Science I Note Set 8 1

Binding Times

15

Static Bindingfunction call is bound to function implementation at compile

timeDynamic Binding

function call is bound to a function implementation at run-time depending on the type of the object responsible for the call

Page 16: Spring 2008 Mark Fontenot CSE 2341 - Honors Principles of Computer Science I Note Set 8 1

Virtual Function

16

A virtual function is a function that expects to be redefined in a derived class.

Compiler performs dynamic binding on virtual functionsDeclared by placing virtual before the return type in

the base class’s function declaration virtual void myFunction();only placed in prototype or header if defined in class