2012-02-24 13 views
16
class base{ 
    ..... 
    virtual void function1(); 
    virtual void function2(); 
}; 

class derived::public base{ 
    int function1(); 
    int function2(); 
}; 

int main() 
{ 
    derived d; 
    base *b = &d; 
    int k = b->function1() // Why use this instead of the following line? 
    int k = d.function1(); // With this, the need for virtual functions is gone, right? 

} 

मैं कॉम्पसी इंजीनियर नहीं हूं और मैं यह जानना चाहता हूं। वर्चुअल फ़ंक्शंस का उपयोग क्यों करें यदि हम बेस क्लास पॉइंटर्स से बच सकते हैं?व्युत्पन्न कक्षाओं के लिए बेस क्लास पॉइंटर्स का उपयोग क्यों करें

उत्तर

47

बहुरूपता की शक्ति वास्तव में एप नहीं है अपने सरल उदाहरण में किराया, लेकिन यदि आप इसे थोड़ा बढ़ाते हैं तो यह स्पष्ट हो सकता है।

class vehicle{ 
     ..... 
     virtual int getEmission(); 
} 

class car : public vehicle{ 
     int getEmission(); 
} 

class bus : public vehicle{ 
     int getEmission(); 
} 

int main() 
{ 
     car a; 
     car b; 
     car c; 
     bus d; 
     bus e; 

     vehicle *traffic[]={&a,&b,&c,&d,&e}; 

     int totalEmission=0; 

     for(int i=0;i<5;i++) 
     { 
      totalEmission+=traffic[i]->getEmission(); 
     } 

} 

यह आपको पॉइंटर्स की एक सूची के माध्यम से पुन: स्थापित करने देता है और अंतर्निहित प्रकार के आधार पर विभिन्न विधियों को बुलाया जाता है। असल में यह आपको कोड लिखने देता है जहां आपको यह जानने की आवश्यकता नहीं है कि बच्चे का प्रकार संकलन समय पर क्या है, लेकिन कोड वैसे भी सही कार्य करेगा।

+6

में पढ़ें यह संभवतः आभासी कार्यों के उपयोग के लिए सबसे अच्छे उदाहरणों में से एक है। धन्यवाद! – Garfield

+0

शानदार उत्तर, लेकिन जब मुझे पहले से ही पता है कि मुझे इन सभी वर्ग वस्तुओं के लिए उत्सर्जन जोड़ना है, तो मैं मैन्युअल रूप से 'कार' और 'बस' दोनों के लिए ऑब्जेक्ट्स क्यों नहीं बना सकता और उन्हें सामान्य रूप से जोड़ सकता हूं? मुझे बेस क्लास टाइप पॉइंटर की आवश्यकता क्यों है। – Yankee

+0

मान लीजिए कि क्या हम वर्चुअल फ़ंक्शन का उपयोग नहीं करते हैं, तो क्लास क्लास को बेस क्लास पॉइंटर्स का उपयोग करने का क्या फायदा है? – Rajesh

4

आप सही हैं, अगर आपके पास कोई ऑब्जेक्ट है जिसे आपको पॉइंटर के माध्यम से संदर्भित करने की आवश्यकता नहीं है। जब वस्तु को बनाए गए प्रकार के रूप में ऑब्जेक्ट नष्ट कर दिया जाएगा तो आपको वर्चुअल विनाशक की भी आवश्यकता नहीं है।

उपयोगिता तब आती है जब आप किसी ऑब्जेक्ट को किसी अन्य ऑब्जेक्ट से पॉइंटर प्राप्त करते हैं, और आप वास्तव में नहीं जानते कि सबसे व्युत्पन्न प्रकार क्या है। आपके पास एक ही आधार पर बनाए गए दो या दो से अधिक व्युत्पन्न प्रकार हो सकते हैं, और एक ऐसा फ़ंक्शन है जो बेस प्रकार पर पॉइंटर लौटाता है। वर्चुअल फ़ंक्शंस आपको ऑब्जेक्ट को नष्ट करने का समय होने तक, जो व्युत्पन्न प्रकार आप उपयोग कर रहे हैं, इस बारे में चिंता किए बिना पॉइंटर का उपयोग करने की अनुमति देगा। आभासी विनाशक वस्तु को नष्ट कर देगा, यह जानने के बिना कि कौन सी व्युत्पन्न कक्षा इसके अनुरूप है।

यहाँ आभासी कार्यों का उपयोग करने का सबसे सरल उदाहरण है:

base *b = new derived; 
b->function1(); 
delete b; 
+1

मुझे लगता है कि उनका सवाल था कि बेस क्लास पॉइंटर्स का उपयोग क्यों करें और क्यों नहीं आभासी विनाशक –

1

अपने बहुरूपता लागू करने के लिए। जब तक आपके पास बेस क्लास पॉइंटर व्युत्पन्न ऑब्जेक्ट को इंगित नहीं करता है, तब तक आपके पास पॉलिमॉर्फिज्म नहीं हो सकता है।

One of the key features of derived classes is that a pointer to a derived class is type-compatible with a pointer to its base class. Polymorphism is the art of taking advantage of this simple but powerful and versatile feature, that brings Object Oriented Methodologies to its full potential.

In C++, a special type/subtype relationship exists in which a base class pointer or a reference can address any of its derived class subtypes without programmer intervention. This ability to manipulate more than one type with a pointer or a reference to a base class is spoken of as polymorphism.

Subtype polymorphism allows us to write the kernel of our application independent of the individual types we wish to manipulate. Rather, we program the public interface of the base class of our abstraction through base class pointers and references. At run-time, the actual type being referenced is resolved and the appropriate instance of the public interface is invoked. The run-time resolution of the appropriate function to invoke is termed dynamic binding (by default, functions are resolved statically at compile-time). In C++, dynamic binding is supported through a mechanism referred to as class virtual functions. Subtype polymorphism through inheritance and dynamic binding provide the foundation for objectoriented programming

The primary benefit of an inheritance hierarchy is that we can program to the public interface of the abstract base class rather than to the individual types that form its inheritance hierarchy, in this way shielding our code from changes in that hierarchy. We define eval(), for example, as a public virtual function of the abstract Query base class. By writing code such as _rop->eval(); user code is shielded from the variety and volatility of our query language. This not only allows for the addition, revision, or removal of types without requiring changes to user programs, but frees the provider of a new query type from having to recode behavior or actions common to all types in the hierarchy itself. This is supported by two special characteristics of inheritance: polymorphism and dynamic binding. When we speak of polymorphism within C++, we primarily mean the ability of a pointer or a reference of a base class to address any of its derived classes. For example, if we define a nonmember function eval() as follows, // pquery can address any of the classes derived from Query void eval(const Query *pquery) { pquery->eval(); } we can invoke it legally, passing in the address of an object of any of the four query types:

int main() 
{ 
AndQuery aq; 
NotQuery notq; 
OrQuery *oq = new OrQuery; 
NameQuery nq("Botticelli"); // ok: each is derived from Query 
// compiler converts to base class automatically 
eval(&aq); 
eval(&notq); 
eval(oq); 
eval(&nq); 
} 

जबकि एक वस्तु का पता के साथ eval() को लागू करने की एक प्रयास एक संकलन समय त्रुटि में क्वेरी परिणाम से प्राप्त नहीं:

int main() 
{ string name("Scooby-Doo"); // error: string is not derived from Query 
eval(&name); 
} 

Within eval(), the execution of pquery->eval(); must invoke the appropriate eval() virtual member function based on the actual class object pquery addresses. In the previous example, pquery in turn addresses an AndQuery object, a NotQuery object, an OrQuery object, and a NameQuery object. At each invocation point during the execution of our program, the actual class type addressed by pquery is determined, and the appropriate eval() instance is called. Dynamic binding is the mechanism through which this is accomplished. In the object-oriented paradigm, the programmer manipulates an unknown instance of a bound but infinite set of types. (The set of types is bound by its inheritance hierarchy. In theory, however, there is no limit to the depth and breadth of that hierarchy.) In C++ this is achieved through the manipulation of objects through base class pointers and references only. In the object-based paradigm, the programmer manipulates an instance of a fixed, singular type that is completely defined at the point of compilation. Although the polymorphic manipulation of an object requires that the object be accessed either through a pointer or a reference, the manipulation of a pointer or a reference in C++ does not in itself necessarily result in polymorphism. For example, consider

// no polymorphism 
    int *pi; 
// no language-supported polymorphism 
    void *pvi; 
// ok: pquery may address any Query derivation 
    Query *pquery; 

In C++, polymorphism exists only within individual class hierarchies. Pointers of type void* can be described as polymorphic, but they are without explicit language support — that is, they must be managed by the programmer through explicit casts and some form of discriminant that keeps track of the actual type being addressed.

+0

अंतिम भाग सी ++ प्राइमर से हैं! उस पुस्तक को प्राप्त करें और पॉलीमोर्फिज्म –

0

आप दो प्रश्न पूछे गए हैं (शीर्षक में और अंत में) लगता है:

  1. क्यों व्युत्पन्न वर्ग के लिए उपयोग आधार वर्ग संकेत दिए गए? यह बहुरूपता का बहुत उपयोग है। यह आपको विशिष्ट कार्यान्वयन की अनुमति देते हुए वस्तुओं को समान रूप से इलाज करने की अनुमति देता है। अगर यह आपको परेशान करता है, तो मुझे लगता है कि आपको पूछना चाहिए: बहुरूपता क्यों?

  2. वर्चुअल विनाशकों का उपयोग क्यों करें यदि हम बेस क्लास पॉइंटर्स से बच सकते हैं? यहां समस्या है, आप पॉलिमॉर्फिज्म की शक्ति का फायदा उठाने के लिए बेस क्लास पॉइंटर्स से हमेशा नहीं बच सकते हैं।

संबंधित मुद्दे