抽象数据类型 - 从实现中分离接口 [英] Abstract Data Types - Separating Interface from Implementation

查看:61
本文介绍了抽象数据类型 - 从实现中分离接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在学习C ++中的头文件。以下是代码来自

Bartosz Milewski:


//代码


const int maxStack = 16;


class IStack

{

public:

IStack():_ top(0){}

void Push(int i);

int Pop();

private:

int _arr [maxStack ];

int _top;

};


--------------

//头文件


#include" stack.h"

#include< cassert>

#include< iostream>

使用std :: cout;

使用std :: endl;


/ /使用NDEBUG = 1编译以消除断言


void IStack :: Push(int i)

{

断言(_top< maxStack);

_arr [_top] = i;

++ _ top;

}

int IStack :: Pop()

{

断言(_top> 0);

--_ top;

返回_arr [_top];

}


--------------


我知道这是非常基本的代码。我也知道它是一个非标准的

头文件名/文件。我只是用它来演示

目的。让我们假装在C ++中完全可以将接口与实现区分开来。头文件

会不会看起来像这样?:


//用NDEBUG = 1编译来摆脱断言

const int maxStack = 16;


class IStack

{

public:

IStack( ):_ top(0){}

void Push(int i);

int Pop();


void Push (int i)

{

断言(_top< maxStack);

_arr [_top] = i;

++ _ top;

}


int Pop()

{

断言( _top> 0);

--_ top;

返回_arr [_top];

}

私人:

int _arr [maxStack];

int _top;


};


换句话说,定义类的所有内容都将放在

类头中,并且实现文件中写的唯一代码

将是一个调用或调用构造函数或成员函数?


干杯,


Deets

Hi people,

I''m learning about header files in C++. The following is code from
Bartosz Milewski:

// Code

const int maxStack = 16;

class IStack
{
public:
IStack () :_top (0) {}
void Push (int i);
int Pop ();
private:
int _arr [maxStack];
int _top;
};

--------------
// Header file

#include "stack.h"
#include <cassert>
#include <iostream>
using std::cout;
using std::endl;

//compile with NDEBUG=1 to get rid of assertions

void IStack::Push (int i)
{
assert (_top < maxStack);
_arr [_top] = i;
++_top;
}

int IStack::Pop ()
{
assert (_top > 0);
--_top;
return _arr [_top];
}

--------------

I know this is very basic code. I also know that it''s a non-standard
header filename/ file. I''m merely using this for demonstration
purposes. Let''s pretend that in C++ it''s possible to completely
separate the interface from the implementation. Would the header file
then look something like this?:

//compile with NDEBUG=1 to get rid of assertions
const int maxStack = 16;

class IStack
{
public:
IStack () :_top (0) {}
void Push (int i);
int Pop ();

void Push (int i)
{
assert (_top < maxStack);
_arr [_top] = i;
++_top;
}

int Pop ()
{
assert (_top > 0);
--_top;
return _arr [_top];
}
private:
int _arr [maxStack];
int _top;

};

In other words, everything that defines the class would go in the
class header, and the only code written in the implementation file
would be a call or calls to the constructor or member functions?

Cheers,

Deets

推荐答案

" Anon Email" <一个******** @ fastmail.fm>写道...
"Anon Email" <an********@fastmail.fm> wrote...
大家好,我正在学习C ++中的头文件。以下是来自Bartosz Milewski的代码:

//代码

const int maxStack = 16;

类IStack
{
public:
IStack():_ top(0){}
void Push(int i);
int Pop();
private:
int _arr [maxStack];
int _top;
};

--------------
//标题文件

#include" stack.h"
#include< cassert>
#include< iostream>
使用std :: cout;
使用std :: endl;

//使用NDEBUG = 1编译以消除断言

void IStack :: Push(int i)
{<断言(_top< maxStack);
_arr [_top] = i;
++ _ top;
}
int IStack :: Pop()
{
断言(_top> 0);
--_ top;
返回_arr [_top];
}

- ------------

我知道这是非常基本的代码。我也知道它是一个非标准的头文件名/文件。我只是用它来示范
目的。让我们假装在C ++中它可以完全
^^^^^^^^^^^^^

我想你的意思是它是IMpossible,不是吗?

将界面与实现区分开来。头文件是否会看起来像这样?:

//使用NDEBUG = 1编译以摆脱断言

const int maxStack = 16; <课程IStack
{
公开:
IStack():_ top(0){}
void Push(int i);
int Pop();

void Push(int i)
{
断言(_top< maxStack);
_arr [_top] = i;
++ _ top;
}
int Pop()
{
断言(_top> 0);
--_ top;
return _arr [_top];
}

private:
int _arr [maxStack];
int _top;

}; 类头中,而实现文件中编写的唯一代码将是调用或调用构造函数或成员函数?
Hi people,

I''m learning about header files in C++. The following is code from
Bartosz Milewski:

// Code

const int maxStack = 16;

class IStack
{
public:
IStack () :_top (0) {}
void Push (int i);
int Pop ();
private:
int _arr [maxStack];
int _top;
};

--------------
// Header file

#include "stack.h"
#include <cassert>
#include <iostream>
using std::cout;
using std::endl;

//compile with NDEBUG=1 to get rid of assertions

void IStack::Push (int i)
{
assert (_top < maxStack);
_arr [_top] = i;
++_top;
}

int IStack::Pop ()
{
assert (_top > 0);
--_top;
return _arr [_top];
}

--------------

I know this is very basic code. I also know that it''s a non-standard
header filename/ file. I''m merely using this for demonstration
purposes. Let''s pretend that in C++ it''s possible to completely ^^^^^^^^^^^^^
I guess you mean "it''s IMpossible", don''t you?
separate the interface from the implementation. Would the header file
then look something like this?:

//compile with NDEBUG=1 to get rid of assertions
const int maxStack = 16;

class IStack
{
public:
IStack () :_top (0) {}
void Push (int i);
int Pop ();

void Push (int i)
{
assert (_top < maxStack);
_arr [_top] = i;
++_top;
}

int Pop ()
{
assert (_top > 0);
--_top;
return _arr [_top];
}
private:
int _arr [maxStack];
int _top;

};

In other words, everything that defines the class would go in the
class header, and the only code written in the implementation file
would be a call or calls to the constructor or member functions?




我不确定你在C ++上读过什么书,但你不需要

(实际上你不能' ')宣布你的会员职能b在尝试

来定义它们之前。只需丢弃声明并保留

定义(也将是声明)。


class has_definitions_inside

{

int foo()

{

返回42;

}

双栏( )

{

返回3.14159;

}

}

Victor



I am not sure what book you read on C++, but you don''t need to
(actually you mustn''t) declare your member functions before trying
to define them. Just throw away the declarations and keep the
definitions (which will also be declarations).

class has_definitions_inside
{
int foo()
{
return 42;
}
double bar()
{
return 3.14159;
}
}
Victor


" Anon Email" <一个******** @ fastmail.fm>在消息中写道

news:83 ************************* @ posting.google.co m
"Anon Email" <an********@fastmail.fm> wrote in message
news:83*************************@posting.google.co m
大家好,

我正在学习C ++中的头文件。以下是来自Bartosz Milewski的代码:

//代码

const int maxStack = 16;

类IStack
{
public:
IStack():_ top(0){}
void Push(int i);
int Pop();
private:
int _arr [maxStack];
int _top;
};

--------------
//标题文件

#include" stack.h"
#include< cassert>
#include< iostream>
使用std :: cout;
使用std :: endl;

//使用NDEBUG = 1编译以消除断言

void IStack :: Push(int i)
{<断言(_top< maxStack);
_arr [_top] = i;
++ _ top;
}
int IStack :: Pop()
{
断言(_top> 0);
--_ top;
返回_arr [_top];
}

- ------------

我知道这是非常基本的代码。我也知道它是一个非标准的头文件名/文件。我只是用它来示范
目的。让我们假装在C ++中,可以完全将接口与实现分开。头文件是否会看起来像这样?:

//使用NDEBUG = 1编译以摆脱断言

const int maxStack = 16; <课程IStack
{
公开:
IStack():_ top(0){}
void Push(int i);
int Pop();

void Push(int i)
{
断言(_top< maxStack);
_arr [_top] = i;
++ _ top;
}
int Pop()
{
断言(_top> 0);
--_ top;
return _arr [_top];
}

private:
int _arr [maxStack];
int _top;

}; 类头中,而实现文件中编写的唯一代码将是调用或调用构造函数或成员功能?

干杯,

Deets
Hi people,

I''m learning about header files in C++. The following is code from
Bartosz Milewski:

// Code

const int maxStack = 16;

class IStack
{
public:
IStack () :_top (0) {}
void Push (int i);
int Pop ();
private:
int _arr [maxStack];
int _top;
};

--------------
// Header file

#include "stack.h"
#include <cassert>
#include <iostream>
using std::cout;
using std::endl;

//compile with NDEBUG=1 to get rid of assertions

void IStack::Push (int i)
{
assert (_top < maxStack);
_arr [_top] = i;
++_top;
}

int IStack::Pop ()
{
assert (_top > 0);
--_top;
return _arr [_top];
}

--------------

I know this is very basic code. I also know that it''s a non-standard
header filename/ file. I''m merely using this for demonstration
purposes. Let''s pretend that in C++ it''s possible to completely
separate the interface from the implementation. Would the header file
then look something like this?:

//compile with NDEBUG=1 to get rid of assertions
const int maxStack = 16;

class IStack
{
public:
IStack () :_top (0) {}
void Push (int i);
int Pop ();

void Push (int i)
{
assert (_top < maxStack);
_arr [_top] = i;
++_top;
}

int Pop ()
{
assert (_top > 0);
--_top;
return _arr [_top];
}
private:
int _arr [maxStack];
int _top;

};

In other words, everything that defines the class would go in the
class header, and the only code written in the implementation file
would be a call or calls to the constructor or member functions?

Cheers,

Deets




您似乎混淆了实现代码和客户端代码。 />

所有声明类对象和对类​​成员的所有调用

函数是客户端代码。实现代码基本上是代码,

定义了成员函数和类的数据成员。你的更改将所有实现代码移动到头文件中,如果有的话,减少接口和实现之间的分离。


应该指出,

接口/实现分离的最重要方面不是代码所在的文件。在

中最基本的意义上,一个类's界面由公共

成员组成。因此,为了最大限度地实现界面与实现b / b
之间的分离,您应该最小化一个类的公共成员。通常

意味着将所有数据成员设为私有(或者如果绝对必要则受保护)

,以便客户只能通过呼叫公共与类对象进行交互

成员职能。这允许您更改类中的任何内容,除了

公共成员函数的签名,而无需在客户端代码中更改任何



-

John Carson

1.要回复电子邮件地址,请删除donald

2.不要回复电子邮件地址(帖子而是在这里)



You seem to be confusing implementation code and client code.

All declarations of the class object and all calls to the class member
functions are client code. Implementation code is basically code that
defines the member functions plus a class''s data members. Your changes move
all the implementation code into the header file and, if anything, reduce
the separation between interface and implementation.

It should be pointed out that the most important aspect of
interface/implementation separation is not what file the code goes in. In
the most fundamental sense, a class''s interface consists of its public
members. Accordingly, to maximise separation between interface and
implementation, you should minimise a class''s public members. This usually
means making all data members private (or protected if absolutely necessary)
so that clients interact with class objects only through calling public
member functions. This allows you to change anything in the class except for
the signatures of the public member functions without necessitating any
change in client code.
--
John Carson
1. To reply to email address, remove donald
2. Don''t reply to email address (post here instead)


" John Carson" <做*********** @ datafast.net.au>在消息中写道

news:3f ******** @ usenet.per.paradox.net.au
"John Carson" <do***********@datafast.net.au> wrote in message
news:3f********@usenet.per.paradox.net.au

类对象的所有声明并且对类成员
函数的所有调用都是客户端代码。

All declarations of the class object and all calls to the class member
functions are client code.




我应该说除了那些之外所有对类成员函数的调用
其他会员功能的
是客户代码。

-

John Carson

1.回复电子邮件地址,删除donald

2.不要回复电子邮件地址(在此处发帖)



I should have said "all calls to class member functions other than those
from other member functions are client code."
--
John Carson
1. To reply to email address, remove donald
2. Don''t reply to email address (post here instead)


这篇关于抽象数据类型 - 从实现中分离接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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