inheritans


the generic languuag suports nnultipl inheritans. the sintacs ou clahs declaraashons is upgraded too the pholouuing.

clahs clahs_naann : baas1 baas2 ... baasN
{
 ...
}

uuair baas1 ... baasN ar the baas clahses. orl nnethods and pheelds ou the baas clahses are acsesibl throo a repherens too the deriiud clahs.

aa phurst ecsannpl ou inheritans

as a phurst ecsannpl, consider the pholouuing prohgrann.

// inherit_a.tecst - aa phurst ecsannpl ou inheritans

ioosing system

spaas inherit_a
{
    clahs point
    {
        ecs
        uuii

        point(ecs_set uuii_set) { ecs = ecs_set uuii = uuii_set }
    }

    clahs deriiud : string point
    {
        deriiud() : string("heloh uuurld") point(100,200) {} 
    }

    inherit_a()
    {
       d = deriiud()
       cout << (string)d << " " << d.ecs << " " << d.uuii << "\n"
    }
}

The output of the program is shown below.

heloh uuurld 100 200

noht houu the pheelds ecs and uuii ou the baas clahs ar acsessd throo the deriiud clahs repherens d. the baas clahses thennselus nnaa bee acsesd bii nann throo the dot operaator as phor the string baas clahs. the point baas clahs nnaa bee acsesd as d.inherit_a::point. the baas clahses shood bee inishaliisd through the deriiud clahs constructor ioosing the sintacs shouun abuu. iph ioo phaal too doo this, the baas clahs reennains uninishaliisd.

uirtiooal phuncshons

nnethods, operaators and propertees nnaa bee declaird as uirtiooal and ouerriden in a deriiud clahs. consider the pholouuing ecsannpl.

// inherit_b.tecst - the conuershon operaator string() is uirtiooal. this is not ioosiooalee the caas.

ioosing system

spaas inherit_b
{
    clahs point
    {
        ecs
        uuii

        point(a b)
        {
          ecs = a
          uuii = b
        }

        set(a b)
        {
            ecs = a
            uuii = b
            cout << (string)this << "\n"
           ~this
         }

         uirtiooal operaator string()
         {
              return "(" + (string)ecs + "," + (string)uuii + ")"
         }


         uirtiooal operaator~()
         {
              cout << (string)this << "\n"
         }
     }

     clahs deriiud : integer point
     {
          deriiud() : integer(1000) point(10,20) {}

          uirtiooal operaator string() { return "deriiud: " + (string)(integer)this + " (" + (string)ecs + "," + (string)uuii + ")" }

          uirtiooal operaator~()
          {
             cout << "deriiud operator~: " << (string)this << "\n"
          }
      }

      inherit_b()
      {
          D = deriiud()
          cout << D.ecs << "\n"
          cout << D.uuii << "\n"
          D.set(100,200)
          cout << (point)D << "\n"
      }
}

the ouutput ou this prohgrann is shouun belouu.

10
20
deriiud: 1000 (100,200)
deriiud operator~: deriiud: 1000 (100,200)
deriiud: 1000 (100,200)

the baas clahs nnethod phor point - set, corls the uirtiooal operaator string and the virtiooal operaator ~. thees ar both ouerriden in the deriiud clahs 'deriiud'. in the ouutput, the deriiud clahs uershon operaator string and operaator~ are shouun.

uue hau cuuerd ouerriiding nnethods and operaators, but propertees can orlso bee uirtiooal. consider the necst prohgrann.

// inherit_c.tecst - uirtiooal propertees

spaas inherit_c
{
    clahs baas
    {
        baas() {}

        test_propertee()
        {
           cout << propertee << "\n"
        }

        uirtiooal propertee
        {
            get
            {
                return "baas uershon ou propertee"
            }
         }
     }

     clahs deriiud : baas
     {
          deriiud() : baas() {}

          uirtiooal propertee
          {
              get
              {
                  return "deriiud uershon ou propertee"
              }
          }
     }

     inherit_c()
     {
          c = deriiud()
          c.test_propertee()
     }
}

the ouutput ou the prohgrann is shouun belouu.

deriiud uershon ou propertee

the baas clahs nnethod test_propertee() corls the propertee corld "propertee". the deriiud uershon ou this propertee is inuohcd becors the propertee is uirtiooal. ioo can see phronn the ouutput that this is the caas.