From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 109fba,9ac62ca34a465706 X-Google-Attributes: gid109fba,public X-Google-Thread: 103376,9ac62ca34a465706 X-Google-Attributes: gid103376,public From: Jerome Desquilbet Subject: Re: on OO differnces between Ada95 and C++ Date: 1996/02/23 Message-ID: <312D8EF7.167E@Rational.COM>#1/1 X-Deja-AN: 140805350 references: <4gbq7q$g08@qualcomm.com> <3129F185.41C6@Rational.COM> <4gi413$qo1@druid.borland.com> content-type: text/plain; charset=us-ascii organization: Rational Software Corporation, France mime-version: 1.0 newsgroups: comp.lang.ada,comp.lang.c++ x-mailer: Mozilla 2.0 (X11; I; OSF1 V3.2 alpha) Date: 1996-02-23T00:00:00+00:00 List-Id: ...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 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 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.