You are on page 1of 3

Function Overloading

• Multiple functions with the same name.


• Differentiated by arguments (number &
Intro to C++ part 2 type). eg:
double area (double radius);
Daniel Wigdor double area (double height, double width);
Some material from: …
Winston, P.H., “On to C++” cout << “Area of circle: “ << area(1.0) << “ Area of
square: “ << area(2.0,2.0) << endl;
ISBN: 0-201-58043-8

Get & Set Functions Can Get & Set Abstract Data
• You should not allow clients to access • Maintain abstract concepts
member variables directly.
class TankCar {
• Use get & set functions public: double radius, length;
double getRadius() { return radius; }
class TankCar { double getLength() { return length; }
public: double radius, length; void setRadius(double r) { radius = r; }
double getRadius() { return radius; } void setLength(double l) { length = l; }
double getLength() { return length; } double getDiameter(){ return 2.0 * radius; }
void setRadius(double r) { radius = r; } void setDiameter(double d){ radius = d / 2.0 }
void setLength(double l) { length = l; } };
};

Abstraction – Why? Protect Member Variables


• Can change implementation without • Can “hide” member variables to prevent
changing other code: client from changing them.
class TankCar {
class TankCar {
public:
public: double diameter, length; double getRadius() { return radius; }
double getRadius() { return radius*2.0; } double getLength() { return length; }
double getLength() { return length; } void setRadius(double r) { radius = r; }
void setRadius(double r) { diameter = r*2.0; } void setLength(double l) { length = l; }
void setLength(double l) { length = l; } double getDiameter(){ return 2.0 * radius; }
double getDiameter(){ return diameter; } void setDiameter(double d){ radius = d / 2.0 }
void setDiameter(double d){ diameter = d; } private:
}; double radius, length;
};

1
Client Can’t Modify “Privates” Inheritance
class TankCar {
public: • We have two railcars defined: BoxCar and
double getRadius() { return radius; } TankCar.
double getLength() { return length; }
void setRadius(double r) { radius = r; } • Adding a shared concept means adding it
void setLength(double l) { length = l; }
double getDiameter(){ return 2.0 * radius; }
to both classes
void setDiameter(double d){ radius = d / 2.0 } • eg: adding “percent full” to concept of
private:
double radius, length; railcar means adding a data member to
}; both classes.
main() {
TankCar t; • We move shared concepts to a “higher
t.radius = 8.0; // this line won’t compile level” class.
t.setRadius(8.0); // this is how it’s done

Inheritance: General -> Specific Inheritance Example


class RailCar {
public:
• We can design one class to “extend” int getPercentFull() { return pcf; }
another as follows: void setPercentFull(int full) { pcf = full; }
/* default constructor */
class parent { … }; // define the contents of the general class RailCar() { pcf = 17; }
class child1 : public parent { … }; //setup “child1” to extend “parent” private:
class child2 : public parent { … }; //setup “child2” to extend “parent” int pcf;
};
/* classes child1 & child2 both contain all members from parent */ class TankCar : public RailCar { /* same as previous slide */ };
class BoxCar : public RailCar { /* same as previous slide */ };
main() {
• “parent” is the “super class” of “child1” & TankCar t;
“child2”, which are “sub-classes”. cout << t.getPercentFull(); // what value is returned?
}
• Super class is more “general”, sub-class is
more specific.

Inheritance & Constructors Shadowing


• All “BoxCar” and “TankCar” objects • Member functions of super classes can be
contain everything from the RailCar class. “shadowed”
• Initializing a sub-class (BoxCar) calls two • Shadowing is when a sub-class has a
constructors: sub-class (BoxCar) and function with the same name as the super
super-class (RailCar). class.
• When calling a function, the lowest-level
function is run.
• Shadowing can be overridden.

2
Shadowing: Example When to Use Inheritance
class Bird {
public: void speak() { cout << “beep beep beep”; } • When they parallel natural categories
}; • Prevent avoidable duplication
class Duck : public Bird {
public: void speak() { cout << “quack quack”; } • Simplify maintenance
};
main() { • Avoid introducing bugs to debugged code
Bird b;
Duck d;
• Use purchased code
b.speak(); // beep
d.speak(); // quack
d.Bird::speak(); // beep
}

You might also like