Vehicle Management and Shape Calculation Systems

Vehicle Toll Calculation System

Problem: Develop a system to manage toll collection for various types of vehicles. Implemant the following base class framework:

class Vehicle
{
protected:
    string registrationNumber;
public:
    Vehicle(string regNum) {
        registrationNumber = regNum;
    }
    virtual int calculateToll() = 0; // Calculate toll fee
};

Using Vehicle as the base class, construct Car, Truck, and Bus classes with the following toll calculation formulas:

  • Car: passengerCount * 8 + weight * 2
  • Truck: weight * 5
  • Bus: passengerCount * 3

Create these classes and implement a main function that:

  • Dynamically creates Car, Truck, or Bus objects based on input
  • For Car: requires passenger count and weight
  • For Truck: requires weight
  • For Bus: requires passenger count

Input format:

  • First line: number of test cases
  • Subsequent lines: vehicle information (type, registration number, and relevant attributes)

Output: Registration number and toll fee for each vehicle.

Sample Solution:

class Car : public Vehicle {
private:
    int passengerCount;
    int weight;
public:
    Car(string regNum, int passengers, int w) : Vehicle(regNum) {
        passengerCount = passengers;
        weight = w;
    }
    
    virtual int calculateToll() override {
        return passengerCount * 8 + weight * 2;
    }
};

class Truck : public Vehicle {
private:
    int weight;
public:
    Truck(string regNum, int w) : Vehicle(regNum) {
        weight = w;
    }
    
    virtual int calculateToll() override {
        return weight * 5;
    }
};

class Bus : public Vehicle {
private:
    int passengerCount;
public:
    Bus(string regNum, int passengers) : Vehicle(regNum) {
        passengerCount = passengers;
    }
    
    virtual int calculateToll() override {
        return passengerCount * 3;
    }
};

int main() {
    int testCases, vehicleType;
    string regNumber;
    
    cin >> testCases;
    
    for (int i = 0; i < testCases; i++) {
        cin >> vehicleType >> regNumber;
        
        switch (vehicleType) {
            case 1: {
                int passengers, weight;
                cin >> passengers >> weight;
                Car vehicle(regNumber, passengers, weight);
                cout << regNumber << " " << vehicle.calculateToll() << endl;
                break;
            }
            case 2: {
                int weight;
                cin >> weight;
                Truck vehicle(regNumber, weight);
                cout << regNumber << " " << vehicle.calculateToll() << endl;
                break;
            }
            case 3: {
                int passengers;
                cin >> passengers;
                Bus vehicle(regNumber, passengers);
                cout << regNumber << " " << vehicle.calculateToll() << endl;
                break;
            }
        }
    }
    
    return 0;
}

Shape Area Calculation System

Problem: Design Shape, Circle, and Rectangle classes based on the inheritance relationship shown below to calculate and output the areas of rectangles and circles.

Requirements:

  • Shape's destructor and area() function should be virtual
  • Implement proper inheritance hierarchy
  • Calculate areas correctly for both shapes

Sample Solutoin:

class Shape {
public:
    Shape() {}
    virtual ~Shape() {}
    virtual float area() = 0;
};

class Circle : public Shape {
private:
    float centerX, centerY;
    float radius;
public:
    Circle(float x, float y, float r) {
        centerX = x;
        centerY = y;
        radius = r;
    }
    
    virtual ~Circle() {}
    
    virtual float area() override {
        return 3.1415926f * radius * radius;
    }
};

class Rectangle : public Shape {
private:
    float width, height;
public:
    Rectangle(float w, float h) {
        width = w;
        height = h;
    }
    
    virtual ~Rectangle() {}
    
    virtual float area() override {
        return width * height;
    }
};

int main() {
    Shape* shapes[2];
    float w, h;
    cin >> w >> h;
    shapes[0] = new Rectangle(w, h);
    
    float r;
    cin >> r;
    shapes[1] = new Circle(0.0f, 0.0f, r);
    
    printf("Area of rectangle: %.2f\n", shapes[0]->area());
    printf("Area of circle: %.2f\n", shapes[1]->area());
    
    for (int i = 0; i < 2; i++) {
        delete shapes[i];
    }
    
    return 0;
}

Tags: Object-Oriented Programming Inheritance Polymorphism C++ vehicle management

Posted on Wed, 21 Oct 2026 16:17:46 +0000 by Mick33520