Not sure why the output wont calculate? // ConsoleApplication20.cpp : main proje
ID: 3571943 • Letter: N
Question
Not sure why the output wont calculate?
// ConsoleApplication20.cpp : main project file.
#include "stdafx.h"
using namespace System;
#include<iostream>
#include<conio.h>
using namespace std;
class Rectangle
{
private:
int length;
int width;
public:
Rectangle(int l=0, int w=0);
void setLength(int l);
void setWidth(int w);
int getLength();
int getWidth();
void output();
int getarea();
};
Rectangle::Rectangle(int l, int w)
{
length=l;
width=w;
}
void Rectangle::setLength(int l)
{
length=l;
}
void Rectangle::setWidth(int w)
{
width=w;
}
int Rectangle::getLength()
{
return length;
}
int Rectangle::getWidth()
{
return width;
}
void Rectangle::output()
{
cout << "Length: " << length << endl;
cout << "Width: " << width << endl;
}
int Rectangle ::getarea()
{
return length*width;
}
class Block :public Rectangle
{
private:
int height;
public:
Block(int l=0, int w=0, int h=0);
void setHeigth(int h);
int getHeigth();
void output();
int getarea();
};
Block::Block(int l, int w, int h) :Rectangle(l,w)
{
height=h;
}
void Block::setHeigth(int h)
{
height=h;
}
int Block::getHeigth()
{
return height;
}
int Block ::getarea()
{
return Rectangle::getarea()*height;
}
int main()
{
Rectangle rectangle;
rectangle.setLength(3);
rectangle.setWidth(4);
cout << "Rectangle Area" << endl;
cout << "Area: " << endl;
Block block;
block.setLength(3);
block.setWidth(4);
block.setHeigth(5);
cout << "Block area" << endl;
cout << "Area: " << endl;
_getch();
return 0;
}
Explanation / Answer
//#include "stdafx.h" commenting this as i dont have this file. remove it if you want
//using namespace System; commenting this as this is not a valid namespace. remove it if you want
#include<iostream>
#include<conio.h>// this file is not there, please remove it if you want
using namespace std;
class Rectangle
{
private:
int length;
int width;
public:
Rectangle(int l=0, int w=0);
void setLength(int l);
void setWidth(int w);
int getLength();
int getWidth();
void output();
int getarea();
};
Rectangle::Rectangle(int l, int w)
{
length=l;
width=w;
}
void Rectangle::setLength(int l)
{
length=l;
}
void Rectangle::setWidth(int w)
{
width=w;
}
int Rectangle::getLength()
{
return length;
}
int Rectangle::getWidth()
{
return width;
}
void Rectangle::output()
{
cout << "Length: " << length << endl;
cout << "Width: " << width << endl;
}
int Rectangle ::getarea()
{
return length*width;
}
class Block :public Rectangle
{
private:
int height;
public:
Block(int l=0, int w=0, int h=0);
void setHeigth(int h);
int getHeigth();
void output();
int getarea();
};
Block::Block(int l, int w, int h) :Rectangle(l,w)
{
height=h;
}
void Block::setHeigth(int h)
{
height=h;
}
int Block::getHeigth()
{
return height;
}
int Block ::getarea()
{
return Rectangle::getarea()*height;
}
int main()
{
Rectangle rectangle;
rectangle.setLength(3);
rectangle.setWidth(4);
cout << "Rectangle Area" << endl;
cout << "Area: " << rectangle.getarea()<<endl;// you were not calling th method to get the area
Block block;
block.setLength(3);
block.setWidth(4);
block.setHeigth(5);
cout << "Block area" << endl;
cout << "Area: " << block.getarea()<<endl;// you were not calling th method to get the area
//_getch();// method not defined. please remove it if you want
return 0;
}
-----------output--------
Rectangle Area
Area: 12
Block area
Area: 60
-----------output--------
Note: Please go through the comments to know what was wrong
You can play around the class to invoke differnet method.Feel free to ask doubts/question. God bless you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.