Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

need help on c++ program please here\'s code: The C++ classes VBase and VDerived

ID: 3826986 • Letter: N

Question

need help on c++ program please

here's code:

The C++ classes VBase and VDerived that are shown on page 3 are also available on the WyoCourses site in the file Lab13.cpp (link on the assignment page). These two form a simple base and derived class pair. We begin with this program and investigate simple modifications and additions to it. 1. Construct a new Microsoft Visual Studio Win32 Console Application project with this file as the sole content. Build and run the program, verifying that the console output is simply Object(3) 2. Open a new text document (e.g. in Notepad, Lab13Test.txt) and generate a set of answers to the questions that follow. Question #1: The base class VBase is an "abstract" class. Why is this true? HINT: attempt to add the following code line to the body of main() VBase B and note the messages provided by the compiler. NOTE: remove this line after you try it!

Explanation / Answer

Answers:
#Q1: The console output is verified to be Object(3)
#Q2: VBase is an abstract class as it contains a pure virtual function virtual void makeAbs() const = 0;
Pure virtual functions begin with keywird 'virtual' and end with 0.
Error message provided by the compiler:

error: cannot declare variable 'v' to be of abstract type 'VBase' VBase v;   
because the following virtual functions are pure within 'VBase':
virtual void VBase::makeAbs() const

#Q3:
(a) and (b): int getStuff() const {return stuff*stuff;} as a public method in VDerived doesnot compile.
Error is caused as 'stuff' is private. Error message:
error: 'int VBase::stuff' is private
int stuff;
^
prog.cpp:37:31: error: within this context
int getStuff()const {return stuff*stuff;}
^
prog.cpp:20:6: error: 'int VBase::stuff' is private
int stuff;
^
  
(c): Fix for this: Make stuff 'protected' in VBase class. New output is Object(9)
   protected: int stuff;

  
#Q4: Last challenge:

Code:

#include <iostream>
using std::cout;
using std::endl;
using std::ostream;

class VBase {
   public:
       VBase (int vs = 0)
       {
           setStuff(vs);
       }
       virtual int getStuff()const {return stuff;}
       virtual void setStuff(int s)
       {
       stuff = (s>=0) ? s:0;
       }

private:
   virtual void makeAbs() const=0;
  
   //This gives access to 'stuff' in the derived class
protected:
   int stuff;

};


class VDerived:public VBase
{

public:
   VDerived(int vs=0){ setStuff(vs);}
  
       //setStuff changes the value of weight everytime it is called
       virtual void setStuff(int s)
       {
       stuff = (s>=0) ? s:0;
       weight = stuff*2;
       }

private:
   virtual void makeAbs() const {}
           //New member weight
   int weight;
           //getStuff now returns stuff*weight
   int getStuff()const {return stuff*weight;}
};

ostream& operator <<(ostream& out,const VBase& right)
{

   out<<"Object("<<right.getStuff()<<")";
   return out;

}
  
int main()
{


   VDerived A(3);
   cout<<A<<endl;
   A.setStuff(4);
   cout<<A<<endl;

   return 0;

}