Car.hpp
// Car.hpp // Car Class definition// // Created by Kevin Roark on 3/26/23 #ifndef Car_hpp #define Car_hpp #include <stdio.h> #include <iostream> #include <string> using namespace std; #endif /* Car_hpp */ class Car { //variables private: string make; string model; int year; double price; public: //getters string getMake() const; string getModel() const; int getYear() const ; double getPrice() const; //setters void setMake(string pMake); void setModel(string pModel); void setYear(int pYear); void setPrice(double pPrice); //functions void Print(); //constructors Car(string pMake, string pModel, int pYear, double pPrice); Car(string pMake, string pModel, int pYear); Car(); };
Car.cpp
//// Car.cpp // Car Class implementation // Created by Kevin Roark on 3/26/23 #include "Car.hpp" #include <string> #include <iomanip> using namespace std; /* Getters (Assessors) */ string Car::getMake() const { return make; } string Car::getModel() const { return model; } int Car::getYear() const { return year; } double Car::getPrice() const { return price; } /* setters (Mutators) */ void Car::setMake(string pMake) { make = pMake; } void Car::setModel(string pModel) { model = pModel; } void Car::setYear(int pYear) { year = pYear; } void Car::setPrice(double pPrice) { price = pPrice; } /* Constructors - with Example of an Overloaded Constructor */ Car::Car(string pMake, string pModel, int pYear, double pPrice) { make = pMake; model = pModel; year = pYear; price = pPrice; } Car::Car(string pMake, string pModel, int pYear) { make = pMake; model = pModel; year = pYear; price = 0.0; } Car::Car() { make = "Unknown Make"; model = "Unknown Model"; year = 2023; price = 0.0; } //functions void Car::Print() { cout << "Make: " << make << endl; cout << "Model: " << model << endl; cout << "Year: " << year << endl; cout << showpoint << fixed << setprecision(2); cout << "Price: $" << price << endl; }
main.cpp
// main.cpp // Car Demo of multiple constructors// // Created by Kevin Roark on 3/26/23. #include <iostream> #include "Car.hpp" using namespace std; int main() { cout << "Overloaded Constructor Example\n"; Car myCar("Honda", "Civic", 2020, 15000.00); myCar.Print(); cout << endl; Car myCarTwo("Ford", "Ranger", 2021); myCarTwo.setPrice(16500); myCarTwo.Print(); cout << endl; Car myCarThree; myCarThree.setModel("BMW"); myCarThree.setMake("X3"); myCarThree.setPrice(26500); myCarThree.setYear(2019); myCarThree.Print(); cout << endl; return 0; }