i wrote class , object program , giving me errors can't around. here few errors can't around, found in main.cpp following errors....
car car1("ford", "crown victoria", 1997); car car2("chevrolet", "camaro"); car car3("hyundai", "sonata", -15); 126 intellisense: no instance of constructor "car::car" matches argument list argument types are: (const char [5], const char [15], int)
and
car1.setvalue("flinstones", "rock car", -2100); car3.setvalue("toyota", "camry", 2005); 132 intellisense: reference of type "std::string &" (not const-qualified) cannot initialized value of type "const char [6]"
here code
// -----------------------car.h----------------------- #include <iostream> #include <string> class car{ public: car(string &make, string &model, int year=2015); // constructor 3 parameters string getmake(); string getmodel(); int getyear(); int getspeed(); bool setvalue(string &make, string &model, int year); // set values parameters bool accelerate(char a); bool brake(char b); void display(); // displays output private: string automake; string automodel; int autospeed; int autoyear; }; // -----------------------------car.cpp--------------------------- // class definition car. #include <iostream> #include "car.h" #include <string> using namespace std; car::car(string &make, string &model, int year) { automake = make; automodel = model; autospeed = 0; if(year < 0) year = 2015; else autoyear = year; } string car::getmake() { return automake; } string car::getmodel() { return automodel; } int car::getyear() { return autoyear; } int car::getspeed() { return autospeed; } bool car::setvalue(string &make, string &model, int year) { if(year < 0) { automake = make; automodel = model; autoyear = year; return true; } else return false; } void car::display() { //cout <<"your car " << autoyear << automodel << automake << endl; //cout <<"and going " << autospeed << " mph." << endl; } bool car::accelerate(char a) { if((a=='h')||(a=='h')||(a=='m')||(a=='m')||(a=='l')||(a=='l')) { if((a=='h')||(a=='h')) autospeed += 10; if((a=='m')||(a=='m')) autospeed += 5; if((a=='l')||(a=='l')) autospeed += 1; return true; } else return false; } bool car::brake(char b) { if((b=='h')||(b=='h')||(b=='m')||(b=='m')||(b=='l')||(b=='l')) { if((b=='h'||b=='h' && autospeed > 10)) autospeed = 10; if((b=='m'||b=='m' && autospeed > 5)) autospeed = 5; if((b=='l'||b=='l' && autospeed > 1)) autospeed = 1; return true; } else return false; } // -------------main.cpp-------------------- // driver routine test functions of car class #include <iostream> #include <string> #include "car.h" #include "car.cpp" using namespace std; int main() { car car1("ford", "crown victoria", 1997); car car2("chevrolet", "camaro"); car car3("hyundai", "sonata", -15); cout << "\n*** displaying each car's stats\n"; cout << "car1:\n"; car1.display(); cout << "\ncar2:\n"; car2.display(); cout << "\ncar3:\n"; car3.display(); cout << "\n*** accelerating car 3 several times:\n"; car3.accelerate('h'); // accelerate hard cout << "car3 speed: " << car3.getspeed() << '\n'; car3.accelerate('m'); // accelerate medium cout << "car3 speed: " << car3.getspeed() << '\n'; car3.accelerate('l'); // accelerate low cout << "car3 speed: " << car3.getspeed() << '\n'; car3.accelerate('l'); // accelerate low cout << "car3 speed: " << car3.getspeed() << '\n'; car3.accelerate('z'); // accelerate invalid level cout << "car3 speed: " << car3.getspeed() << '\n'; cout << "\n*** resetting car make/models\n"; car1.setvalue("flinstones", "rock car", -2100); car3.setvalue("toyota", "camry", 2005); cout << "car1:\n"; car1.display(); cout << "\ncar3:\n"; car3.display(); cout << "\n*** decelerating car3\n"; car3.brake('m'); cout << "car3 speed: " << car3.getspeed() << '\n'; car3.brake('l'); cout << "car3 speed: " << car3.getspeed() << '\n'; car3.brake('l'); cout << "car3 speed: " << car3.getspeed() << '\n'; car3.brake('m'); cout << "car3 speed: " << car3.getspeed() << '\n'; car3.brake('a'); cout << "car3 speed: " << car3.getspeed() << '\n'; car3.brake('h'); cout << "car3 speed: " << car3.getspeed() << '\n'; cout << "\n*** calling accessors\n"; cout << "car1:\n"; cout << " make: " << car1.getmake() << '\n' << " model: " << car1.getmodel() << '\n' << " year: " << car1.getyear() << '\n'; cout << "car2:\n"; cout << " make: " << car2.getmake() << '\n' << " model: " << car2.getmodel() << '\n' << " year: " << car2.getyear() << '\n'; cout << "car1:\n"; cout << " make: " << car3.getmake() << '\n' << " model: " << car3.getmodel() << '\n' << " year: " << car3.getyear() << '\n'; }
as error says, need provide constructor const
references:
car(const string &make, const string &model, int year=2015);