Take the code below and modify it so that the Sport class becomes abstract. #inc
ID: 3555116 • Letter: T
Question
Take the code below and modify it so that the Sport class becomes abstract.
#include <iostream>
#include <string>
using namespace std;
class Sport
{
public:
Sport()
{
kind = "who knows";
}
void show()
{
cout << "this is " << kind << endl;
}
private:
string kind;
};
class BallSport : public Sport
{
public:
void set(string bt)
{
balltype = bt;
}
void show()
{
cout << "this is " << balltype << "ball ";
}
private:
string balltype;
};
int main()
{
//delcare an object
Sport sp;
//member function
sp.show();
//Create instance and call BallSport members
Ballsport bp;
bp.set("Let us do!");
bp.show();
//pause system for a while
system("pause");
return 0;
}
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
class Sport
{
public:
virtual void show() = 0; // pure virtual means class is abstract.
protected:
string kind;
};
class BallSport : public Sport
{
public:
void set(string bt)
{
balltype = bt;
}
void show()
{
cout << "this is " << balltype << "ball ";
}
private:
string balltype;
};
int main()
{
//delcare an object
//Sport sp; you cant create abstract class object...
//member function
//sp.show(); so this call is invalid.
//Create instance and call BallSport members
BallSport bp;
bp.set("Let us do!");
bp.show();
//pause system for a while
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.