(C++ Beginner) How do I call these class functions into the main? -
i keep having errors, i'm not sure how call car class main function. instructions given below.
#include <iostream> #include <string> class car{ public: car(){ int year = 1990; std::string make = "bentley"; int speed = 0; }; car(int new_year, std::string new_make, int new_speed) { year = new_year; make = new_make; speed = new_speed; } int get_year() { return year; } std::string get_make() { return make; } int get_speed() { return speed; } void accelerate() { speed+=5; } void brake() { speed-=5; } private: int year; std::string make; int speed; }; int main() { int year = 1990; std::string make = "bentley"; int speed = 0; car yourcar(year, make, speed); std::cout << "year: " << yourcar.get_year << std::endl; std::cout << "make: " << yourcar.get_make << std::endl; std::cout << "speed: " << yourcar.get_speed << std::endl; }
instructions: please implement class named car in c++ has following member variables:
- year. int holds car’s model year.
- make. string object holds make of car.
- speed. int holds car’s current speed. in addition, class should have following member functions:
- constructor. constructor should accept car’s year , make arguments , assign these values object’s year , make member variables. constructor should initialize speed member variable 0.
- accessors or getters. appropriate accessors or getters should allow values retrieved object’s year, make , speed member variables.
- accelerate. accelerate function should add 5 speed member variable each time it’s called.
- brake. brake function should subtract 5 speed member variable each time called.
demonstrate class in program creates car object , calls accelerate function 5 times. after each call accelerate function, current speed of car , display it. then, call brake function 5 times. after each call break function, current speed of car , display it.
change
car(){ int year = 1990; std::string make = "bentley"; int speed = 0; };
to
car(){ year = 1990; make = "bentley"; speed = 0; }
you don't need specify data type here.
next,
std::cout << "year: " << yourcar.get_year << std::endl; std::cout << "make: " << yourcar.get_make << std::endl; std::cout << "speed: " << yourcar.get_speed << std::endl;
should be
std::cout << "year: " << yourcar.get_year() << std::endl; std::cout << "make: " << yourcar.get_make() << std::endl; std::cout << "speed: " << yourcar.get_speed() << std::endl;
you forgot ()
after functions.
Comments
Post a Comment