Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagecpp
//  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();        
};

This C++ code is a header file named "Car.hpp" that contains the class definition for the Car class. It declares the member variables, member functions (including getters and setters), and constructors for the Car class.

Here's a breakdown of the code:

  • The code includes the necessary header files:

    • #include <stdio.h>: This header file is included but not used in the code. It can be removed.

    • #include <iostream>: This header file provides input/output operations, such as cout for outputting data to the console.

    • #include <string>: This header file is included to use the string data type and related functions.

  • The code uses the std namespace to avoid writing std:: before each standard library function. This allows the code to directly use functions like string without the std:: prefix.

  • The class declaration begins with the class Car line, which introduces the Car class.

  • The private: section defines the private members of the class. These are the member variables that can only be accessed within the class itself.

    • string make;: This member variable holds the make (brand) of the car.

    • string model;: This member variable holds the model of the car.

    • int year;: This member variable holds the year of the car.

    • double price;: This member variable holds the price of the car.

  • The public: section defines the public interface of the class. These are the member functions and member variables that can be accessed from outside the class.

  • The code declares the getter member functions for retrieving the values of the member variables:

    • string getMake() const;: This member function returns the make (brand) of the car.

    • string getModel() const;: This member function returns the model of the car.

    • int getYear() const;: This member function returns the year of the car.

    • double getPrice() const;: This member function returns the price of the car.

  • The code declares the setter member functions for modifying the values of the member variables:

    • void setMake(string pMake);: This member function sets the make (brand) of the car using the provided parameter.

    • void setModel(string pModel);: This member function sets the model of the car using the provided parameter.

    • void setYear(int pYear);: This member function sets the year of the car using the provided parameter.

    • void setPrice(double pPrice);: This member function sets the price of the car using the provided parameter.

  • The code declares the Print() member function, which prints the details of the car to the console.

  • The code declares multiple constructor functions for the Car class:

    • Car(string pMake, string pModel, int pYear, double pPrice);: This constructor initializes the Car object with the provided make, model, year, and price.

    • Car(string pMake, string pModel, int pYear);: This constructor initializes the Car object with the provided make, model, and year. The price is not provided and will be set to a default value.

    • Car();: This constructor is the default constructor and initializes the Car object with default values for make, model, year, and price.

Overall, this header file defines the structure and interface of the Car class, including its member variables, member functions, and constructors. It provides a blueprint for creating Car objects, accessing and modifying their attributes, and performing operations on them.

Car.cpp

Code Block
languagecpp
////  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;
}

This C++ code is the implementation file "Car.cpp" that corresponds to the Car class defined in the "Car.hpp" header file. It provides the definitions for the member functions of the Car class.

Here's a breakdown of the code:

  • The code includes the necessary header files:

    • #include "Car.hpp": This includes the corresponding header file for the Car class.

  • The code uses the std namespace to avoid writing std:: before each standard library function. This allows the code to directly use functions like cout without the std:: prefix.

  • The code defines the getter member functions to access the values of the member variables of the Car class. Each function returns the corresponding member variable:

    • string Car::getMake() const: This member function returns the make (brand) of the car.

    • string Car::getModel() const: This member function returns the model of the car.

    • int Car::getYear() const: This member function returns the year of the car.

    • double Car::getPrice() const: This member function returns the price of the car.

  • The code defines the setter member functions to modify the values of the member variables of the Car class. Each function assigns the provided parameter value to the corresponding member variable:

    • void Car::setMake(string pMake): This member function sets the make (brand) of the car using the provided parameter.

    • void Car::setModel(string pModel): This member function sets the model of the car using the provided parameter.

    • void Car::setYear(int pYear): This member function sets the year of the car using the provided parameter.

    • void Car::setPrice(double pPrice): This member function sets the price of the car using the provided parameter.

  • The code defines the constructor functions for the Car class. These constructors initialize the member variables of the Car object:

    • Car::Car(string pMake, string pModel, int pYear, double pPrice): This constructor takes four parameters and initializes the Car object with the provided make, model, year, and price.

    • Car::Car(string pMake, string pModel, int pYear): This constructor takes three parameters and initializes the Car object with the provided make, model, and year. The price is set to a default value of 0.0.

    • Car::Car(): This is the default constructor that initializes the Car object with default values for make, model, year, and price.

  • The code defines the Print() member function. This function prints the details of the car to the console using cout. It displays the make, model, year, and price of the car.

Overall, this implementation file provides the definitions for the member functions of the Car class, including getters, setters, constructors, and the Print() function. It implements the functionality specified in the class declaration and allows objects of the Car class to be created, modified, and printed.

main.cpp

Code Block
languagecpp
//  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;
}

...

This C++ code is a main program file named "main.cpp" that serves as a demo for the usage of the Car class and its multiple constructors.

Here's a breakdown of the code:

  • The code includes the necessary header files:

    • #include <iostream>: This header file provides input/output operations, such as cout for outputting data to the console.

    • #include "Car.hpp": This includes the user-defined header file "Car.hpp", which contains the class declaration for the Car class.

  • The code uses the std namespace to avoid writing std:: before each standard library function. This allows the code to directly use functions like cout without the std:: prefix.

  • The main() function is the entry point of the program. Execution of the program starts from here.

  • The program demonstrates the usage of multiple constructors of the Car class by creating Car objects and performing operations on them.

  • In the first example, an instance of the Car class named myCar is created using the constructor with four parameters. The constructor is called with the arguments "Honda", "Civic", 2020, and 15000.00, which initializes the myCar object with the provided values for make, model, year, and price.

  • The Print() member function of the myCar object is called to print the details of the car, including the make, model, year, and price.

  • In the second example, another instance of the Car class named myCarTwo is created using the constructor with three parameters. The constructor is called with the arguments "Ford", "Ranger", and 2021, which initializes the myCarTwo object with the provided values for make, model, and year. The price is not provided and is set separately using the setPrice() member function.

  • The setPrice() member function of the myCarTwo object is called to set the price of the car to 16500.

  • The Print() member function of the myCarTwo object is called to print the updated details of the car.

  • In the third example, another instance of the Car class named myCarThree is created using the default constructor. The constructor is called without any arguments, which initializes the myCarThree object with the default values specified in the constructor.

  • The member functions (setModel(), setMake(), setPrice(), setYear()) of the myCarThree object are called to modify the make, model, price, and year of the car.

  • The Print() member function of the myCarThree object is called to print the updated details of the car.

  • The program ends by returning 0 from the main() function, indicating successful execution.

Overall, this main program demonstrates the usage of the Car class and its multiple constructors by creating objects, setting and retrieving their attributes, and printing the details of the cars to the console.