So I'm working on a program. Base class is a polygon (which has an unknown number of sides), with derived classes for rectangle and triangle which inherit from the polygon, and square which inherits from triangle. In my base class I have 2 private members: num_sides and length. Also in my base class I have a setnum_sides() and setlength() function. Do I simply call these 2 functions to set the num_sides and length for all shapes? It seems a little redundant to enter 4 lengths for rectangle, since there are 2 sides of each length.
This is a handwritten program, but if someone needs the code I have so far posted I can type it up....just might take a little bit.
Thanks,
J
8 1405
So I'm working on a program. Base class is a polygon (which has an unknown number of sides), with derived classes for rectangle and triangle which inherit from the polygon, and square which inherits from triangle. In my base class I have 2 private members: num_sides and length. Also in my base class I have a setnum_sides() and setlength() function. Do I simply call these 2 functions to set the num_sides and length for all shapes? It seems a little redundant to enter 4 lengths for rectangle, since there are 2 sides of each length.
This is a handwritten program, but if someone needs the code I have so far posted I can type it up....just might take a little bit.
Thanks,
J
Shouldn't square derive from rectangle instead of triangle? What is lenght supposed to do? Maybe it's better to have a int * pointer lenghts to store the lenght of all the sides. For the sides, you can override the functions in derived classes that you use to set them. For example, the square setSides would take 1 integer compared to the rectangle's 2.
I would assume that square would derive from rectangle, but it's not what the assignment says. I'm assuming that is a mistake. The program has length involved because the next assignment is going to calculate area and perimeter of the shapes.
I would assume that square would derive from rectangle, but it's not what the assignment says. I'm assuming that is a mistake. The program has length involved because the next assignment is going to calculate area and perimeter of the shapes.
well in actuality square and rectangle can inherit from triangle. this will work if triangle contains virtual function for area and perimeter has length, width and hyp. square can inherit them and have its own VIRTUAL function for determining its area and parimeter... esentially a polygon is made up of differant triangles, which an instance of a triangle can contain 1 or more other triangles... look at any 3-d game or model skeleton, you will not see a square.
Shouldn't square derive from rectangle instead of triangle?
A square should not even derive from a rectangle. Read my LSP article in the
Java Article section about exactly this little issue.
kind regards,
Jos
A square should not even derive from a rectangle. Read my LSP article in the
Java Article section about exactly this little issue.
kind regards,
Jos
Nice article, just thought I'd post the link.
How would this work? I ask in main the number of sides. If 4 is selected I ask if it is a polygon, a rectangle or a square, then call the respective setlength function. The setlength function would be declared in the polygon class, the the 3 derived classes would inherit just that. If 3 is selected it would call the triangle.setlength, and if a number greater than 4 is selected it would call the polygon.setlength function? I'm just confused as to how I would inherit the numsides function from polygon, if I don't know the number of sides.
J
How would this work? I ask in main the number of sides. If 4 is selected I ask if it is a polygon, a rectangle or a square, then call the respective setlength function. The setlength function would be declared in the polygon class, the the 3 derived classes would inherit just that. If 3 is selected it would call the triangle.setlength, and if a number greater than 4 is selected it would call the polygon.setlength function? I'm just confused as to how I would inherit the numsides function from polygon, if I don't know the number of sides.
J
Trying to determine the actual shape given just the number of sides is not a
good selection mechanism. You need a better user input processing mechanism
than just that.
kind regards,
Jos
Trying to determine the actual shape given just the number of sides is not a
good selection mechanism.
That should be workable. You have a base class Shape and derived classes ThreeSides, FourSides, etc.
Rectangle derives from FourSides
Pararellogram derived from FourSides
etc.
Then you just: -
Shape* s = new Rectangle;
-
-
s->DisplayName(); //you see "Rectangle"
-
s->DisplayNumberSides(); //you see 4
-
That is, you do not try to determine the shape by the number of sides. The object already knows how many sides it has so you just ask it for that information.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: KK |
last post by:
Windows Forms Inheritance, Incomplete?
I was playing around with Windows Forms
and found out this Forms Inheritance feature.
The moment I saw that, I felt this can be
used effectively if the...
|
by: KK |
last post by:
Windows Forms Inheritance, Incomplete?
I was playing around with Windows Forms
and found out this Forms Inheritance feature.
The moment I saw that, I felt this can be
used effectively if the...
|
by: Matthew Louden |
last post by:
I want to know why C# doesnt support multiple inheritance? But why we can
inherit multiple interfaces instead? I know this is the rule, but I dont
understand why. Can anyone give me some concrete...
|
by: Ben Blank |
last post by:
I'm writing a family of classes which all inherit most of their methods and
code (including constructors) from a single base class. When attempting to
instance one of the derived classes using...
|
by: VR |
last post by:
Hi, I read about Master Pages in ASP.Net 2.0 and after implementing some
WinForms Visual Inheritance I tryed it with WebForms (let's say .aspx pages,
my MasterPage does not have a form tag itself...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |