Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello all, I have a question about C++ & OOP concepts. I get the simpler concept

ID: 3555848 • Letter: H

Question

Hello all,

I have a question about C++ & OOP concepts. I get the simpler concepts without the OOP in C++, but I struggle with the conceps & usage with things like calling functions, declaring classes (which I get) and using them as well (which I'm more shaky on).

I was wondering if someone could help me out by doing a (as short as possible) example of making a class, getting text input, and then outputting it as a vector. For example: making a "car" or "food" class or something similar, then getting text (string input), and finally outputting it into a text file. I'm following along with the cplusplus.com tutorials.

It would be really helpful if you could also put small notes in comments about any style preferences along the way.   

I tried something like this, but there's a lot of errors.

#include <iostream>
#include <string>

using namespace std;

class Auto
{
   public: //needed?
   string make;
   string model;
}

int main()
{
   Auto NewCar ();
   cout << "Please enter the make: ";
   cin >> make;
   cout << make;
   cout << " Please enter the model: ";
   cin >> model;
   cout << model;

//no clue on vectors

   return 0;
}

I (kind of) get how to make classes and inheritance, but actually using them is a whole other story. Gah! Help!

Explanation / Answer

#include <iostream>
#include <vector>
#include <string>

using namespace std;

class Auto
{
   private: // this should be private..that is purpose of class
   string make;
   string model;
//we need constructor to create object else compiler will create on its own.
public:
Auto()
{
make = ""; // assign make to empty string.
model = "";// assign model to empty string.
}
void setMake(string make) //mutator to set make
{
this->make = make;
}
void setModel(string model) //mutator to set make
{
this->model = model;
}
string getMake() // have accessor to get make of Auto
{
return make;
}
string getModel() // have accessor to get model of Auto
{
return model;
}

};

int main()
{
   Auto NewCar;
   string make,model;
   cout << "Please enter the make: ";
   cin >> make;
   NewCar.setMake(make); // set make using mutator.
   cout << NewCar.getMake();

   cout << " Please enter the model: ";
   cin >> model;
   NewCar.setModel(model); // set model using mutator.
   cout << NewCar.getModel();
    //create Vector of Autos.
   vector<Auto> auto_array;
   auto_array.push_back(NewCar);

   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote