类和接口 [英] Classes and interfaces

查看:53
本文介绍了类和接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我想创建一个Shape类和一些子类,如Rectangle,Circle

等。但是对于Shape类,我想要有一个点列表(使用

向量),我也想要它,以便Shape Classes强制子类

来实现Draw()函数。我相信我想要的是一个

界面吧?如果是这样,我该如何创建Shape类来强制实现Draw()函数的子类?


感谢您的帮助!


-

问候,

韦伯斯特

解决方案

"韦伯斯特" <无*** @ nowhere.net>写道...

我想创建一个Shape类和一些子类,如Rectangle,
Circle等。但是对于Shape类,我想要一个点列表(使用一个
vector)我想要它,以便Shape Classes强制子类
实现Draw()函数。我相信我想要的是一个
接口吧?


接口是双方之间通信的机制。任何

函数都是调用者和被调用者之间的接口(如果这些是

对象)。所以,是的,成员函数属于接口。类别。

无论是基类_forces_派生类到/ b $ b实现与否都无关紧要。

如果是这样,我如何创建Shape类来强制子类实现Draw()函数??




它必须是纯虚拟的。


V


Webster写道:

你好,

我想要创建一个Shape类和一些子类,如Rectangle,Circle
等。但是对于Shape类,我想要一个点列表(使用
向量),我也想要它以便Shape Classes强制子类
实现Draw()函数。我相信我想要的是一个
接口吧?


在Java中。在C ++中,它被称为抽象类。

如果是这样,我如何创建Shape类来强制子类实现Draw()函数??




struct Shape

{

virtual void draw()= 0;

};




" Webster" <无*** @ nowhere.net>在消息中写道

news:6q ********************* @ news04.bloor.is.net.c able.rogers.com ...

你好,

我想创建一个Shape类和一些子类,如Rectangle,
Circle等。但是对于Shape类,我想要有一个点列表(使用
向量),我也想要它,以便Shape Classes强制子类
实现Draw()函数。我相信我想要的是一个
接口吧?如果是这样,我如何创建Shape类到
强制子类实现Draw()函数?

感谢您的帮助!

-
问候,
韦伯斯特



抽象或纯虚拟基类与AFAIK是一回事,以及

这意味着是一个无法实例化的类,即:它是用

设计的,这种方式只是来源于。这些被称为

COM中的接口,C#等(关于Java的dunno)。

注意:这种类的类也称为ADT'(抽象数据类型) 。


然而你似乎需要的是一个虚拟方法,它通常与ADT'(抽象数据类型)密切相关,作为ADT暴露

任何方法都会使用虚拟方法。

什么虚拟方法告诉编译器我打算覆盖这个

函数派生类即:

class Class1 {

public:

virtual void Draw(){std :: cout<< Class1 draw()<< std :: endl;}

};


这是一个虚拟方法的例子。

接下来如果你创建一个派生类包含一个方法,使用相同的

签名如下:


class Class2:public Class1 {

public:

void Draw(){std :: cout<< Class2 draw()<< std :: endl;}

};

当您创建派生类的实例时,编译器知道调用

正确的方法,即:

int main(){

Class1 obj1;

Class2 obj2;

obj1.Draw(); //输出Class1 Draw()

obj2.Draw(); //输出Class2 Draw()


返回0;

}


如果函数是虚函数那么它如果

派生类包含一个相同签名的方法,它将渗透到派生类。

我不确定我是否已经解释得很好但是我希望它有所帮助。


你也可以创建基类的实例,你不需要把它作为ADT支持


另外如果你想把基类设为ADT,那么你可以在其中一种方法之后加上''= 0''

。下面的代码会使Class1纯虚拟

,因此你打算不直接初始化它和

只能用作接口。 (这就是COM的核心和

ATL(抽象模板库)):


class Class1 {

public:

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

};


你现在不能创建Class1的实例但是Class2仍然可以从

派生它和虚拟功能仍然可以渗透到Class2。一定要

包括虚拟析构函数等。


但是说过你可以声明指向ADT的指针。从这里开始这一切都有点复杂,所以我会把它留在那里。


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

解决方案

"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


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;
};



"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.


这篇关于类和接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆