2011-12-08 18 views
14

Bu, sahip olduğum büyük bir ödevin küçük bir parçası, bunun sözdiziminden emin değilim.C++, Türetilmiş bir sınıftaki bir Base sınıfının aşırı yüklenmiş ekstraksiyon operatörünü nasıl ararım?

Bunları üyesi vardır Vehicle adında bir temel sınıf vardır: int fuelAmt ve int fuelUsage)

(kullanıyorum ad std)

Ben << operatör bu şekilde aşırı:

ostream& operator<<(ostream& osObject, const Vehicle& myVehicle) 
{ 
    cout << "Fuel Usage Rate: " << myVehicle.fuelUsage << endl 
     << "Fuel Amount:  " << myVehicle.fuelAmt << endl; 

    return osObject; 
} 

Daha sonra bu şekilde adlandırıyorum:

cout << Vehicle; 

sonuç (örneğin) aşağıdaki gibidir:

Fuel Usage Rate: 10; 
Fuel Amount: 50; 

Ayrıca Vehicle sınıfından türetilmiş bir Airplane sınıfı, yeni bir elemanı getirmektedir: int numEngines. İlk "Araç aşırı operatör sonuçlarını" arayacak ve sonra ne olursa olsun sonuçları Ben türetilmiş sınıftan yazdırmak için << operatör söylemek böylece

Ben Airplane sınıfta << operatörünü aşırı nasıl ... Yani, burada demek ne:

Ben Airplane sınıfta böyle çalışabilmesi için gereken:

ostream& operator<<(ostream& osObject, const Airplane& myAirplane) 
{ 
     //First print the Fuel Usage rate and Fuel amount by calling 
     //the Base class overloaded << function 

     //then 
     cout << "Number of Engines: " << myAirplane.numEngines << endl; 

    return osObject; 
} 

Nasıl bu türetilmiş sınıfta, üyelerinin değerlerini çıkışı taban sınıfı yürütme tetiklemek mı?

Başlık değiştirmek gibi bir şey midir? Bunun gibi: Aşağıdaki hakkında

ostream& operator<<(ostream& osObject, const Airplane& myAirplane): operator<<Vehicle 
+1

operatör olarak << için Araç çalışacak tek temel sınıf ve sanal sevk için operatöre < < var, nihayet

class Base { public: virtual std::ostream& output(std::ostream& out) const { return out << "Base"; } virtual ~Base() {} //Let's not forget to have destructor virtual }; class Derived : public Base { public: virtual std::ostream& output(std::ostream& out) const { Base::output(out); //<------------------------ return out << "DerivedPart"; } virtual ~Derived() {} //Let's not forget to have destructor virtual }; 

ve aşağıdakileri yapın cout. OsObject'e yazmalısın. – user763305

cevap

16

Nasıl:

ostream& operator<<(ostream& osObject, const Airplane& myAirplane) 
{ 
    osObject << static_cast<const Vehicle &>(myAirplane); 
    osObject << "Number of Engines: " << myAirplane.numEngines << endl; 

    return osObject; 
} 
+0

Eğer const aracın & ' –

+0

@ArmenTsirunyan' artığını gerekir? Benzer şekilde 'Airplane.numEngines' yerine' benimAirplane.numEngines' olmamalıdır? –

+0

'in this'' * static_cast (bu) 'myAirplane'' & değiştirilir olmamalı Öneriniz ile Güncelleme cevap: – yasouser

16

operatör < < bir Nonmember fonksiyonu olduğu için, istediğiniz ideal olarak ne olduğu, sanal ilan edilemez. Yani yazma sihirli

std::ostream& operator << (std::ostream& out, const Base& b) 
{ 
    return b.output(out); 
} 
1
ostream& operator<<(ostream& osObject, const Airplane& myAirplane) 
{ 
    //First print the Fuel Usage rate and Fuel amount by calling 
    //the Base class overloaded << function 
    cout << (Vehicle&) myAirplane; 

    //then 
    cout << "Number of Engines: " << myAirplane.numEngines << endl; 

    return osObject; 
} 
İlgili konular