के लिए अनिर्धारित संदर्भ संपादित करें: नीचे दिए गए निश्चित कोड को पोस्ट करें। आपकी मदद के लिए सभी को शुक्रिया!सी ++ वर्ग विरासत, 'कक्षा :: कन्स्ट्रक्टर()'
मैं सिर्फ सी ++ सीख रहा हूं और विरासत में परेशानी हो रही हूं। मैंने खोजा और खोजा है और कुछ भी करने की कोशिश की है लेकिन मैं इस कोड को संकलित करने के लिए संकलित नहीं कर सकता हूं, जो कार्यक्षमता को बनाए रखना है।
मुझे लगता है कि मैं एक बेवकूफ गलती कर रहा हूं या शायद मुझे कुछ बड़ी अवधारणा याद आ रही है, लेकिन अगर कोई इसे देख सके तो मैं वास्तव में इसकी सराहना करता हूं!
यदि मैं ऑब्जेक्ट बनाने वाले स्टारसिस्टम कन्स्ट्रक्टर में 3 लाइनों पर टिप्पणी करता हूं, तो यह संकलित करता है, इसलिए मुझे पता है कि इस मुद्दे को करना है।
#include <iostream>
#include <vector>
#include <string>
#include <stdlib.h>
#include <time.h>
using namespace std;
class SystemBody
{
public:
SystemBody();
int systembodyindex;
int starsystemindex;
SystemBody(int systembodyindex, int starsystemindex)
{
cout << "StarSystem " << starsystemindex << ": creating empty SystemBody " << systembodyindex << endl;
}
};
class Star : public SystemBody
{
public:
Star();
string startype;
Star(int systembodyindex, int starsystemindex)
{
cout << "StarSystem " << starsystemindex << ": converting empty SystemBody into Star " << systembodyindex << endl;
}
};
class Planet : public SystemBody
{
public:
Planet();
string planettype;
Planet(int systembodyindex, int starsystemindex)
{
cout << "StarSystem " << starsystemindex << ": converting empty SystemBody into Planet " << systembodyindex << endl;
}
};
class ExitNode : public SystemBody
{
public:
ExitNode();
vector<int> connectedindexlist;
ExitNode(int systembodyindex, int starsystemindex)
{
cout << "StarSystem " << starsystemindex << ": converting empty SystemBody into Exit Node " << systembodyindex << endl;
}
};
class StarSystem
{
public:
StarSystem();
int starsystemindex;
vector<StarSystem> connectedlist;
vector<Planet> planetlist;
StarSystem(int index)
{
starsystemindex = index;
cout << "--Creating StarSystem: " << starsystemindex << endl;
int numberofbodies = (rand() % 4) + 2;
for (int i = 0; i < numberofbodies; i +=1)
{
if (i == 0)
{
Star body(i, starsystemindex);
}
else if (i == numberofbodies)
{
ExitNode body(i, starsystemindex);
}
else
{
Planet body(i, starsystemindex);
}
}
}
void addConnection(StarSystem connectedstarsystem)
{
cout << "--StarSystem " << starsystemindex << ": Adding connection to StarSystem " << connectedstarsystem.starsystemindex << endl;
connectedlist.push_back(connectedstarsystem);
}
};
int main()
{
srand(time(0));
StarSystem starsystem0(0);
return 0;
}
संपादित करें:
आपकी मदद के लिए हर किसी के लिए धन्यवाद! अगर भविष्य में किसी भी को यह उपयोगी लगे तो बस यहां निश्चित कोड पोस्ट कर रहा है।
#include <iostream>
#include <vector>
#include <string>
#include <stdlib.h>
#include <time.h>
using namespace std;
class SystemBody
{
public:
int systembodyindex;
int starsystemindex;
SystemBody ()
{
cout << "----SystemBody BEING CREATED WITH NO PARAMETERS" << endl;
}
SystemBody (int bodyindex, int systemindex)
{
systembodyindex = bodyindex;
starsystemindex = systemindex;
cout << "----StarSystem " << starsystemindex << ": creating empty SystemBody " << systembodyindex << endl;
}
};
class Star : public SystemBody
{
public:
Star (int bodyindex, int systemindex) : SystemBody (bodyindex, systemindex)
{
cout << "----StarSystem " << starsystemindex << ": converting empty SystemBody into Star " << systembodyindex << endl;
}
};
class Planet : public SystemBody
{
public:
Planet (int bodyindex, int systemindex) : SystemBody (bodyindex, systemindex)
{
cout << "----StarSystem " << starsystemindex << ": converting empty SystemBody into Planet " << systembodyindex << endl;
}
};
class ExitNode : public SystemBody
{
public:
ExitNode (int bodyindex, int systemindex) : SystemBody (bodyindex, systemindex)
{
cout << "----StarSystem " << starsystemindex << ": converting empty SystemBody into ExitNode " << systembodyindex << endl;
}
};
class StarSystem
{
public:
int starsystemindex;
vector<StarSystem> connectedlist;
vector<Planet> planetlist;
StarSystem (int index)
{
starsystemindex = index;
cout << "--Creating StarSystem: " << starsystemindex << endl;
int numberofbodies = (rand() % 4) + 2;
for (int i = 0; i <= numberofbodies; i +=1)
{
if (i == 0)
{
Star body(i, starsystemindex);
}
else if (i == numberofbodies)
{
ExitNode body(i, starsystemindex);
}
else
{
Planet body(i, starsystemindex);
}
}
}
};
int main()
{
srand(time(0));
StarSystem starsystem0(0);
return 0;
}
क्या आप यह निर्दिष्ट करना चाहते हैं कि आप कौन सी कार्यक्षमता चाहते हैं? – jepugs
मैं एक जेनेरिक सिस्टमबॉडी क्लास रखने की कोशिश कर रहा हूं, जिसमें सदस्य गुण और विधियां होंगी जो वंचित वर्ग स्टार, ग्रह और निकास के लिए आम हैं। – phimath
कन्स्ट्रक्टर में आप 'स्टार', 'ExitNode' और' Planet' ऑब्जेक्ट्स को स्टैक-आधारित स्वचालित चर के रूप में प्रारंभ कर रहे हैं - जैसे ही वे दायरे से बाहर निकल जाएंगे, इन सभी को हटा दिया जाएगा। क्या आप इन्हें किसी सूची या वेक्टर में जोड़ने के लिए हैं? –