comp.lang.ada
 help / color / mirror / Atom feed
From: Jerome Desquilbet <jDesquilbet@Rational.COM>
Subject: Re: on OO differnces between Ada95 and C++
Date: 1996/02/23
Date: 1996-02-23T00:00:00+00:00	[thread overview]
Message-ID: <312D8EF7.167E@Rational.COM> (raw)
In-Reply-To: 4gi413$qo1@druid.borland.com

...YET ANOTHER RELIGIOUS WAR...

Pete Becker wrote:

> >#define private public // *** BERK! ***
> >#include "...h"        // second definition for the same class
> >#undef private
> 
> This is not true. A program that attempts to do this violates the one
> definition rule, so it is not a legal C++ program.

Pete,

What is this "one definition rule"? Could you point me to the
appropriate C++ Draft Standard location?

Try this:

// shape.h -----------------------------------------------------------

#ifndef SHAPE_H_
#define SHAPE_H_
class Shape {
  public:
    Shape();
    virtual void Draw  ()                         const = 0;
    virtual void Erase ()                         const = 0;
            void Move  (int Delta_X, int Delta_Y);
            void Rotate(int Delta_A);
  private:
    int X, Y, A;
};
#endif

// shape.C -----------------------------------------------------------

#include "shape.h"
#include <iostream.h>

Shape::Shape() {X=Y=A=0;}

void Shape::Move(int Delta_X, int Delta_Y) {
  cout << "I am a shape and I begin to move from ("
       << X << ',' << Y << ").\n";
  Erase(); X+=Delta_X; Y+=Delta_Y; Draw();
  cout << "I am a shape and I have finished moving to ("
       << X <<',' << Y << ").\n";
}
void Shape::Rotate(int Delta_A) {
  cout << "I am a shape and I begin to rotate from " << A << ".\n";
  Erase(); A+=Delta_A; Draw();
  cout << "I am a shape and I have finished rotating to " << A << ".\n";
}

// rectangle.h --------------------------------------------------------

#ifndef RECTANGLE_H_
#define RECTANGLE_H_
#include "shape.h"
class Rectangle : public Shape {
  public:
    Rectangle();
    void Draw  ()                         const;
    void Erase ()                         const;
    void Resize(int Delta_H, int Delta_W);
  private:
    int H, W;
};
#endif

// rectangle.C --------------------------------------------------------

#include "rectangle.h"
#include <iostream.h>

Rectangle::Rectangle() : Shape() {H=W=0;}

void Rectangle::Draw() const {
  cout << "I am a rectangle and I draw myself.\n";
}
void Rectangle::Erase() const {
  cout << "I am a rectangle and I erase myself.\n";
}
void Rectangle::Resize(int Delta_H, int Delta_W) {
  cout << "I am a rectangle and I begin to change my size.\n";
  Erase(); H+=Delta_H; W+=Delta_W; Draw();
  cout << "I am a rectangle and I have finished changing my size.\n";
}

// try_shape_rectangle_dirty_way.C ------------------------------------

#define private public // *** BERK! ***
#include "rectangle.h" // second definition for class Rectangle
#undef private        // in the program
int main() {
  Rectangle R;
  Shape*    pS;
  pS = &R;
  pS->X = 8;
  pS->Y = 18;
  pS->Move(10, 20);
}


It gives the following log:


I am a shape and I begin to move from (8,18).
I am a rectangle and I erase myself.
I am a rectangle and I draw myself.
I am a shape and I have finished moving to (18,38).


Note that the initial coordinates were (8,18).

The whole program contains two definitions for the same classes Shape
and Rectangle: for each, one definition with attributes private and
another definition with everything public.

It seems that in C++, every encapsulation can be legally broken,
essentially because of _independent_ compilation that allows
redefinitions of the same class.

Ada compilation model is different: it's _separate_ compilation.
Impossible in Ada to have a redefinition in the same program: it's
detected at compile-time. If something is checked in C++, it's deferred
to link-time (for example a redefinition of the same class that will
clash because the two constructors will have the same name).


Read also "The design and evolution of C++" by Bjarne Stroustrup,
chapter 17 "Namespaces", section 3 "Ideals for a solution": Ada has a
more complete solution than C++.

The basic property of OO languages is encapsulation. According to this
criteria, C++ fails.

Non mais enfin, quoi !


  Jerome.




  reply	other threads:[~1996-02-23  0:00 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-02-20  0:00 on OO differnces between Ada95 and C++ Nasser Abbasi
1996-02-20  0:00 ` Robert I. Eachus
1996-02-20  0:00 ` Norman H. Cohen
1996-02-21  0:00   ` Mark A Biggar
1996-02-22  0:00     ` Norman H. Cohen
1996-02-27  0:00   ` Adam Morris
1996-02-20  0:00 ` Jerome Desquilbet
1996-02-21  0:00   ` Robert Dewar
1996-02-22  0:00     ` Jerome Desquilbet
1996-02-24  0:00       ` Robert Dewar
1996-02-22  0:00   ` Pete Becker
1996-02-23  0:00     ` Jerome Desquilbet [this message]
1996-02-26  0:00     ` Darren C Davenport
1996-02-26  0:00       ` Pete Becker
1996-02-27  0:00         ` Nigel Perry
1996-02-21  0:00 ` Darren C Davenport
1996-02-21  0:00 ` Jon S Anthony
1996-02-21  0:00 ` John English
1996-02-22  0:00   ` Gene Ouye
1996-02-26  0:00     ` John English
1996-02-22  0:00   ` Nasser Abbasi
1996-02-26  0:00     ` John English
1996-02-27  0:00       ` Dale Stanbrough
  -- strict thread matches above, loose matches on Subject: below --
1996-02-21  0:00 Christian Jaensch, FRG
1996-02-26  0:00 Simon Johnston
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox