Home > Enterprise >  Initializing C class attribute from another class
Initializing C class attribute from another class

Time:10-09

Currently, I am trying to implement what I have learned so far: OOP class in C . Here I have two different classes: VehicleInfo and Vehicle. Here down below is my written code:

#include <iostream>
#include <string>
#include <fstream>
#include <math.h>
#define M_PI  3.1416
using namespace std;

class VehicleInfo{
    public:
        string brand;
        bool electric;
        int catalogue_price;
        float tax_percentage = 0.05;

        VehicleInfo(string brand, bool electric, int catalogue_price){
            VehicleInfo::brand = brand;
            VehicleInfo::electric = electric;
            VehicleInfo::catalogue_price = catalogue_price;
        }

        float compute_tax(){
            if (VehicleInfo::electric == true){
                tax_percentage = 0.02;
                }
            return VehicleInfo::catalogue_price * tax_percentage;
        }

        void print_vehicle_info(){
            std::cout << "Brand : " << brand << std::endl;
            std::cout << "Payable Tax : " << compute_tax() << std::endl;
        }
};

class Vehicle{
    public:
        string id;
        string license_plate;
        VehicleInfo vehicle;
        
        Vehicle(string id, string license_plate, VehicleInfo vehicle){
            Vehicle::id = id;
            Vehicle::license_plate = license_plate;
            Vehicle::vehicle = vehicle;
        }

        string getName(){
            return Vehicle::vehicle.brand;
        }

        int getTax(){
            return Vehicle::vehicle.compute_tax();
        }

        void print_vehicle(){
            std::cout << "ID : " << id << std::endl;
            std::cout << "License Plate : " << license_plate << std::endl;
            std::cout << "Brand : " << getName() << std::endl;
            std::cout << "Tax : " << getTax() << std::endl;
        }

};


int main(int argc, char const *argv[])
{
    cout << endl;
    VehicleInfo unit1 = VehicleInfo("Tesla Model 3", true, 60000);
    unit1.print_vehicle_info();

    Vehicle unit2 = Vehicle("YU2314", "KL0932", unit1);
    unit2.print_vehicle();

    std::cin.get();
    return 0;
}


What I expect from the code is that the attribute 'vehicle' in class Vehicle would initialize using attribute and function 'compute_tax()' from class VehicleInfo. Error code I received showed like this:

cohesion_coupling.cpp: In constructor 'Vehicle::Vehicle(std::__cxx11::string, std::__cxx11::string, VehicleInfo)':
cohesion_coupling.cpp:42:70: error: no matching function for call to 'VehicleInfo::VehicleInfo()'
         Vehicle(string id, string license_plate, VehicleInfo vehicle){
                                                                      ^
cohesion_coupling.cpp:17:9: note: candidate: 'VehicleInfo::VehicleInfo(std::__cxx11::string, bool, int)'
         VehicleInfo(string brand, bool electric, int catalogue_price){
         ^~~~~~~~~~~
cohesion_coupling.cpp:17:9: note:   candidate expects 3 arguments, 0 provided
cohesion_coupling.cpp:10:7: note: candidate: 'VehicleInfo::VehicleInfo(const VehicleInfo&)'
 class VehicleInfo{
       ^~~~~~~~~~~
cohesion_coupling.cpp:10:7: note:   candidate expects 1 argument, 0 provided
cohesion_coupling.cpp:10:7: note: candidate: 'VehicleInfo::VehicleInfo(VehicleInfo&&)'
cohesion_coupling.cpp:10:7: note:   candidate expects 1 argument, 0 provided

Any suggestion to improve the code (or perhaps some corrections needed on class practices that I have done) ?

CodePudding user response:

In your Vehicle constructor, the compiler first has to default-construct all the member variables. Only after that will the code in the constructor run and Vehicle::vehicle = vehicle; will get called. So your vehicle member variable will first get default-constructed and then assigned a value.

But your VehicleInfo does not have a default constructor, which is the error you're getting.

To get around this, use member initializers like so:

Vehicle(string id, string license_plate, VehicleInfo vehicle) :
    id(id),
    license_plate(license_plate),
    vehicle(vehicle)
{
}
  • Related