简体   繁体   中英

C++ - Object as a variable in parameters

I am learning C++ through TheNewBostons tutorials and there is one of the programs that I can't figure out how it works. I'll show you the codde before the question.

Main.cpp:

#include <iostream>
#include "People.h"
#include "Birthday.h"


using namespace std;

int main()
{
    Birthday birthObject(12,28,1986);

    People Ob("Vidar", birthObject);
    Ob.printInfo();
}

Birthday.h:

#ifndef BIRTHDAY_H
#define BIRTHDAY_H


class Birthday
{
    public:
        Birthday(int m,int d,int y);
        void prinDate();
    private:
        int month, day, year;
};

#endif

Birthday.cpp:

#include <iostream>
#include "Birthday.h"

using namespace std;

Birthday::Birthday(int m,int d,int y)
{
    month = m;
    day = d;
    year = y;
}

void Birthday::prinDate()
{
    cout << day << "/" << month << "-" << year << endl;
}

People.h:

#ifndef PEOPLE_H
#define PEOPLE_H

#include <string>
#include "Birthday.h"
using namespace std;

class People
{
    public:
        People(string x, Birthday bo);
        void printInfo();
    private:
        string name;
        Birthday dateOfBirth;
};

#endif

People.cpp:

#include "People.h"
#include "Birthday.h"

#include <iostream>
#include <string>
using namespace std;

People::People(string x, Birthday bo)
: name(x), dateOfBirth(bo)
{

}

void People::printInfo()
{
    cout << name << " was born on ";
    dateOfBirth.prinDate();
}

The thing that I can't figure out is how the objects are used as variables and parameters, and also how you can create an object without calling the constructor (in People.h).

This expression (mem initializer)

dateOfBirth(bo)

in the constructor definition

People::People(string x, Birthday bo)
: name(x), dateOfBirth(bo)
{

}

means a call of the copy constructor of class Birthday to construct object dateOfBirth from object bo.

For example if you would add an explicitly defined copy constructor for class Birthday the following way

class Birthday
{
    public:
        Birthday(int m,int d,int y);
        Birthday( const Birthday &rhs )
        {
            std::cout << "Birthday::Birthday( const Birthday & ) is called" << std::endl;
            month = rhs.month; day = rhs.day; year = rhs.year;
        } 
        void prinDate();
    private:
        int month, day, year;
};

then when this statement was executed

People Ob("Vidar", birthObject);

you would get message

Birthday::Birthday( const Birthday & ) is called

If you do not define explicitly the copy constructor then the compiler defines it implicitly.

A reasonable question.

What is happening here is that it is calling the copy constructor.

Whenever you declare a class there are several functions which are declared for you, unless you explicitly override them.

  • Default constructor (if no other constructor is explicitly declared)
  • Copy constructor if no move constructor or move assignment operator is explicitly declared. If a destructor is declared generation of a copy constructor is deprecated.
  • Move constructor if no copy constructor, move assignment operator or destructor is explicitly declared.
  • Copy assignment operator if no move constructor or move assignment operator is explicitly declared. If a destructor is declared generation of a copy assignment operator is deprecated.
  • Move assignment operator if no copy constructor, copy assignment operator or destructor is explicitly declared.
  • Destructor

Have a look here: http://en.wikipedia.org/wiki/Special_member_functions

In this case the copy constructor is called.

Let's take this step by step:

Birthday birthObject(12,28,1986);

here you're creating a new local variable birthObject of type of Birthday class, passing as parameters (12,289,1986). That invokes the three-parameter constructor of Birthday class

People Ob("Vidar", birthObject);

Here you're creating a local variable Ob of type People (note common conventions have class names starting with uppercase, variables starting with lowercase) and passing as parameters ("Vidar" and birthObject).

That invokes the two-parameters constructor of People. This is defined as:

People::People(string x, Birthday bo): name(x), dateOfBirth(bo) { }

Some notes here:

  • Both parameters are passed by value. This means that the copy constructor (implicit, because you haven't defined any) will be invoked for each of them. (note, as it has been noted in other answers, it is probably best to use const & instead).
  • attributes are initialized (name, dateOfBirth) (this involves an additional copy constructor for each of them).

    Ob.printInfo();

Finally, the printInfo() method of the Ob instance is invoked... which in turn invokes the prinData() method of the Ob's attribute dateOfBirth. Those methods do not have parameters and are invoked on the respective instances.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM