Class inheritance

Last Edited By Krjb Donovan
Last Updated: Mar 11, 2014 07:51 PM GMT

Question

Hi Zlatko

I have 2 classes, Rectangle & Block. I am trying to make than communicate. However there are a few errors during program compile.


  1. include <iostream>

using namespace std;

//class Rectangle class Rectangle { private: int length; int width; public: int getData(int,int); void showRectangle(); int computeArea(); };


int Rectangle::getData(int len, int wid) { length = len; width = wid; }

void Rectangle::showRectangle() { cout <<"Dimensions are " << length << "by " << width << endl;

}

int Rectangle::computeArea() { int area = length * width; cout << "Area is " << area << endl;

}

//class Block class Block : public Rectangle { private: int height; public: int getData(int,int); void showBlock(); int computeArea(); };

int Block::getData(int len, int wid, int ht) { Rectangle::getData(len, wid); ht = height; }

void Block::showBlock() { cout << "Height " << height << endl; showRectangle(); cout << endl; }

int Block::computeArea() { int area = length * width * height; cout << "Volume is " << area << endl; }

//main function int main() { Rectangle square; square.getData(4,5); cout << endl << "Rectangle..." << endl; square.showRectangle; square.computeArea();

Block cube; cube.getData(2, 4, 6); cout << "Block..." << endl; cube.showBlock(); cube.computeArea(); }


Answer

Hello Steve.

Below is the corrected program. I am not including a text explanation of what I did. I am trying something new. You can watch a video of the program being corrected and hear my explanations by going to this site:

http://www.divshare.com/download/13343006-54b

I recommend you watch in full screen mode.

Please do let me know if the video is watchable, or if it needs too much bandwidth.

  1. include <iostream>

using namespace std;

//class Rectangle class Rectangle { protected:

   int length;
   int width;

public:

   Rectangle();
   void setData(int,int);
   void showRectangle();
   int computeArea();

};

Rectangle::Rectangle() {

   length = 0;
   width = 0;

}

void Rectangle::setData(int len, int wid) {

   length = len;
   width = wid;

}

void Rectangle::showRectangle() {

   cout <<"Dimensions are " << length << "by "
       << width << endl;

}

int Rectangle::computeArea() {

   int area = length * width;
   cout << "Area is " << area << endl;
   return area;

}

//class Block class Block : public Rectangle { private:

   int height;

public:

   Block();
   void setData(int len, int vid, int ht);
   void showBlock();
   int computeVolume();

};

Block::Block() {

   height = 0;

}

void Block::setData(int len, int wid, int ht) {

   Rectangle::setData(len, wid);
   height = ht;

}

void Block::showBlock() {

   cout << "Height " << height << endl;
   showRectangle();
   cout << endl;

}

int Block::computeVolume() {

   int volume = length * width * height;
   cout << "Volume is " << volume << endl;
   return volume;

}

//main function int main() {

   Rectangle square;
   square.setData(4,5);
   cout << endl << "Rectangle..." << endl;
   square.showRectangle();
   square.computeArea();
   Block cube;
   cube.setData(2, 4, 6);
   cout << "Block..." << endl;
   cube.showBlock();
   cube.computeArea();
   cube.computeVolume();

}

Advertisement

©2024 eLuminary LLC. All rights reserved.