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

An example of c++ inheritance using the classes sphere and ball. It includes the definition of constructors, destructors, overloaded operators, and virtual functions. The document also discusses the rule of the big three and the concept of subclass substitution.

Typology: Study notes

Pre 2010

Uploaded on 03/16/2009

koofers-user-c58
koofers-user-c58 🇺🇸

10 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Announcements:
MP2 due 09/16/07, 11:59p.
Sections are moved from Siebel 1214 to DCL L440
Reading: up to now, all of Chapter 1
Chapter 2.2
Today:
C++!!
Rule of the BIG THREE
Inheritance
pf3
pf4
pf5
pf8
pf9
pfa
pfd

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;