20-Inheritance - #include <iostream> using namespace...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <iostream> using namespace std; // Base Class class Shape { protected: float area; float length; float width; public: Shape(float length = 0.0f, float width = 0.0f) { setLength(length); setWidth(width); } void setLength(float length) { this->length = length > 0 ? length : 0; } void setWidth(float width) { this->width = width > 0 ? width : 0; } void print() { cout << "Length: " << length << " Width: " << width << "\n"; } void calcArea(); }; // Derived Class // The derived class is granted access to all of // the public and protected members of the base // class, as if we had defined them in the derived // class itself. Private members of the base // class remain private class Quadralateral : public Shape { private: void calcArea() { // area, length and width are defined in base class area = length * width; this->area = this->length * this->width; Shape::area = Shape::length * Shape::width; } public: Quadralateral(float length = 0.0f, float width = 0.0f) : Shape(length, width) { calcArea(); } void print() {
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/05/2011 for the course CS 37 taught by Professor Glenncuevas during the Summer '09 term at Irvine Valley College.

Page1 / 3

20-Inheritance - #include &lt;iostream&gt; using namespace...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online