"वर्ग के अंदर" (आई) विधि "कक्षा के बाहर" (ओ) विधि के रूप में ही है।
हालांकि, (i) का उपयोग तब किया जा सकता है जब कक्षा केवल एक फ़ाइल में उपयोग की जाती है (एक .cpp फ़ाइल के अंदर)। (ओ) का उपयोग हेडर फ़ाइल में होने पर किया जाता है। सीपीपी फाइलें हमेशा संकलित होती हैं। हेडर फाइल संकलित किए जाते हैं जब आप #include "header.h" का उपयोग करें।
यदि आप हेडर फ़ाइल में (i) का उपयोग करते हैं, तो फ़ंक्शन (Fun1) प्रत्येक बार जब आप "header.h शामिल" शामिल करेंगे, घोषित किया जाएगा। यह एक ही समारोह को कई बार घोषित कर सकता है। संकलन करना मुश्किल है, और त्रुटियों का भी कारण बन सकता है। सही उपयोग के लिए
उदाहरण:
File1: "Clazz.h"
//This file sets up the class with a prototype body.
class Clazz
{
public:
void Fun1();//This is a Fun1 Prototype.
};
File2: "Clazz.cpp"
#include "Clazz.h"
//this file gives Fun1() (prototyped in the header) a body once.
void Clazz::Fun1()
{
//Do stuff...
}
file3: "UseClazz.cpp"
#include "Clazz.h"
//This file uses Fun1() but does not care where Fun1 was given a body.
class MyClazz;
MyClazz.Fun1();//This does Fun1, as prototyped in the header.
फ़ाइल 4: "AlsoUseClazz.cpp"
#include "Clazz.h"
//This file uses Fun1() but does not care where Fun1 was given a body.
class MyClazz2;
MyClazz2.Fun1();//This does Fun1, as prototyped in the header.
File5: "DoNotUseClazzHeader.cpp"
//here we do not include Clazz.h. So this is another scope.
class Clazz
{
public:
void Fun1()
{
//Do something else...
}
};
class MyClazz; //this is a totally different thing.
MyClazz.Fun1(); //this does something else.
शायद सी ++ पर एक शुरुआती पुस्तक क्रम में हो सकती है? –
@ डाउनवॉटर क्यों? मेरे प्रश्न में क्या गलत है? – JohnJohnGa
वास्तव में यहां 3 विकल्प हैं। आपके दूसरे उदाहरण में हेडर फ़ाइल में फ़ंक्शन परिभाषा हो सकती है (लेकिन अभी भी रेखांकित नहीं है), या एक अलग '.cpp' फ़ाइल में। –