I have a problem calling my showDetail() function and print the details as of now? how can I avoid the segmentation fault? So that I can further solve the actual problem i/p:
1,copter,101,22.77
3,copter,103,22.50
2,plane,202,22.86
2,copter,102,22.22
3,plane,203,22.55
1,copter,101,22.89
1,plane,201,22.67
3,copter,103,22.60
3,plane,203,23.01
2,plane,202,22.81
1,plane,201,22.68
2,copter,102,22.11
o/p:
copter 1,copter,101,22.89
2,copter,102,22.11
3,copter,103,22.60
plane: 1,plane,201,22.68
2,plane,202,22.81
3,plane,203,23.01
#include <bits/stdc++.h>
using namespace std;
//defining an Abstract Class Aircraft
class Aircraft{
//all members are set as protected so that they can be accessed by their child classes
protected:
int instanceId; //instance id showing which instance is that
string aircraftType; //type of aircraft either Copter or Plane
int refrenceId; //refrence id for the Aircraft
float curLocation; //location will held in this variable of the Aircraft
//all the functions & Constructor are ublic so that it can access the members of class
public:
//virtual keyword to implement the Abstract class (in c++)
virtual void setAircraftType(string aircraftType)=0;
//function to update the current Location of Aircraft
void updateLocation(float loc)
{
this->curLocation=loc;
}
void showDetails()
{
cout<<this->instanceId<<", "<<this->refrenceId<<", "<<this->curLocation<<endl;
}
};
//1st Subclass of Base class Aircraft i.e. Copter
class Copter:public Aircraft
{
public:
//overiding of virtual function
void setAircraftType(string aircraftType){
this->aircraftType=aircraftType;
}
Copter(int instanceId, string aircraftType, int refrenceId, float curLocation)
{
this->instanceId=instanceId;
//setAircraftType("copter");
this->aircraftType=aircraftType;
this->refrenceId=refrenceId;
this->curLocation=curLocation;
}
};
//2nd Subclass of Aircraft i.e. Plane class
class Plane:public Aircraft
{
public:
//overiding of virtual function
void setAircraftType(string aircraftType){
this->aircraftType=aircraftType;
}
Plane(int instanceId, string aircraftType, int refrenceId, float curLocation)
{
this->instanceId=instanceId;
//setAircraftType("plane");
this->aircraftType=aircraftType;
this->refrenceId=refrenceId;
this->curLocation=curLocation;
}
};
int main() {
int n;
cout<<"enter the number of record you want to input"<<endl;
cin>>n;
vector<Aircraft*> input_data(n);
for(int i = 0; i < n; i++)
{
int instanceId;
cout<<"Enter the Instance Id"<<endl;
cin>>instanceId;
cout<<"Enter the type of Aircraft"<<endl;
string typeOfAircraft;
cin>>typeOfAircraft;
cout<<"Enter the refrence id of Aircraft"<<endl;
int refId;
cin>>refId;
cout<<"Enter the current location of Aircraft";
float curLocation;
cin>>curLocation;
if(typeOfAircraft == "copter")
{
input_data.push_back(new Copter(instanceId, typeOfAircraft, refId, curLocation));
}
else
{
input_data.push_back(new Plane(instanceId, typeOfAircraft, refId, curLocation));
}
}
for(int i = 0; i < n; i++)
{
input_data[i]->showDetails();
}
}```
Aucun commentaire:
Enregistrer un commentaire