1 / 16

# CSE 2341 - Honors Principles of Computer Science I - PowerPoint PPT Presentation

CSE 2341 - Honors Principles of Computer Science I. Spring 2008 Mark Fontenot [email protected] Note Set 8. Quick Look. Class Diagrams More inheritance. Class Diagrams. Each student is in 0 or 1 class( es ). One or more students in a ClassSection.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about ' CSE 2341 - Honors Principles of Computer Science I' - haven

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### CSE 2341 - HonorsPrinciples of Computer Science I

Spring 2008

Mark Fontenot

Note Set 8

Class Diagrams

More inheritance

Each student is in 0 or 1 class(es)

One or more students in a ClassSection

Filled in Diamond: Represents composition

Generalization: Person is a

general form of Student and

Professor.

Student is person

Professor is person

No real idea of multiplicity in generalization

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

• Need a CurvedActivity class

• multiplies the raw score by a curve value before setting the score

{

private:

char Letter;

float Score;

public:

void SetScore(float S)

{

Score = S;

}

float GetScore() { return Score; }

char GetLetter() { return Letter; }

};

#endif

This function will

be overridden in the

derived class

{

if (Score > 89)

Letter = 'A';

else if (Score > 79)

Letter = 'B';

else if (Score > 69)

Letter = 'C';

else if (Score > 59)

Letter = 'D';

else

Letter = 'F';

}

#ifndef CURVEDACTIVITY_H

#define CURVEDACTIVITY_H

protected:

float rawScore; float percentage;public:

// overridden in derived class

void setScore (float s) {

rawScore = s; // base class setScore method

rawScore * percentage);

}

CurvedActivity.h

Calls the base class member function

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

float getPercentage()

{ return percentage; }

float getRawScore()

{ return rawScore; }

};

CurvedActivity.h

// 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;

}

• If a derived class overrides a base class member function

• objects of the base class call the base class version of the member function

• objects of the derived class call the derived class version of the member function

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.

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.

classBase

{

public:

doCalc(){//something;}

calc()

{ doCalc(); }

};

classDerived:public Base

{

public:

doCalc()

{//something different;}

};

int main()

{

Derived d;

d.calc();

return 0

}

Which doCalc()

gets called?

• Static Binding

• function call is bound to function implementation at compile time

• Dynamic Binding

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

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

• Compiler performs dynamic binding on virtual functions

• Declared 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