面向对象..... [英] Object orientation.....

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

问题描述

我大学二年级的学生.....

我的任务是..

是否真的可以在C中编写面向对象的代码?

如果是,我们如何实现abstration,多态性,层次结构等。

in C

解决方案

Vishal Naidu说:

我大学二年级的学生.....
我的问题是..
真的有可能在C中编写面向对象的代码?


是的。同样地,真的可以用除了火柴和口香糖之外的任何东西构建一个真人大小的自由女神像复制品。

如果是,我们如何实现abstration ,多元主义,层次结构等。
在C



system(" g ++ -o foo foo.cpp");

我不希望100%否定,所以我应该指出,通过使用opaque类型,你可以毫不费力地获得数据隐藏和封装。

-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 1999年7月29日
http://www.cpax.org.uk

发送电子邮件至上述域名


Vishal,


It而是取决于你所指的面向对象的意思。但是在C的早期,有一个明确定义的概念是什么是基于对象的含义。编程有一些OO

的概念(例如,数据隐藏)但最终可能会被程序员破坏

如果他们愿意的话。


基本思路是构造一个struct(数据成员)和一个

函数指针表(方法)并使用很多void指针;) -

你可以通过搜索object

based来找到详细的描述。和C但是这种方法实际上不再用于明显的

原因。

hth,

ajm。


Vishal Naidu写道:

我是第二年的大学生。


恭喜!

是否真的可以在C中编写面向对象的代码?


是。

如果是,我们如何实现
[数据]抽象,[运行时]多态性,[继承],
等在C?




有两种抽象:


1.)算法抽象和

2.)数据抽象。


算法抽象是指语言特征

允许你创建子程序

(子程序,程序,函数等)

这是独立的,可重复使用的模块。

原始达特茅斯BASIC

不支持算法抽象

但大多数其他高级计算机编程语言都有。

数据抽象是指语言功能

允许您创建新的用户定义类型(UDT)。

标准Fortran 77不支持数据抽象

但大多数现代高级计算机编程语言都支持。


C计算机编程语言允许你创建UDT

*通过*封装*结构中的其他数据类型

但C不*支持数据隐藏 - 私有数据成员。 />
注意 - 对于一些面向对象的程序员,

封装意味着数据隐藏。

数据隐藏不*意味着加密。

没有办法隐藏数据表示

,只有几个简单的测试,一个坚定的程序员不能公开



数据隐藏只是为了防止程序员意外地直接访问实际的数据表示。


C计算机编程语言*不支持继承。

指向派生类型的指针需要明确的种姓

指向基本类型的指针。


C程序员一直在使用运行时多态性

,只要有C程序员。

标准文件例如,ype是一种多态类型。


i m a college student in my second year.....
my queston is..
is it really possible to write object oriented code in C ?
and if yes how do we achieve abstration, polymorhism , hierarchy etc.
in C

解决方案

Vishal Naidu said:

i m a college student in my second year.....
my queston is..
is it really possible to write object oriented code in C ?
Yes. Similarly, it is really possible to build a life-sized replica of the
Statue of Liberty using nothing but matchsticks and chewing gum.
and if yes how do we achieve abstration, polymorhism , hierarchy etc.
in C



system("g++ -o foo foo.cpp");
I don''t wish to be 100% negative, so I should point out that you can get
data hiding and encapsulation fairly painlessly by using opaque types.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29 July 1999
http://www.cpax.org.uk
Email rjh at the above domain


Vishal,

It rather depends on precisely what you mean by "object oriented" but
in the early days of C there was a well defined notion of what was
meant by "object based" programming which has some of the notions of OO
(e.g., data hiding) but could ultimately be subverted by the programmer
if they so wished.

The basic idea was to construct a struct (the data members) and a
function pointer table (the methods) and use void pointers a lot ;) -
you can probably find a detailed description by searching for "object
based" and C but this approach is really not used anymore for obvious
reasons.

hth,
ajm.


Vishal Naidu wrote:

I''m a college student in my second year.
Congratulations!
Is it really possible to write object oriented code in C?
Yes.
And, if yes, how do we achieve
[data] abstraction,
[run-time] polymorhism,
[inheritance],
etc. in C?



There are two kinds of abstraction:

1.) algorithmic abstraction and
2.) data abstraction.

Algorithmic abstraction refers to language features
that allow you to create subprograms
(subroutines, procedures, functions, etc.)
which are independent, reusable modules.
The original Dartmouth BASIC
did not support algorithmic abstraction
but most other high level computer programming languages do.
Data abstraction refers to language features
that allow you to create new, User Defined Types (UDTs).
Standard Fortran 77 does not support data abstraction
but most modern high level computer programming languages do.

The C computer programming language allows you to create UDTs
by *encapsulating* other data types in a struct
but C does *not* support data hiding -- private data members.
Beware -- for some object oriented programmers,
encapsulation implies data hiding.
Data hiding does *not* mean encryption.
There is no way to hide the data representation
which a determined programmer can''t expose
with just a few simple tests.
Data hiding is intended only to prevent programmers
from accidently accessing the actual data representation directly.

The C computer programming language does *not* support inheritance.
An explicit caste is required of a pointer to a derived type
to a pointer of the base type is required.

C programmers have been using run-time polymorphism
as long as there have been C programmers.
The standard FILE type, for example, is a polymorphic type.


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

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