473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Class hierarchy

I would have to implement a class hierarchy.
The base class is the class Quadrilateral.
The discendents arre th classes Trapezoid, Parallelogram, Rectangle, Square.

Quadrilateral
|_
Trapezoid
|_
Parallelogram
|_
Rectangle
|_
Square
The class Quadrilateral takes the cordinates of the four vertex.
What must I implement in the other classes?
In the trapezoid what do I insert?
Thanks.
Jul 22 '05 #1
9 3202

"Piotre Ugrumov" <au************@tin.it> wrote in message
news:%I********************@news4.tin.it...

is this your course homework?

I would have to implement a class hierarchy.
The base class is the class Quadrilateral.
The discendents arre th classes Trapezoid, Parallelogram, Rectangle, Square.
Quadrilateral
|_
Trapezoid
|_
Parallelogram
|_
Rectangle
|_
Square
The class Quadrilateral takes the cordinates of the four vertex.
What must I implement in the other classes?
In the trapezoid what do I insert?
Thanks.

Jul 22 '05 #2
Piotre Ugrumov wrote:
I would have to implement a class hierarchy.
The base class is the class Quadrilateral.
The discendents arre th classes Trapezoid, Parallelogram, Rectangle, Square.

Quadrilateral
|_
Trapezoid
|_
Parallelogram
|_
Rectangle
|_
Square
The class Quadrilateral takes the cordinates of the four vertex.
Is there a practical reason to do this? The fact that a square is a
rectangle does not mean that class Square must be derived from class
Rectangle. This sort of design is typically inefficient. The reason is
that the derived classes don't need the generality that likely will be
provided by the base class. For example, you said Quadrilateral "takes"
four vertices. If those vertices are stored in member variables, then
by deriving Square from Quadrilateral, you force Square to remember all
four vertices. These members will be wasted in Square, since a Square
can be defined by one vertex and a side length.
What must I implement in the other classes?
In the trapezoid what do I insert?


In order to do what? For what do you expect to use these classes?

-Jeff

Jul 22 '05 #3
"Piotre Ugrumov" <au************@tin.it> wrote in message
news:%I********************@news4.tin.it...
I would have to implement a class hierarchy.
The base class is the class Quadrilateral.
The discendents arre th classes Trapezoid, Parallelogram, Rectangle, Square.
Quadrilateral
|_
Trapezoid
|_
Parallelogram
|_
Rectangle
|_
Square
The class Quadrilateral takes the cordinates of the four vertex.
What must I implement in the other classes?
In the trapezoid what do I insert?
Thanks.


Assuming that this is a homework problem (otherwise I would quibble with the
design) I would say that you could implement this heirarchy with classes
which differ only in their constructors. For instance:

class Square : public Rectangle
{
public:
Square(double x0, double y0, double x1, double y1) :
Rectangle(x0, y0, x1, y1, x0+y1-y0, y0+x1-x0) {}
};

Here I have assumed that the constructor for a Rectangle takes three points,
the fourth being computable from the first three.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #4
"Cy Edmunds" <ce******@spamless.rochester.rr.com> writes:
Assuming that this is a homework problem (otherwise I would quibble with the
design) I would say that you could implement this heirarchy with classes
which differ only in their constructors. For instance:

class Square : public Rectangle
{
public:
Square(double x0, double y0, double x1, double y1) :
Rectangle(x0, y0, x1, y1, x0+y1-y0, y0+x1-x0) {}
};

Here I have assumed that the constructor for a Rectangle takes three points,
the fourth being computable from the first three.


This assumes that the square is positioned relatively, since it's not
obvious which vertices you have specified. If this assumption is
correct, you could simply have "Square (double v)" as the constructor,
just specifying the line length of one side--you can compute the
vertices in the constructor. Also, if you're always using vertices, a
Vertex class would be handy, too.
--
Roger Leigh

Printing on GNU/Linux? http://gimp-print.sourceforge.net/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
Jul 22 '05 #5
Jeff Schwab wrote:
Piotre Ugrumov wrote:
I would have to implement a class hierarchy.
The base class is the class Quadrilateral.
The discendents arre th classes Trapezoid, Parallelogram, Rectangle, Square.
Quadrilateral
|_
Trapezoid
|_
Parallelogram
|_
Rectangle
|_
Square
The class Quadrilateral takes the cordinates of the four vertex.


Is there a practical reason to do this? The fact that a square is a
rectangle does not mean that class Square must be derived from class
Rectangle. This sort of design is typically inefficient. The reason is
that the derived classes don't need the generality that likely will be
provided by the base class. For example, you said Quadrilateral "takes"
four vertices. If those vertices are stored in member variables, then
by deriving Square from Quadrilateral, you force Square to remember all
four vertices. These members will be wasted in Square, since a Square
can be defined by one vertex and a side length.
> What must I implement in the other classes?
> In the trapezoid what do I insert?


In order to do what? For what do you expect to use these classes?


Amen! This is a truly disgusting assignment. It is wasteful of memory and
human neurons. It gives the student no insight whatsoever into inheritance
and and how it might be useful instead of an impediment to doing something
interesting and perhaps even useful.

To the OP: {I have read your second post too, the link here is not
chronological)

Presumably, at some point you will be able to compute the perimeter and area
of these four plane figures. That seems a reasonable goal. But it seems
you need some context. IOW, are you (main) to be given four vertices and
determine *what* to construct? Or are you given two vertices and told that
this is to be a square? Looking at your code I see you assume main gets
four vertices and the additional information in the name of the resulting
figure. It sounds a bit like cheating (no insult intended) but someone has
to go through hell to call the constructor and then the thing you wrote goes
through another hell to unwind this complicated mess. Do you see my point?

That is, what is the nature of the *driving* program/problem/whatever? One
candidate: you are given four vertices and told it is a square. Your code
determines if it is indeed a square or if the caller had lied to you. And
proceeds accordingly.

Jul 22 '05 #6
"Roger Leigh" <${******@invalid.whinlatter.uklinux.net.invalid > wrote in
message news:87************@wrynose.whinlatter.uklinux.net ...
"Cy Edmunds" <ce******@spamless.rochester.rr.com> writes:
Assuming that this is a homework problem (otherwise I would quibble with the design) I would say that you could implement this heirarchy with classes
which differ only in their constructors. For instance:

class Square : public Rectangle
{
public:
Square(double x0, double y0, double x1, double y1) :
Rectangle(x0, y0, x1, y1, x0+y1-y0, y0+x1-x0) {}
};

Here I have assumed that the constructor for a Rectangle takes three points, the fourth being computable from the first three.
This assumes that the square is positioned relatively, since it's not
obvious which vertices you have specified.


No, I did not assume the square is positioned relatively. In fact I gave
three of the vertices explicity. The three vertices specified are (x0,y0),
(x1,y1), and (x0+y1-y0,y0+x1-x0). The fourth must be (x1+y1-y0,y1+x1-x0),
but that is for Rectangle's constructor to figure out.

From the OP's initial post:

"The class Quadrilateral takes the cordinates of the four vertex"

Thus we need to specify each vertex explicitly for each derived class.
Otherwise when we get to the top of the heirarchy we won't know what to do.
If this assumption is
correct, you could simply have "Square (double v)" as the constructor,
just specifying the line length of one side--you can compute the
vertices in the constructor. Also, if you're always using vertices, a
Vertex class would be handy, too.
Yes, a Vertex class would be a good idea.


--
Roger Leigh

Printing on GNU/Linux? http://gimp-print.sourceforge.net/
GPG Public Key: 0x25BFB848. Please sign and encrypt your

mail.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #7
> Quadrilateral
|_
Trapezoid
|_
Parallelogram
|_
Rectangle
|_
Square


You can define constructors for each class with lesser number of
parameters and then invoke the base class constructor that
takes four parameters.

The main issue with the hierarchy is that it violates the Liskov
Substitution Principle. See the following article:
http://www.eventhelix.com/RealtimeMa..._principle.htm

Any code that has been written with the Quadrilateral base class might
break if, say, a Square object is passed to it. The Square is a
specialized
Quadrilateral. So code like changing length and width independently
will break.

Hope this helps.

Sandeep
--
http://www.EventHelix.com/EventStudio
EventStudio 2.0 - Go Beyond UML Use Case and Sequence Diagrams
Jul 22 '05 #8
EventHelix.com wrote:
Quadrilateral
|_
Trapezoid
|_
Parallelogram
|_
Rectangle
|_
Square

You can define constructors for each class with lesser number of
parameters and then invoke the base class constructor that
takes four parameters.

The main issue with the hierarchy is that it violates the Liskov
Substitution Principle. See the following article:
http://www.eventhelix.com/RealtimeMa..._principle.htm

Any code that has been written with the Quadrilateral base class might
break if, say, a Square object is passed to it. The Square is a
specialized
Quadrilateral. So code like changing length and width independently
will break.

Hope this helps.

Sandeep
--
http://www.EventHelix.com/EventStudio
EventStudio 2.0 - Go Beyond UML Use Case and Sequence Diagrams


That's interesting.

By the way, none of the classes in the temperature-sensor "hierarchy"
inherit anything. Did I miss something?

Jul 22 '05 #9
Jeff Schwab <je******@comcast.net> writes:
EventHelix.com wrote:
Quadrilateral
|_
Trapezoid
|_
Parallelogram
|_
Rectangle
|_
Square

You can define constructors for each class with lesser number of
parameters and then invoke the base class constructor that
takes four parameters.
The main issue with the hierarchy is that it violates the Liskov
Substitution Principle. See the following article:
http://www.eventhelix.com/RealtimeMa..._principle.htm
Any code that has been written with the Quadrilateral base class
might
break if, say, a Square object is passed to it. The Square is a
specialized
Quadrilateral. So code like changing length and width independently
will break.
Hope this helps.
Sandeep
--
http://www.EventHelix.com/EventStudio
EventStudio 2.0 - Go Beyond UML Use Case and Sequence Diagrams


That's interesting.

By the way, none of the classes in the temperature-sensor "hierarchy"
inherit anything. Did I miss something?


If I understand correctly, if the design must meet the Liskov S. P.,
the base class should provide virtual methods to get/set each vertex,
and then each derived "shape" should override them such that the other
vertices are readjusted to contrain the quadrilateral to their shape?
--
Roger Leigh

Printing on GNU/Linux? http://gimp-print.sourceforge.net/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: John J. Lee | last post by:
I'm trying to change a base class of a big class hierarchy. The hierarchy in question is 4DOM (from PyXML). 4DOM has an FtNode class that defines __getattr__ and __setattr__ that I need to...
1
by: Stefan Seefeld | last post by:
hi there, I'v run into a little problem for which I only found an ugly workaround, so I'd like to know whether people are aware of better ways to achieve this... I'm defining a class 'Class'...
9
by: mead | last post by:
What kind of classes is qualified as "concrete classes"? When should a member function in a class defined as "pure virtual" and when as "virtual"? Thanks!
21
by: Blue Ocean | last post by:
The reason why I ask is because I am unfamiliar with the idea of templates. It seems like it would be easier if all classes that needed something like template<class T> class Stack { ... } ...
2
by: Matt | last post by:
Hello, I would like to generate what I call an "overall class hierarchy" in UML automatically derived from my C++ code source. An example of what I seek: ...
21
by: Mark Broadbent | last post by:
Consider the following statements //------- Item i = Basket.Items; //indexer is used to return instance of class Item Basket.Items.Remove(); //method on class item is fired item i = new...
2
by: BigAbility | last post by:
can i get full class hierarchy of visual studio 2005?? especially, i want to get cotrol class hierarchy of vs.net 2005 help me...
2
by: Bror Johansson | last post by:
Hi, I have a class-hierarchy (fairly deep and fairly wide). Is there a good and general way to test an instance-object obj for having a class belonging to a certain "sub-tree" of the hierarchy...
3
by: krzysztof.konopko | last post by:
Hello! I want to design a class hierarchy with one base abstract class, let's say CBase. I have a predicate that every object in the class hierarchy must have a parent object of type from this...
12
by: Gordon | last post by:
I want to provide a set of static functions in a superclass that work with class constants defined in a decendant of that class. Unfortunately I've run into a snag with this idea. Example: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
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...
0
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...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.