473,388 Members | 1,326 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,388 software developers and data experts.

Classes and interfaces

Hello,

I want to create a Shape class and some subclasses such as Rectangle, Circle
etc. However for the Shape Class, I want to have a list of points (using a
vector) and also I want it so that the Shape Classes forces the subclasses
to implement a Draw() function. I believe what I want is called an
interface right?? If so, how do I go about creating the Shape Class to force
the subclasses to implement the Draw() function??

Thanks for any help!

--
Regards,
Webster
Jul 22 '05 #1
9 1216
"Webster" <no***@nowhere.net> wrote...
I want to create a Shape class and some subclasses such as Rectangle, Circle etc. However for the Shape Class, I want to have a list of points (using a
vector) and also I want it so that the Shape Classes forces the subclasses
to implement a Draw() function. I believe what I want is called an
interface right??
Interface is a mechanism for communication between two parties. Any
function is an interface between the caller and the called (if those are
objects). So, yes, member functions fall into the "interface" category.
Whether it's something the base class _forces_ the derived classes to
implement or not does not matter.
If so, how do I go about creating the Shape Class to force
the subclasses to implement the Draw() function??


It has to be pure virtual.

V
Jul 22 '05 #2
Webster wrote:
Hello,

I want to create a Shape class and some subclasses such as Rectangle, Circle
etc. However for the Shape Class, I want to have a list of points (using a
vector) and also I want it so that the Shape Classes forces the subclasses
to implement a Draw() function. I believe what I want is called an
interface right??
In Java. In C++, it's called an "abstract class."
If so, how do I go about creating the Shape Class to force
the subclasses to implement the Draw() function??


struct Shape
{
virtual void draw( ) =0;
};

Jul 22 '05 #3

"Webster" <no***@nowhere.net> wrote in message
news:6q*********************@news04.bloor.is.net.c able.rogers.com...
Hello,

I want to create a Shape class and some subclasses such as Rectangle, Circle etc. However for the Shape Class, I want to have a list of points (using a
vector) and also I want it so that the Shape Classes forces the subclasses
to implement a Draw() function. I believe what I want is called an
interface right?? If so, how do I go about creating the Shape Class to force the subclasses to implement the Draw() function??

Thanks for any help!

--
Regards,
Webster

An abstract or pure virtual base class is the same thing AFAIK, and what
this means is a class that cannot be instantiated i.e: it is designed in
such a way that is is only meant to be derived from. These are called
Interfaces in COM, C# etc (dunno about Java).
Note: classes of this type are also know as ADT's ( Abstract data types).

However what you seem to require is a virtual method, which is normally
closely related with ADT's ( Abstract data types) as an ADT which exposes
any methods will normall use vitual methods.
What virtual means is tell the compiler that I intend to overide this
function in a derived class i.e:
class Class1{
public:
virtual void Draw(){std::cout << "Class1 draw()"<< std::endl;}
};

That is an example of a virtual method.
Next if you create a derived class an incorporate a method with the same
signature like so:

class Class2:public Class1{
public:
void Draw(){std::cout << "Class2 draw()"<< std::endl;}
};
When you create an instance of the derived class the compiler knows to call
the correct method i.e:
int main(){
Class1 obj1;
Class2 obj2;
obj1.Draw(); //outputs Class1 Draw()
obj2.Draw(); //outputs Class2 Draw()

return 0;
}

If a function is virtual then it will percolate up to a derived class if the
derived class contains a method of the same signature.
I'm not sure if I have explained this very well but I hope it helps.

You can also create instances of the base class, you don't need to make it
an ADT.
Additionally if you do want to make a base class an ADT then you put '=0'
after one of the methods. The following code would make Class1 pure virtual
and therefore you would be intending that it is not directly initialised and
only to be used as an interface. (This is what lies at the heart of COM and
ATL(Abstract template library) ):

class Class1{
public:
virtual void Draw() =0 {std::cout << "Class1 draw()"<< std::endl;}
};

You cannot create an instance of Class1 now but Class2 can still derive from
it and the virtual functions will still percolate up to Class2. Be sure to
include virtual destructors etc.

However having said that you can declare a pointer to an ADT. It all gets a
bit complicated from here so I'll leave it at that.

Jul 22 '05 #4
"Webster" <no***@nowhere.net> wrote:
I want to create a Shape class and some subclasses such as Rectangle, Circle
etc. However for the Shape Class, I want to have a list of points (using a
vector) and also I want it so that the Shape Classes forces the subclasses
to implement a Draw() function. I believe what I want is called an
interface right?? If so, how do I go about creating the Shape Class to force
the subclasses to implement the Draw() function??

Thanks for any help!


class Shape {
public:
virtual ~Shape() { }
virtual std::vector<point> getPoints() const = 0;
virtual void draw() = 0;
};

I have to ask, what are shapes supposed to draw on? Shouldn't that be a
parameter of the draw member-function?
Jul 22 '05 #5
"Daniel T." <po********@eathlink.net> wrote in message
news:po******************************@news01.east. earthlink.net...
"Webster" <no***@nowhere.net> wrote: class Shape {
public:
virtual ~Shape() { }
virtual std::vector<point> getPoints() const = 0;
virtual void draw() = 0;
};
Cool.. thanks for the exact class!!
I have to ask, what are shapes supposed to draw on? Shouldn't that be a
parameter of the draw member-function?


The draw function will be called from another class. I am using FLTK and
OpenGL so there is a panel that calls the Draw() function which just sends
the points.

--
Regards,
Webster
Jul 22 '05 #6
"Webster" <no***@nowhere.net> wrote in message
news:_3********************@twister01.bloor.is.net .cable.rogers.com...
"Daniel T." <po********@eathlink.net> wrote in message
news:po******************************@news01.east. earthlink.net...
"Webster" <no***@nowhere.net> wrote:

class Shape {
public:
virtual ~Shape() { }
virtual std::vector<point> getPoints() const = 0;
virtual void draw() = 0;
};


Cool.. thanks for the exact class!!
I have to ask, what are shapes supposed to draw on? Shouldn't that be a
parameter of the draw member-function?


The draw function will be called from another class. I am using FLTK and
OpenGL so there is a panel that calls the Draw() function which just sends
the points.

--
Regards,
Webster


Well then let me suggest an alternative: use a standalone function to draw
the points:

class Shape {
// same as before except no draw() function
};

void draw(const Shape &);

You are considering a shape to be a bunch of points. Fair enough, but what
does that have to do with drawing? Absolutely nothing. Your Shape family of
classes might prove useful in other applications if you don't package the
drawing stuff with them.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #7
"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message
news:MD*******************@twister.nyroc.rr.com...
I have to ask, what are shapes supposed to draw on? Shouldn't that be a parameter of the draw member-function?
The draw function will be called from another class. I am using FLTK and
OpenGL so there is a panel that calls the Draw() function which just sends the points.
Well then let me suggest an alternative: use a standalone function to draw
the points:

class Shape {
// same as before except no draw() function
};

void draw(const Shape &);

You are considering a shape to be a bunch of points. Fair enough, but what
does that have to do with drawing? Absolutely nothing. Your Shape family of classes might prove useful in other applications if you don't package the
drawing stuff with them.


You bring up a very good and valid point... but how can I draw the shape
unless I know what to do with the points?? For instance, if I have a Line
specified by 2 Points, I just draw a line between them, but a Rectangle
specified by two Points, I have to derive the extra 2 Points. That is why I
wanted a Draw() function for each Shape, so that I can know how to draw
them, or if any special considerations need to be taken into account.

--
Regards,
Webster
Jul 22 '05 #8
Webster wrote:
"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message
news:MD*******************@twister.nyroc.rr.com...

I have to ask, what are shapes supposed to draw on? Shouldn't that be
a
parameter of the draw member-function?
The draw function will be called from another class. I am using FLTK and
OpenGL so there is a panel that calls the Draw() function which just
sends
the points.


Well then let me suggest an alternative: use a standalone function to draw
the points:

class Shape {
// same as before except no draw() function
};

void draw(const Shape &);

You are considering a shape to be a bunch of points. Fair enough, but what
does that have to do with drawing? Absolutely nothing. Your Shape family


of
classes might prove useful in other applications if you don't package the
drawing stuff with them.

You bring up a very good and valid point... but how can I draw the shape
unless I know what to do with the points?? For instance, if I have a Line
specified by 2 Points, I just draw a line between them, but a Rectangle
specified by two Points, I have to derive the extra 2 Points. That is why I
wanted a Draw() function for each Shape, so that I can know how to draw
them, or if any special considerations need to be taken into account.


If you're writing a very small program, putting the shapes in a
hierarchy with a "draw" method is probably okay. For larger projects,
it may be worth creating a separate "Drawer" hierarchy, and moving the
polymorphism of Shapes to compile-time.
#include <iostream>

namespace Shapes
{
struct Rectangle { };
struct Triangle { };
}

namespace Drawing
{
template< typename T >
void draw( T const& t )
{
std::cout << "Don't know how to draw typeid \""
<< typeid( t ).name( ) << "\". :(\n";
}

template< >
void draw( Shapes::Rectangle const& rectangle )
{
std::cout << "I drew a rectangle.\n";
}

template< >
void draw( Shapes::Triangle const& triangle )
{
std::cout << "I drew a triangle.\n";
}

struct Abstract_drawer
{
virtual void draw_shape( ) =0;
};

template< typename Shape >
struct Drawer: Abstract_drawer
{
void draw_shape( )
{
draw( shape );
}

Shape shape;
};
}

#include <algorithm>
#include <vector>

int main( )
{
using namespace Shapes;
using namespace Drawing;

Drawer< Rectangle > rectangle_drawer;
Drawer< Triangle > triangle_drawer;
Drawer< int > int_drawer;

std::vector< Abstract_drawer* > drawers;

drawers.push_back( &rectangle_drawer );
drawers.push_back( &triangle_drawer );
drawers.push_back( &int_drawer );

std::for_each( drawers.begin( ),
drawers.end( ),
std::mem_fun( &Abstract_drawer::draw_shape ) );
}

Jul 22 '05 #9

"Jeff Schwab" <je******@comcast.net> wrote in message
news:io********************@comcast.com...
struct Shape
{
virtual void draw( ) =0;
};

make this

virtual void draw() const = 0;

as by drawing itself the object should not change.
Usually you also have a device/window/printer to draw on,
which needs to be passed to the method.

virtual void draw(DEVICE *) const = 0;

In windows this would be a HDC (called differently on other systems).
Jul 22 '05 #10

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

Similar topics

4
by: Robert Zurer | last post by:
I notice that Microsoft puts their interfaces and classes in the same assembly. For example System.Data contains OleDbConnection and IDbConnection etc. I have found it useful to keep my...
9
by: Gomaw Beoyr | last post by:
Two question about the "partial classes" (in the next wersion of ..NET). Question 1 ========== Will partial classes (in the next version of C#) have to be declared "partial" in ALL places. ...
30
by: Frank Rizzo | last post by:
We are having one of those religious debates at work: Interfaces vs Classes. My take is that Classes give you more flexibility. You can enforce a contract on the descendant classes by marking...
9
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
6
by: s99999999s2003 | last post by:
hi i come from a non OO environment. now i am learning about classes. can i ask, in JAva, there are things like interface. eg public interface someinterface { public somemethod (); .... ... }...
19
by: dl | last post by:
I'll try to clarify the cryptic subject line. Let's say I have a base class 'GeometricObject' with a virtual method 'double distance (const GeometricObject &) const'. Among the derived classes...
6
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it...
5
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I am actually a VB.Net guy, but I have worked somewhat with C++ and C#. I just want to ask about the relationship between Abstract Classes and Interfaces. My first question is if...
8
by: =?Utf-8?B?QmVu?= | last post by:
Hi, I have a couple of questions about the proper design of classes. I'll use a simple Customer class for my question. 1) Lets say that I have this Customer class like I said, and I want to...
4
by: Peter | last post by:
Hi I was wondering about the use of interfaces for "data classes". Actually I don't know the accepted term for these types of classes - they are simply classes which have getters and setters, to...
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
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
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,...
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
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 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.