Cse 2341 honors principles of computer science i
This presentation is the property of its rightful owner.
Sponsored Links
1 / 17

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


  • 44 Views
  • Uploaded on
  • Presentation posted in: General

CSE 2341 - Honors Principles of Computer Science I. Spring 2008 Mark Fontenot [email protected] Note Set 10. Base Class Pointers. Pointers to a base class object may be assigned the address of derived class objects

Download Presentation

CSE 2341 - Honors Principles of Computer Science I

An Image/Link below is provided (as is) to download presentation

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 honors principles of computer science i

CSE 2341 - HonorsPrinciples of Computer Science I

Spring 2008

Mark Fontenot

[email protected]

Note Set 10


Base class pointers

Base Class Pointers

  • Pointers to a base class object may be assigned the address of derived class objects

  • The base class pointer will ignore any overridden functions in the derived class


Polymorphism

Polymorphism

class Base

{

public:

void Show()

{ cout << "This is from the Base class.\n"; }

};

class Derived : public Base

{

public:

void Show()

{ cout << "This is from the Derived class.\n"; }

};

int main()

{

Base *Bptr;

Derived Dobject;

Bptr = &Dobject;

Bptr->Show(); // which Show function executes?

return 0;

}


Polymorphism1

Polymorphism

class Base

{

public:

virtual void Show()

{ cout << "This is from the Base class.\n"; }

};

class Derived : public Base

{

public:

virtual void Show()

{ cout << "This is from the Derived class.\n"; }

};

int main()

{

Base *Bptr;

Derived Dobject;

Bptr = &Dobject;

Bptr->Show(); // which Show function executes now?

return 0;

}


Overriding vs redefining

Overriding vs. Redefining

  • Redefining

    • When a derived class member function has the same name/signature as a base-class member function

  • Overriding

    • When a class redefines a virtual function, it is said that the class overrides the virtual function

  • In C++

    • Redefined member functions are statically bound

    • Overridden member functions are dynamically bound


Virtual destructors

Virtual Destructors

Remember to always have virtual destructors in inheritance hierarchy

Needed so a base-class pointer to derived-class object knows which destructor to call (or to start with)


Abstract base classes and pure virtual functions

Abstract Base Classes and Pure Virtual Functions

  • Abstract base class

    • cannot be instantiated

    • other classes are derived from it (and they can be instantiated)

    • contains at least one pure virtual function

    • Can still have pointers of abstract-base-class type

  • Pure Virtual Function

    • a member function that must be overridden in any derived class.

    • member function prototype ends with = 0; in the base class

      • virtual void talk() = 0;

    • Have no body or definition in the base class

  • syntax error to try and instantiate an object of an abstract base class


Student

Student

Student

Can’t be a student

without having a major.

So student cannot be

instantiated – it should be

abstract

CS Student

EE Student

CpE Student

A student must have a major of CS, CpE, or EE.


Back to the farm

Back to the Farm

CAnimal

Must be a particular type of animal.

Animals don’t talk, but all sub-classes do talk

CCat

CCow


Back to the farm1

Back To the Farm

class CAnimal

{

public:

virtual void talk ()=0;

};

Syntax Error

int main ()

{

CAnimal* arr[3];

arr[0] = new CAnimal();

arr[1] = new CCow();

arr[2] = new CCat();

arr[0]->talk();

arr[1]->talk();

arr[2]->talk();

return 0;

}

class CCow : public CAnimal

{

public:

virtual void talk ()

{

cout << "Moooooo." << endl;

CAnimal::talk();

}

};

class CCat : public CAnimal

{

public:

virtual void talk ()

{

cout << "Meowwww" << endl;

}

};


Classes derived from derived classes

Classes Derived from Derived Classes

A

B

C

A base class can also be derived from another class


Multiple inheritance

Multiple Inheritance

Class B

Class A

Class C

When one class inherits from more than one base class


Multiple inheritance1

Multiple Inheritance

class Date{

protected:

int month, day, year;

public:

Date(int m, int d, int y)

{

month = m; day = d; year = y;

}

int getDay() {return day;}

int getYear() {return year;}

int getMonth() {return month;}

};


Multiple inheritance2

Multiple Inheritance

class Time

{

protected:

int hour, minute;

public:

Time(int h, int m)

{ hour = h; minute = m; }

int getHour() {return hour;}

int getMinute() {return minute;}

};


Multiple inheritance3

Multiple Inheritance

class DateTime : public Date, public Time

{

protected:

char DTString[20];

public:

DateTime(int, int, int, int, int, int);

void GetDateTime(char *Str) { strcpy(Str, DTString); }

};


Multiple inheritance4

Multiple Inheritance

DateTime::DateTime(int Dy, int Mon, int Yr, int Hr, int Mt, int Sc) :

Date(Dy, Mon, Yr), Time(Hr, Mt, Sc)

{

char Temp[10]; // Temporary work area for itoa()

strcpy(DTString, itoa(GetMonth(), Temp, 10));

strcat(DTString, “/”);

strcat(DTString, itoa(GetDay(), Temp, 10));

strcat(DTString, “/”);

strcat(DTString, itoa(GetYear(), Temp, 10));

strcat(DTString, “ “);

strcpy(DTString, itoa(GetHour(), Temp, 10));

strcat(DTString, “:”);

strcat(DTString, itoa(GetMin(), Temp, 10));

strcat(DTString, “:”);

strcat(DTString, itoa(GetSec(), Temp, 10));

}


Multiple inheritance5

Multiple Inheritance

#include <iostream>

#include “datetime.h”

int main()

{

char Formatted[20];

DateTime PastDay(4, 2, 60, 5, 32, 27);

PastDay.GetDateTime(Formatted);

cout << Formatted << endl;

return 0;

}


  • Login