0
Help ! I still can't solve it
Abstract Classes You are given a Vehicle base class, and two subclasses, Car and Bicycle. Both classes have a drive() method whi #include <iostream> using namespace std; class Vehicle { //complete the class definition }; class Car: public Vehicle { public: void drive() { cout << "Driving a car!"<<endl; } }; class Bicycle: public Vehicle { public: void drive() { cout << "Driving a bicycle!"<<endl; } }; int main() { Car c; Bicycle b; Vehicle *v1 = &c; Vehicle *v2 = &b; v1->drive(); v2->drive(); return 0; }
2 Answers
+ 2
What do you mean? Please provide full information of your question, with a link to your code bits as attempt, not copy paste your code here.
+ 1
You have to do class defination like this so other class can access their properties.
#include <iostream>
using namespace std;
class Vehicle {
//complete the class definition
public:
virtual void drive()=0;
};
class Car: public Vehicle {
public:
void drive() {
cout << "Driving a car!"<<endl;
}
};
class Bicycle: public Vehicle {
public:
void drive() {
cout << "Driving a bicycle!"<<endl;
}
};
int main()
{
Car c;
Bicycle b;
Vehicle *v1 = &c;
Vehicle *v2 = &b;
v1->drive();
v2->drive();
return 0;
}