Answer to Question #344792 in C++ for Zain

Question #344792

write a program that show the working of tollplaza.the program will consist of vehicle class which has further two child(paying vehicle and non paying vehicle).Vehicle class should be having an account of all the vehicle that pass through the toll-plaza . paying vehicle has three type:

1)Type A(used for cars/jeeps)

2)Type B(used for Hiace/truck)

3)Type C(used for bus/heavy vehicles)

Type A has passing fee RS30/- Type B has passing fee RS50/- Type C has passing fee RS100/-

There is another class Person who is going to the entry but he needs to be authorized by assigning a unique id and password.

Note:

us static function,classes,constant,global variable where ever necessary.Output will begin by a Welcome message and then ask for authorization of the person. It should be having proper messages and the object should only be created at the time of use. Incorrect entry of person should only take 5 entries and then end/exit the program.


1
Expert's answer
2022-05-25T10:22:41-0400


#include <iostream>
#include <cstring>
#include <vector> 
using namespace std;


class Vehicle{
    private:
        static vector<char> passedVehicles;
    protected:
        Vehicle(char type){
            passedVehicles.push_back(type);
        }
};
class nonPayingVehicle: public Vehicle{
    public:
        nonPayingVehicle(): Vehicle('n'){
        }
};
class payingVehicle: public Vehicle{
    private:
        float fee;
    public:
        payingVehicle(char type): Vehicle(type){
            switch(type){
                case 'A': case 'a': fee = 30;
                case 'B': case 'b': fee = 30;
                case 'C': case 'c': fee = 30;
                default: fee = 0;
            }
        }
};
class Person{
    private:
        int id;
        string pass;
    public:
        Person(int person_id, string password){
            id = person_id;
            pass = password;
        }
        int getID(){
            return id;
        }
        bool validatePass(string password){
            if(password.compare(pass)==0)
                return true;
            return false;
        }
    
};




vector<char> Vehicle::passedVehicles;


int main()
{
    // test
    payingVehicle a('A'),b('B'),c('C');
    nonPayingVehicle d;
    //
    vector<Person>accounts;
    
    int entered_id;
    string entered_pass;
    
    start:           // TODO: make functions out of it
    cout<<"Welcome!"<<endl;
    cout<<"Enter id: ";
    cin>>entered_id;
    Person * ptr = nullptr;
    for(Person i:accounts){
        if(i.getID()==entered_id)
            ptr = &i;
    }
    
    if(ptr==nullptr){
        cout<<"ID not found! Register a new account."<<endl<<"ID: ";
        int registerID;
        string registerPass;
        cin>>registerID;
        cout<<"Enter password: ";
        cin>>registerPass;
        Person tempPerson(registerID, registerPass);
        accounts.push_back(tempPerson);
        
        goto start;
    }
    
    else{
        for(int i = 0; i< 6; i++){
            if(i==6){
                cout<<"Denied!"<<endl;
                return 0;
            }
            cout<<"Enter password: ";
            cin>>entered_pass;
            if(ptr->validatePass(entered_pass)){
                cout<<"Approved"<<endl;
                break;
            }
            cout<<"Wrong!"<<endl;
        }
    }


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS