-1

Thanks so much for taking the time to look at my question!

Right now I am working with classes and objects. I am trying to write a program that stores information about visitors to a hotel. A user will input the name of the visitor and some information about them. The program will then store that information in an object and be able to calculate how much to charge for the users stay.

The problem that I am running into is that I don't know how to let the program create new objects for the visitors. For example, if Sally came in I would like to create a new object for her within the program that could store her information.

I have looked at dynamic object creation and done a fair amount of Googling on the subject but can't seem to find any answers. Here is a simplified version of what I would like to do:

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

class visitor {
public:
    string name;
    int age;
};

int main()
{
//a new person comes to the hotel, the person at the desk gives the program his/her name
//and age and it is put into a class so it can be used later.
}

If there is a better way to accomplish this I would love suggestions, I am but a fledgling programmer and it is very possible that I am approaching this incorrectly.

Thanks in advance!

Zeke Medley
  • 111
  • 7
  • This question totally sounds like the Matrix quote. – SergeyA Mar 31 '16 at 19:03
  • Welcome to Stackoverflow! Clearly what you need is to learn the basics of C++. Teaching you those is beyond the purposes and scope of this site but we do have [The Definitive C++ Book Guide and List](http://stackoverflow.com/q/388242/1362568), which can certainly help you. – Mike Kinghan Mar 31 '16 at 19:12

2 Answers2

1

You have done fine, so far.

class visitor {
public:
   string name;
   int age;
};

int main()
{
   //a new person comes to the hotel, the person at the desk 
   //gives the program his/her name
   //and age and it is put into a class so it can be used later.
}

Now remember how easy it is to define a integer value i, and initialize it with 0:

int i = 0;

Your class is just like "int". So name a variable declared just like int.

visitor guest1;

You should write a default ctor to initialize the contents. Note that your code has a default ctor provided by the compiler. But what it does (nothing) is not terribly useful.

And then write a non-default ctor to fill in the contents.

and so on, and so on.

How about a show method to display values.

guest1.show();

Good luck.

2785528
  • 5,438
  • 2
  • 18
  • 20
0

You need to create a constructor. This is a function that constructs visitors. We write that as follows:

class Visitor {
    public:
        string name;
        int age;
        Visitor(string name, int age) {
            this->name = name;
            this->age = age;
        }
};

We can then create a new Visitor object (note that its usual convention to make a class name's first letter uppercase) with the following:

Visitor sally = Visitor("Sally", 22);

To allow the user to input what we want the name and age to be, you should look at another SO answer like Getting user input in C++.

EDIT: You don't need to create a constructor as the compiler will make one by default in this case, but it will be useful for you to learn by creating your own constructors for the time being, so you know what's happening.

Community
  • 1
  • 1
Cisplatin
  • 2,860
  • 3
  • 36
  • 56
  • Thanks so much! I now realize that my question may have been too broad or basic; however, this answers my question very well. I really appreciate it. – Zeke Medley Mar 31 '16 at 21:45