Docsity

C++ Inheritance and Classes: Sphere and Ball - Prof. Cinda Heeren, Study notes of Data Structures and Algorithms

Partial preview of the text

Download C++ Inheritance and Classes: Sphere and Ball - Prof. Cinda Heeren and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Announcements:

MP2 due 09/16/07, 11:59p.

Sections are moved from Siebel 1214 to DCL L

Reading: up to now, all of Chapter 1

Chapter 2.

Today:

C++!!

Rule of the BIG THREE

Inheritance

Operator=:

class sphere{

public:

sphere();

sphere(double r);

sphere(const sphere & orig);

~sphere();

private:

double theRadius;

int numAtts;

string * attributes;

// overloaded =

sphere & sphere::operator=(const sphere & rhs){

if (this != &rhs){

delete [] attributes;

theRadius = rhs.theRadius;

numAtts = rhs.numAtts;

attributes = new string[numAtts];

for (int I=0;I<numAtts;I++){

attributes[I]=rhs.attributes[I];

return *this;

int main(){

sphere a, b;

// initialize a

b = a;

return 0;

Inheritance:

timepiece

analog digital

clock sundial watch watch clock

alarm cuckoo grandfather pocket wrist alarm

Inheritance:

class sphere {

public:

sphere();

sphere(double r);

double getVolume();

void setRadius(double r);

void display();

private:

double theRadius;

class ball:public sphere {

public:

ball();

ball(double r string n);

string getName();

void setName(string n);

void display();

private:

string name;

sphere

ball:public sphere

vB:protected ball

beachVB:private vB

daygloVB:protected beachVB

An example:

public protected private

Subclass substitution (via example):

sphere s(8.0);

ball b(3.2, “pompom”);

double a = b.getVolume();

Void printVolume(sphere t){

cout << t.getVolume() << endl;}

printVolume(s);

printVolume(b);

class sphere { public: sphere(); sphere(double r); … double getVolume(); void setRadius(double r); … void display(); private: double theRadius; }; class ball:public sphere { public: ball(); ball(double r string n); … string getName(); void setName(string n); … void display(); private: string name; };

something to consider:

void sphere::display() {

cout << “sphere” << endl;

sphere s;

ball b;

s.display(); 

b.display(); 

void ball::display() {

cout << “ball” << endl;

sphere * sptr;

sptr = &s;

sptr->display(); sphere^ *^ sptr;

sptr = &b;

sptr->display();

class sphere { public: sphere(); sphere(double r); … double getVolume(); void setRadius(double r); … void display(); private: double theRadius; }; class ball:public sphere { public: ball(); ball(double r string n); … string getName(); void setName(string n); … void display(); private: string name; };

“virtual” functions:

void sphere::display() {

cout << “sphere” << endl;

void ball::display() {

cout << “ball” << endl;

sphere * sptr;

sptr = &s;

sptr->display(); sphere *^ sptr;

sptr = &b;

sptr->display();

if (a==0)

sptr = &s;

else sptr = &b;

sptr->display();

class flower { public: flower(); virtual void drawBlossom() = 0; virtual void drawStem() = 0; virtual void drawFoliage() = 0; … }; class daisy:public flower { public: virtual void drawBlossom(); virtual void drawStem(); virtual void drawFoliage(); … private: int blossom; // number of petals int stem; // length of stem int foliage // leaves per inch };

Abstract Base Class:

void daisy::drawBlossom() {

// whatever

void daisy::drawStem() {

// whatever

void daisy::drawFoliage() {

// whatever

flower f;

daisy d;

flower * fptr;