Sumaira Samar
Sumaira Samar

Reputation: 73

my relation of aggregation, association and composition doesnt work in this C++ program?

Im trying to make aggregation between Passenger and Bus class, Composition between [wheel, door, engine] and Bus class, simple association of [car, bus] and Vehicle. Also Driver is associated with Car. But I dont know how to write it in C++ code. My program is correct syntactically but giving run-time errors. Please help me.

    #include<iostream>
#include<string>
using namespace std;

class Vehicle
{};

class Bus
{
    Door door;
    Wheel wheel;
    Engine engine;
    Passenger P;

    string busNo;

    int SeatingCapacity, SeatsReservedSofar;

public:
    int GetSeatingCapacity();
    void SetSeatingCapacity(int bCap);
    string GetBusNo();
    void SetBusNo(string bNo);
    void ReserveSeat(Passenger P); 
    void DisplayInfo();
};

class Passenger
{
    string Name;
    char Gender;

public:
    void SetPassengerName(string name);
    string GetPassengerName();
    void SetGender(char gender);
    char GetGender();
};

class Car
{};

class Driver
{

};

class Wheel
{

};

class Door
{

};

class Engine
{

};


////////////////////////////////////
//Definition of Functions
////////////////////////////////////
Bus::Bus()
{
cout<<"Creating a new bus..."<<endl;
}

void Bus::SetSeatingCapacity(int bCap)
{
    if(bCap>20 && bCap<50)
        SeatingCapacity=bCap;
}

void Bus::SetBusNo(string bNo)
{
    int len=bNo.length();
    if(len>2 && len<5)
        busNo=bNo;
}

void Passenger::SetPassengerName(string pname)
{
    int len=pname.length;
    if(len>0 && len<40)
        Name=pname;
}

void Passenger::SetGender(char pgender)
{
    if(pgender=='m' || pgender=='f')
        Gender=pgender;
}

void Bus::ReserveSeat(Passenger P)
{

    int capacity = GetSeatingCapacity(), rem;

    if(capacity != SeatsReservedSofar)
    {
        SeatsReservedSofar++;
        cout<<endl<<"Seat reserved for passenger";
        rem=capacity-SeatsReservedSofar;
        cout<<endl<<"Remaining Capacity : "<<rem;
    }
    else
    {
        cout<<endl<<"Seat reservation failed. No space left. ";
    }
}


int main()
{
Bus abus;

cout<<"Enter max capacity of bus for passengers : ";

int capacity;
cin>>capacity;
abus.SetSeatingCapacity(capacity);

cout<<endl<<"Enter the bus registration no : ";
string regno;
std::getline(std::cin,regno);
cin.ignore();
abus.SetBusNo(regno);

cout<<endl<<".....Bus is created successfully. "<<endl<<endl;

string pname;
Passenger passenger;
char gender;

cout<<"Enter the passenger name: ";
std::getline(std::cin, pname);
cin.ignore();
passenger.SetPassengerName(pname);
cout<<"Enter the passenger gender <m for male and f for female>";
cin>>gender;
passenger.SetGender(gender);
abus.ReserveSeat(passenger);

char quest;
for(quest='y'; quest == 'y'; )
{
cout<<endl<<endl<<"Do you want to add more passengers? (y/Y for yes and another character for no)? ";
char quest;
cin>>quest;
}

system("pause");

}

Upvotes: 0

Views: 420

Answers (1)

Adam M
Adam M

Reputation: 21

First of all I think that you program cannot compile, why?

  1. You've placed Passanger, Door, Wheel and Engine in Bus class. At moment of Bus class compilation those types are unknown to the compiler - you need to define them before Bus to use them in the Bus definition,
  2. There is no Bus constructor declared in the class definition, it is placed outside the class, it will not work like that, you need to add Bus() in the Bus class,
  3. in Passenger::SetPassengerName you forgot to place braces after length,
  4. you forgot to define GetSeatingCapacity() method,

If there is a reservation system maybe you can consider to use some kind of Seat class which will keep information about reservation, for example a Passanger pointer or something like that.

To keep information about multiple Passangers you may use something like std::vector< std::weak_ptr< Passanger> >

To have multiple Wheels - something like std::array< Wheel> - same for seats, std::array< Seat>

Upvotes: 2

Related Questions