Re: This I simply can't swallow

From: Clark S. Cox III (clarkcox3_at_gmail.com)
Date: 02/16/05


Date: Wed, 16 Feb 2005 09:30:58 -0500

On 2005-02-16 07:33:05 -0500, Morten Gulbrandsen
<Morten.Gulbrandsen@rwth-Aachen.DE> said:

> Hello programmers,
>
> according to this link :
>
>
> URL:
>
> http://www.deakin.edu.au/~agoodman/ctutorial/818.html
>
>
> An encapsulated object is often called an abstract data type.
>
> Is this true ?

No, encapsulation and abstract data types are completely orthogonal
concepts. Encapsulation is hiding the data/implementation of a class
behind a public interface. For instance, take the std::vector class.
There are any number of ways that it could be implemented "under the
hood", but as long as it presents the correct public interface, code
using it will continue to function properly.

> The code actually runs well, with some modifications,
> I also like the explanations,
>
> Except this:
> Class
> A grouping of data and methods (functions). // accepted
> A class is similar to a type in ANSI-C. // warning

I see nothing wrong with this. 'int' is an ANSI-C type:

int i; //A variable of type int
int *p; //A pointer to int
int f(); //A function returning int
sizeof(int); //The number of bytes needed to store an int

class MyClass {};
MyClass i; //An instance of MyClass
MyClass *p; //A pointer to MyClass
MyClass f(); //A function returning MyClass
sizeof(MyClass); //The number of bytes needed to store an MyClass

Seems pretty similar to me.

> Object
> An instance of a class, // accepted
> similar to a variable defined as an instance of a type. // warning

Again, I see nothing wrong with this.

int i; //variable defined as int
MyClass c; //variable defined as an instance of MyClass

Again, seems pretty similar to me.

> This I simply can't swallow.
>
> I believe a class in C++ is like a struct,
> or typedef struct in ANSI C.

classes and structs are the same thing in C++. The only difference is
that structs default to "public" for its members and inheritence.

> And any struct can also contain function pointers.

Anything a struct can do, a class can do too, as they are the same thing.

>
> A type is a data type like int i=1;

No, in what you've typed there, "int" is a type

> Please correct me.
>
> What exactly is the difference between
> encapsulation and an ADT?

An ADT is a type that cannot, itself, be instantiated. ADTs are useful
for, among other things, providing a base used to build other classes
while providing a common interface.

> In C++?

In C++, to make a class an ADT, you simply need to make one or more of
its member functions pure virtual.

class MyClass
{
public:
  virtual void Foo() = 0;
};

In the above, Foo is a pure virtual function. This makes it impossible
to construct instances of MyClass.

>
> best regards
>
> Morten Gulbrandsen

-- 
Clark S. Cox, III
clarkcox3@gmail.com


Relevant Pages

  • Re: Why this fix work ?
    ... "int" is 32 bits wide. ... This is basically a matter of luck -- bad luck that it works at ... MyClass* myobj; ... "MyClass *", which is actually 64 bits wide. ...
    (comp.lang.c)
  • Re: Class Destroys itself straight away!
    ... CRectangle; ... int area ... MyClass mc = MyClass; ... void MyClass::SayHello ...
    (microsoft.public.vc.language)
  • Re: Explicit function vs. overloaded operator?
    ... > void operator() (int x, ... The conceptual advantage is that if class MyClass does just one thing, ... make the code easier to read. ...
    (comp.lang.cpp)
  • Re: Template collection classes
    ... But it also says as it's true, that we quite always use Reference as ... void Func(int x) ... MyClass(): x ... you will read the message "MyClass copy ctor ...
    (microsoft.public.vc.mfc)
  • Re: Getters and Setters
    ... public int getBar() ... The basic principle behind encapsulation is information hiding. ... private int value; ...
    (comp.lang.java.programmer)