0% found this document useful (0 votes)
65 views4 pages

Inheritance With Classes of Shapes?: C/C++/Ada Code Coverage

The document discusses inheritance with classes of shapes in C++. The author is trying to write code to get the perimeter of a rectangle and area of shapes like rectangle, triangle and square using derived classes and inheritance. They are running into errors with constructors and not sure if inheritance is being used correctly.

Uploaded by

rrs_1988
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views4 pages

Inheritance With Classes of Shapes?: C/C++/Ada Code Coverage

The document discusses inheritance with classes of shapes in C++. The author is trying to write code to get the perimeter of a rectangle and area of shapes like rectangle, triangle and square using derived classes and inheritance. They are running into errors with constructors and not sure if inheritance is being used correctly.

Uploaded by

rrs_1988
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Search:

Go
Not logged in

Forum

Beginners

Inheritance with classes of shapes?

register

log in

C++
Information
Tutorials
Reference
Articles
Forum

Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs

C/C++/Ada Code Coverage


Reduce tim escales, risk & effort for DO 178B/C up to level A (MC/DC)

Inheritance with classes of shapes?


ECEsasha (100)

Apr 21, 2014 at 8:41pm

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)

Apr 22, 2014 at 12:14am

Anyone? D:

fun2code (1800)

Apr 22, 2014 at 1:00am

I w onder if it's your Square class constructor:

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)

Apr 22, 2014 at 1:11am

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)

Apr 22, 2014 at 1:55am

It may be your Rectangle and Triangle constructors:

1 Rectangle::Rectangle(float width, float length){


2 width = width;// may not be assigning the class member values here
3 length = length;// may be assigning the parameter to itself instead
4}
I w ould use different names for the parameters, maybe:

1 Rectangle::Rectangle(float Width, float Length){


2 width = Width;
3 length = Length;// no confusion now
4}
Similarly for base and height in the Triangle constructor.
EDIT: Re the class inheritance. I haven't seen the assignment description, but I'm guessing that the 2 functions
get_area() and get_perimeter() are supposed to be virtual. Probably should be pure virtual in the base class
(because no definitions can be w ritten for the Shape class). So in the Shape class:

1 virtual float get_area() = 0;


2 virtual float get_perimeter() = 0;
I don't think the float area, perimeter; members are needed there either.
You then define virtual versions of get_area() and get_perimeter() in each derived class, except for Square
because the definitions inherited from the Rectangle class are correct already for it.
Just add the virtual keyw ord before the function defs. in Triangle and Rectangle. Example:

1 virtual float Rectangle::get_area(){


2 return width*length;
3}
Last edited on Apr 22, 2014 at 2:10am
ECEsasha (100)

Apr 22, 2014 at 2:36am

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)

Apr 22, 2014 at 2:42am

converted by W eb2PDFConvert.com

You could alternatively do

1 Rectangle::Rectangle(float width, float length){


2 this->width = width; //this is a pointer to the current object
3 this->length = length; //-> is the access operator
4}
though it is best if you have unique names or use initalizer list like:

1 Rectangle::Rectangle(float width, float length)


2 : width(width), length(length){}
*fixed code tags
Last edited on Apr 22, 2014 at 2:43am

Topic archived. No new replies allow ed.

Home page | Privacy policy


cplusplus.com, 2000-2015 - A ll rights reserved - v3.1
Spotted an error? contact us

converted by W eb2PDFConvert.com

You might also like