why is only my first class working? when i run this it just asks about the viruses and not the cost of the OS.

#include <iostream>
using namespace std;


class OperatingSystem
{
private:
float cost;
public:
OperatingSystem() : cost(0) {}
float getCost() { return cost; }
void setCost(float cost) { OperatingSystem::cost = cost; }
void inputInfo();
void outputInfo();
};

class Windows : public OperatingSystem
{
private:
unsigned long numberofviruses;
public:
Windows() : OperatingSystem(), numberofviruses(0) {}
unsigned long getNumberofviruses() { return numberofviruses; }
void setNumberofviruses(unsigned long numberofviruses)
{ Windows::numberofviruses = numberofviruses; }
void inputInfo();
void outputInfo();
};

int main()
{
Windows variable1;

variable1.inputInfo();
variable1.outputInfo();

return(0);
}

void Windows::inputInfo()
{
unsigned long temp;
cout << "Enter the number of viruses Windows currently has: ";
cin >> temp;
setNumberofviruses(temp);
}

void Windows::outputInfo()
{
cout << "Windows current has " << getNumberofviruses()
<< " known viruses." << endl;
}

void OperatingSystem::inputInfo()
{
float temp;
cout << "Enter the cost of the Operating System: ";
cin >> temp;
setCost(temp);
}

void OperatingSystem::outputInfo()
{
cout << "Your Operating System cost $" << getCost() << endl;
}

Recommended Answers

All 2 Replies

Although I am not an inheritance expert, I have a feeling your inputInfo() outputInfo() functions of your parent class need to be labeled 'virtual' because they are overridden in the child class.

The main method reads like

int main()
{
Windows variable1;
variable1.inputInfo();
variable1.outputInfo();
return(0);
}

You have created an instance of Windows class in which you have overridden the inputInfo method inherited from the Operating systems class. What you need to do is:

void Windows::inputInfo()
      {
      unsigned long temp;
      cout << "Enter the number of viruses Windows currently has: ";
      cin >> temp;
      setNumberofviruses(temp);
      OperatingSystem::inputInfo();
      }

int main()
{
      Windows variable1;  
      variable1.inputInfo();
      variable1.outputInfo();
      cout<<variable1.getCost()<<endl;
      system("pause");
      return 0;
}

That should work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.