cpp
#include <iostream>
#include <string>
using namespace std;
class MarineSensor {
private: // Hidden data
string sensorType;
double currentValue;
double minValue, maxValue;
bool isActive;
public: // Public methods
// Constructor - runs when object is created
MarineSensor(string type, double min, double max) {
sensorType = type;
minValue = min;
maxValue = max;
currentValue = 0.0;
isActive = false;
}
void activate() {
isActive = true;
cout << sensorType << " sensor ACTIVE" << endl;
}
void readValue(double newValue) {
if (!isActive) {
cout << "Error: Sensor inactive!" << endl;
return;
}
currentValue = newValue;
cout << sensorType << ": " << currentValue;
if (currentValue < minValue || currentValue > maxValue) {
cout << " [WARNING!]" << endl;
} else {
cout << " [OK]" << endl;
}
}
void displayInfo() {
cout << sensorType << " | Status: " << (isActive ? "ON" : "OFF");
cout << " | Value: " << currentValue << endl;
}
};
cpp
int main() {
// Create sensor objects
MarineSensor tempSensor("Engine Temp", 60.0, 90.0);
MarineSensor pressureSensor("Oil Pressure", 2.0, 6.0);
// Use the sensors
tempSensor.activate();
pressureSensor.activate();
tempSensor.readValue(75.5); // Normal
tempSensor.readValue(95.2); // Warning!
pressureSensor.readValue(4.2); // Normal
pressureSensor.readValue(1.5); // Warning!
return 0;
}
class MarineEngine {
private:
string engineModel;
int powerKW;
double currentRPM, maxRPM;
bool isRunning;
double fuelRate;
public:
MarineEngine(string model, int power, double maxRpm) {
engineModel = model;
powerKW = power;
maxRPM = maxRpm;
currentRPM = 0;
isRunning = false;
fuelRate = 0;
}
void start() {
if (isRunning) {
cout << "Already running!" << endl;
return;
}
isRunning = true;
currentRPM = 800; // idle RPM
fuelRate = powerKW * 0.01;
cout << engineModel << " STARTED at " << currentRPM << " RPM" << endl;
}
void stop() {
isRunning = false;
currentRPM = 0;
fuelRate = 0;
cout << engineModel << " STOPPED" << endl;
}
void setRPM(double rpm) {
if (!isRunning) {
cout << "Engine not running!" << endl;
return;
}
if (rpm > maxRPM) {
cout << "Max RPM exceeded!" << endl;
rpm = maxRPM;
}
currentRPM = rpm;
fuelRate = powerKW * 0.01 * (rpm / 1000.0);
cout << "RPM: " << currentRPM << " | Fuel: " << fuelRate << " L/h" << endl
}
void status() {
cout << engineModel << " | " << (isRunning ? "ON" : "OFF");
cout << " | " << currentRPM << " RPM" << endl;
}
};
cpp
int main() {
MarineEngine mainEngine("Caterpillar C32", 750, 2100);
mainEngine.start();
mainEngine.setRPM(1500);
mainEngine.setRPM(2200); // Will limit to maxRPM
mainEngine.status();
mainEngine.stop();
return 0;
}
cpp
class Motor {
private:
double speed; // Hidden
public:
void setSpeed(double s) {
if (s >= 0 && s <= 3000) {
speed = s;
}
}
double getSpeed() { return speed; }
};
cpp
// Base class
class Vehicle {
protected:
string name;
double speed;
public:
Vehicle(string n) : name(n), speed(0) {}
virtual void move() { cout << name << " moving" << endl; }
};
// Derived class
class Ship : public Vehicle {
private:
int propellers;
public:
Ship(string n, int props) : Vehicle(n), propellers(props) {}
void move() override {
cout << name << " sailing with " << propellers << " propellers" << endl;
}
void anchor() {
cout << name << " dropping anchor" << endl;
}
};
cpp
int main() {
Vehicle* vehicles[2];
vehicles[0] = new Vehicle("Generic");
vehicles[1] = new Ship("Cargo Ship", 2);
for(int i = 0; i < 2; i++) {
vehicles[i]->move(); // Different behavior for each
}
return 0;
}
class PIDController {
private:
double kp, ki, kd; // PID gains
double error, lastError, integral;
public:
PIDController(double p, double i, double d) : kp(p), ki(i), kd(d) {
error = lastError = integral = 0;
}
double calculate(double setpoint, double actual) {
error = setpoint - actual;
integral += error;
double derivative = error - lastError;
double output = kp * error + ki * integral + kd * derivative;
lastError = error;
return output;
}
void reset() {
error = lastError = integral = 0;
}
};
class EngineController {
private:
MarineEngine* engine;
PIDController* speedController;
double targetRPM;
public:
EngineController(MarineEngine* eng) {
engine = eng;
speedController = new PIDController(1.0, 0.1, 0.05);
targetRPM = 0;
}
void setTarget(double rpm) {
targetRPM = rpm;
cout << "Target RPM set to: " << rpm << endl;
}
void update(double currentRPM) {
double correction = speedController->calculate(targetRPM, currentRPM);
cout << "RPM correction: " << correction << endl;
// Apply correction to engine...
}
};
cpp
class AutomationSystem {
private:
MarineSensor tempSensor;
MarineSensor pressureSensor;
MarineEngine mainEngine;
bool emergencyShutdown;
public:
AutomationSystem() :
tempSensor("Engine Temp", 60, 90),
pressureSensor("Oil Pressure", 2, 6),
mainEngine("Main Engine", 1000, 2000),
emergencyShutdown(false) {}
void initialize() {
tempSensor.activate();
pressureSensor.activate();
mainEngine.start();
cout << "Automation system initialized" << endl;
}
void monitorAndControl() {
// Simulate sensor readings
tempSensor.readValue(85.0);
pressureSensor.readValue(4.5);
// Check for emergency conditions
if (/* temperature too high or pressure too low */) {
emergencyShutdown = true;
mainEngine.stop();
cout << "EMERGENCY SHUTDOWN!" << endl;
}
}
void systemStatus() {
tempSensor.displayInfo();
pressureSensor.displayInfo();
mainEngine.status();
}
};
bash
# Compile your C++ program
g++ -o program program.cpp
# Run the program
./program