Hi guys, I just started to learn class in C++ and get confused by some of the codes

class FLATMATE
{
    string name;
    public:
        FLATMATE() { name = "";}
        FLATMATE(string n){name = n;}
        string getName(){return name;}
        void setResident(string n){name = n;}
};

//Below are the base class for different duty types

class DUTY
{
    FLATMATE resident;        <<<<<<<<<<What do we call this one? object or constructor or something else?
 }

any idea what is the name for the "FLATMATE resident "in the class Duty? is it an object?
And what will happen to the resident in class Duty? Get initialized?

Recommended Answers

All 3 Replies

The code FLATMATE resident; creates an instance of FLATMATE. It is called an object. Whenever you create a DUTY object, inside that duty object, is an instance of FLATMATE. So each DUTY object has a FLATMATE object. Its similar to this:

class DUTY{
int resident;
}

the only difference is that instead of it being an int, it is of type FLATMATE. You can initialize FLATMATE object in the constructor of DUTY.

First of all, the use of all-upper-case for class names is horrible. By convention, all-upper-case is reserved for #define (or MACROs). If this example was from a book, consider changing book.

Now, about your question... first, observe the following: "string" is a class, just like "FLATMATE" is a class. So if you don't have a problem understanding what "string name;" is and how it can be part of class "FLATMATE", why do you have a problem with "DUTY" containing a data member of class "FLATMATE". There is absolutely no difference.

>>any idea what is the name for the "FLATMATE resident "in the class Duty?
The name is "data member" of class FLATMATE, more precisely "private data member" because the default access right in a class declaration is "private" (if you haven't learned access rights yet, it won't take long until you reach that concept).

>>is it an object?
Yes. If you are confused about why it is an object and a data member at the same time, take this analogy: A father can say "this is my son", while the son will say "I am a man". Now that becomes: An object of class DUTY can say "resident is my data member", while resident will say "I am an object of class FLATMATE".

>>And what will happen to the resident in class Duty?
When an object of class DUTY is created, an object of class "FLATMATE" will also be created within the DUTY object and will be accessible by the name or identifier "resident". Conversely, when the object of class DUTY is deleted or destroyed, "resident" will be deleted along with it. In between, what will happen to "resident"? well.. whatever you decide to do with it.

>>Get initialized?
Kinda.. you are responsible for providing a valid initialization for resident. However, if you don't, the compiler provides a default one, which is often not good enough but it makes sure "resident" does get initialized when an object of class DUTY is created. Coming back to my original observation about "string" and "FLATMATE", observe how the string "name" gets initialized in the constructors of class FLATMATE (the constructors are the two functions called FLATMATE() and that don't have a return type). The constructors are used to give initial values to the data members when a new object is created. A destructor does the exact opposite, but for simple classes it is not necessary. A constructor is usually there, in multiple forms (I mean different parameters to initialize the object with), but often the destructor is omitted.

I could go on and on about this, but I think you are just starting out, so I will leave it at that for now.

Thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.