Note that there are some explanatory texts on larger screens.

plurals
  1. POBase class with 2 derived classes - How to construct correctly?
    primarykey
    data
    text
    <p>I have 3 classes. Let's call them Class A, class B and class C.</p> <p>Class C inherits from Class B and class B inherits from class A. (There are other classes as well which inherit from class B)</p> <pre><code>EG classC -&gt; classB -&gt; classA classD -&gt; classB -&gt; classA classE -&gt; classB -&gt; classA </code></pre> <p>I want to create a class C object. </p> <p>Class A has a variable 'name' Class B has a variable 'choice'</p> <p>I want the constructor of class C objects to create the objects with a given name.</p> <p>This is how I've done my .h files (which obviously doesn't work) These extracts are from the relevant .h files. I have a .h file and a .cpp file for each class.</p> <pre><code>class classC: public classB { public: classC(string name):classA(name){} }; class classB : public classA { public: classB(string name); void setChoice(); string getChoice(); protected: string choice; }; class classA { public: classA(); classA(string name); string getName(); virtual void setChoice(); protected: string name; }; </code></pre> <p>The extracts from the .cpp files are as follows:</p> <p><em><strong></em> ClassC **</strong></p> <pre><code>classC::classC(string name) {} void classC::setChoice() { choice = "P"; } </code></pre> <p><em><strong></em> ClassB **</strong></p> <pre><code>classB::classB(string name):classA(name){} string classB::getChoice(){ return choice; } void classB::setchoice(string newChoice){ choice = newChoice; } </code></pre> <p><strong>* ClassA*</strong></p> <pre><code>classA::classA() {} classA::classA(string aName):name(aName){} string classA::getName() { return name; } void classA::setSelection() {} </code></pre> <p>Driver:</p> <pre><code>classC* classC1 = new classC("tomato"); class1C-&gt;setChoice(); cout &lt;&lt; "Name is " &lt;&lt; classC1-&gt;getName() &lt;&lt; endl; cout &lt;&lt; "Choice is " &lt;&lt; classC1-&gt;getChoice() &lt;&lt; endl; </code></pre> <p>I have a setChoice method set up as virtual as I want each class from Class C - Class Z to have their own specific setChoice function. The specifications of this project also state that this function must be declared in ClassA.</p> <p>I'm getting an error stating that ClassB expects 1 argument, and none is provided.</p> <p>Can somebody please point me int he right direction. I'm out of ideas.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload