Inheritance With Classes of Shapes?: C/C++/Ada Code Coverage
Inheritance With Classes of Shapes?: C/C++/Ada Code Coverage
Go
Not logged in
Forum
Beginners
register
log in
C++
Information
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Hi! I'm trying to w rite code w here I can get the perimeter of a rectangle, and then the area of a rectangle,
triangle, and square using the get_area() functions.
The classes Triangle and Rectangle are derived from Shape, and the class Square is derived from Rectangle.
My issue is that w hen I run my code as is, I get the error, in Rectangle.h, Rectangle::Rectangle(float,float)
candidate expects 2 arguments, 0 provided
and the error Rectangle::Rectangle(const Rectangle&) candidate expects 1 argument, 0 provided
along w ith an error, in Square::Square(float) saying, no matching function for call to Rectangle::Rectangle()
I thought I had 2 arguments in my Rectangle constructor. And I'm not sure w here I have
Rectangle::Rectangle(const Rectangle&).
Another problem is that I'm not sure if I'm using inheritance correctly as I haven't used area or perimeter from
Shape.
Here is my code thus far:
Shape.h:
1 #ifndef SHAPE_H
2 #define SHAPE_H
3
4 class Shape{ // This is the base class!
5
6 public:
7 Shape();
8 float get_area();
9
10 private:
11 float area, perimeter;
12
13 };
14
15 #endif
Shape.cpp:
1 #include "Shape.h"
2 #include "Triangle.h"
3 #include "Rectangle.h"
4 #include "Square.h"
5 #include <iostream>
6
7 using namespace std;
8
9
10 Shape::Shape(){
11 area = 0;
12 perimeter = 0;
13 }
14
15 float Shape::get_area(){
16 return area;
17 }
Triangle.h:
1 #ifndef TRIANGLE_H
2 #define TRIANGLE_H
3
4 class Triangle : public Shape{
5
6 public:
7 Triangle(float base, float height);
8 float get_area();
9
10 private:
11 float base, height;
12
13 };
14
15
16 #endif
Triangle.cpp:
converted by W eb2PDFConvert.com
1 #include "Shape.h"
2 #include "Triangle.h"
3 #include "Rectangle.h"
4 #include "Square.h"
5 #include <iostream>
6
7 using namespace std;
8
9
10 Triangle::Triangle(float base, float height){
11 base = base;
12 height = height;
13 }
14
15 float Triangle::get_area(){
16 return (0.5*base*height);
17 }
Rectangle.h:
1 #ifndef RECTANGLE_H
2 #define RECTANGLE_H
3
4
5 class Rectangle : public Shape{
6
7 public:
8 Rectangle(float width, float length);
9 float get_area();
10 float get_perimeter();
11
12 private:
13 float width, length;
14
15 };
16
17 #endif
Rectangle.cpp:
1 #include "Shape.h"
2 #include "Triangle.h"
3 #include "Rectangle.h"
4 #include "Square.h"
5 #include <iostream>
6
7 using namespace std;
8
9
10 Rectangle::Rectangle(float width, float length){
11 width = width;
12 length = length;
13 }
14
15 float Rectangle::get_area(){
16 return width*length;
17 }
18
19 float Rectangle::get_perimeter(){
20 return 2*(length+width);
21 }
Square.h:
1 #ifndef SQUARE_H
2 #define SQUARE_H
3
4
5 class Square : public Rectangle{
6
7 public:
8 Square(float side);
9
10 private:
11
12 };
13
14 #endif
Square.cpp:
1 #include "Shape.h"
2 #include "Triangle.h"
3 #include "Rectangle.h"
4 #include "Square.h"
5 #include <iostream>
6
7 using namespace std;
8
9
10 Square::Square(float side){
11 Rectangle r(side,side);
12 }
main.cpp:
converted by W eb2PDFConvert.com
1 #include "Shape.h"
2 #include "Rectangle.h"
Edit
3 #include "Triangle.h"
&
4 #include "Square.h"
Run
5 #include <iostream>
6
7 using namespace std;
8
9
10 int main(){
11
12
13 Rectangle rectangleObject(2,5);
14 Triangle triangleObject(1,10);
15 Square squareObject(5);
16
17 cout << "The area of the rectangle is" << rectangleObject.get_area() << '\n';
18 cout << "The perimeter of the rectangle is " << rectangleObject.get_perimeter() << '\n';
19 cout << "The area of the triangle is " << triangleObject.get_area() << '\n';
20 cout << "The area of the square is " << squareObject.get_area() << '\n';
21
22 return 0;
23 }
Last edited on Apr 21, 2014 at 9:13pm
ECEsasha (100)
Anyone? D:
fun2code (1800)
1 Square::Square(float side){
2 Rectangle r(side,side);
3}
Here, r is a local variable not the Rectangle base object that Square is derived from. Try this:
Square::Square(float side): Rectangle(side,side) {}// calling the 2-arg constructor for the base
object
ECEsasha (100)
Oh, I see! I did not know about that. It compiles now ! Thanks! :D
Except, my output is:
The area of the rectangle isnan
The perimeter of the rectangle is nan
The area of the triangle is 0
The area of the square is 0
I'm really stumped as to w hy the areas are not calculating in get_area
fun2code (1800)
Oh, w hoops.
It all w orks now !
W e haven't learned about virtual functions, yet. But I know w e are soon so I'm excited :D
Thanks for your help and explanations! It's uber appreciated.
giblit (3749)
converted by W eb2PDFConvert.com
converted by W eb2PDFConvert.com