Untitled
unknown
c_cpp
a year ago
2.0 kB
7
Indexable
#ifndef CARRENTAL_H
#define CARRENTAL_H
class CarRental {
private:
char make[50];
char model[50];
int year;
double rentalPricePerDay;
bool isAvailable;
public:
CarRental();
CarRental(const char* make, const char* model, int year, double rentalPricePerDay);
void displayInfo() const;
void rentCar();
void returnCar();
};
#endif
/////////////.cpp
#include "CarRental.h"
#include <iostream>
#include <cstring>
CarRental::CarRental() : year(0), rentalPricePerDay(0.0), isAvailable(true) {
strcpy(make, "Unknown");
strcpy(model, "Unknown");
}
CarRental::CarRental(const char* make, const char* model, int year, double rentalPricePerDay)
: year(year), rentalPricePerDay(rentalPricePerDay), isAvailable(true) {
strncpy(this->make, make, 49);
this->make[49] = '\0';
strncpy(this->model, model, 49);
this->model[49] = '\0';
}
void CarRental::displayInfo() const {
std::cout << "Make: " << make << ", Model: " << model << ", Year: " << year
<< ", Rental Price Per Day: $" << rentalPricePerDay
<< ", Availability: " << (isAvailable ? "Available" : "Not Available") << std::endl;
}
void CarRental::rentCar() {
if (isAvailable) {
isAvailable = false;
std::cout << "Car rented successfully." << std::endl;
} else {
std::cout << "Car is not available for rent." << std::endl;
}
}
void CarRental::returnCar() {
if (!isAvailable) {
isAvailable = true;
std::cout << "Car returned successfully." << std::endl;
} else {
std::cout << "Car was not rented." << std::endl;
}
}
/////.main.cpp//////////
#include "CarRental.h"
#include <iostream>
int main() {
CarRental car1("Toyota", "Camry", 2021, 50.0);
car1.displayInfo();
car1.rentCar();
car1.displayInfo();
car1.returnCar();
car1.displayInfo();
return 0;
}
Editor is loading...
Leave a Comment