comp.lang.ada
 help / color / mirror / Atom feed
From: Ole-Hjalmar Kristensen <ole-hjalmar.kristensen@substitute_employer_here.com>
Subject: Re: how can i allocate an objekt with initialization???
Date: 06 Dec 2004 11:04:56 +0100
Date: 2004-12-06T10:05:00+00:00	[thread overview]
Message-ID: <wvbry8gboi7r.fsf@sun.com> (raw)
In-Reply-To: 3459883.9H2ke8dEzz@linux1.krischik.com

>>>>> "MK" == Martin Krischik <martin@krischik.com> writes:

    MK> Thomas Bruns wrote:
    >> TEST : FATHER_CLASS_PTR;
    >> 
    >> begin
    >> TEST:= new CHILD_CLASS'( INT=>1); -- i will initialze the objekt here,but

    MK> Since you are initializing an element form the parent class you need to
    MK> initialize the child as well:

    MK> TEST:= new CHILD_CLASS'(
    MK>                          INT1 =>1
    MK>                          INT2 =>1);

    MK> only the other way round you can shortcut:

    MK> TEST:= new CHILD_CLASS'(
    MK>                          FATHER_CLASS
    MK>                      WITH
    MK>                          INT2 =>1); 

    MK> And the standart question for beginners: Are you shure you need a pointer?
    MK> Unlike C/C++/Java Ada allows for:

    MK> TEST : FATHER_CLASS'Class  :=�CHILD_CLASS'(
    MK>                          INT1 =>1
    MK>                          INT2 =>1);

    MK> You can use inheritance in Ada without using pointers! FATHER_CLASS'Class
    MK> can be used just like a String - you can pass it around as parameter,
    MK> return it from a function, store it in a record. There are collection
    MK> libraries where you can store them.

You are confused. You can use inheritance and virtual functions in C++
as well without using pointers:

#include <iostream>

class a
{
private:
  int _value;
public:
  a(int x) : _value(x) {}
  int n(){return _value;}
  virtual void f() = 0;
};

class b : public a
{
public:
  b(int x) : a(x) {}
  void f();
};

void b::f()
{
  std::cout << "I am b " << n() <<"\n";
}

class c : public a
{
public:
  c(int x) : a(x) {}
  void f();
};

void c::f()
{
  std::cout << "I am c " << n() << "\n";
}

void test(a& x)
{
  x.f();
}

int main(int argc, char *argv[])
{
  b my_b(1);
  c my_c(2);

  test(my_b);
  test(my_c);

  return 0;
}
    MK> With Regards

    MK> Martin
    MK> -- 
    MK> mailto://krischik@users.sourceforge.net
    MK> http://www.ada.krischik.com

-- 
   C++: The power, elegance and simplicity of a hand grenade.



  reply	other threads:[~2004-12-06 10:04 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-12-05 16:54 how can i allocate an objekt with initialization??? Thomas Bruns
2004-12-05 22:51 ` Stephen Leake
2004-12-06  8:52 ` Martin Krischik
2004-12-06 10:04   ` Ole-Hjalmar Kristensen [this message]
2004-12-06 11:49     ` Adrien Plisson
2004-12-06 13:34       ` Martin Krischik
2004-12-13  6:38         ` Dave Thompson
2004-12-13 11:11           ` Martin Krischik
2004-12-06 11:55     ` Martin Krischik
replies disabled

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