Ways of Passing Parameters to the Constructor

67 views Asked by At

My question is on line 72. Why can we do the same operation at line 81 without any errors? Are there any preferences that should be done if we declare an object that needs parameters to be passed to its class constructor?

#include<iostream>
using namespace std;
class clsPerson 
{
    string _FullName;
    class clsAddress
    {
    private:
        string _AddressLine1;
        string _AddressLine2;
        string _City;
        string _Country;
    public:
        clsAddress(string AddressLine1, string AddressLine2, string City, string Country)
        {
            _AddressLine1 = AddressLine1;
            _AddressLine2 = AddressLine2;
            _City = City;
            _Country = Country;
        }
        string setAddressLine1(string AddressLine1)
        {
            _AddressLine1 = AddressLine1;
        }
        string AddressLine1()
        {
            return _AddressLine1;
        }
        string setAddressLine2(string AddressLine2)
        {
            _AddressLine2 = AddressLine2;
        }
        string AddressLine2()
        {
            return _AddressLine2;
        }
        string setCity(string City)
        {
            _City = City;
        }
        string City()
        {
            return _City;
        }
        string setCountry(string Country)
        {
            _Country = Country;
        }
        string Country()
        {
            return _Country;
        }
        void Print()
        {
            cout << "\nAddress:\n";
            cout << _AddressLine1 << endl;
            cout << _AddressLine2 << endl;
            cout << _City << endl;
            cout << _Country << endl;
        }
    };
public:
    string setFullName(string FullName)
    {
        _FullName = FullName;
    }
    string FullName()
    {
        return _FullName;
    }
    clsAddress Address = clsAddress("", "", "", "");
    // why i can't replace line 71 with => clsAddress Address ("", "", "", "");
    clsPerson(string FullName, string AddressLine1, string AddressLine2, string City, string Country)
    {
        _FullName = FullName;
        Address = clsAddress(AddressLine1, AddressLine2, City, Country); 
    }
};
int main()
{
    clsPerson Person1 ("Mohammed Abu-Hadhoud", "Building 10", "Queen Rania Street", "Amman", "Jordan");
    clsPerson Person1 = clsPerson ("Mohammed Abu-Hadhoud", "Building 10", "Queen Rania Street", "Amman", "Jordan");
    Person1.Address.Print();
    return 0;
}

I tried both ways and they are correct out of the class, but only one way was accepted by the compiler if I declared an object inside the class.

1

There are 1 answers

4
Remy Lebeau On

You can't use parenthesis to initialize a class member directly in its declaration, the syntax rules don't allow it. But, you can use curly braces instead, eg:

class clsPerson 
{
    ...
    clsAddress Address {"", "", "", ""};
    ...
};

Otherwise, use the member initialization list of the parent class's constructor to initialize a member that has a non-default constructor, eg:

class clsPerson 
{
    ...
public:
    clsAddress Address;

    clsPerson(string FullName, string AddressLine1, string AddressLine2, string City, string Country)
        : _FullName(FullName), Address(AddressLine1, AddressLine2, City, Country)
    {
    }

    ...
};

You can also give clsAddress a default constructor instead and let the compiler initialize the std::string members to empty strings for you (since std::string is itself default constructible), eg:

class clsAddress
{
...
public:
    clsAddress() = default;
    ...
};

class clsPerson 
{
    ...
    clsAddress Address;
    ...

    clsPerson(string FullName, string AddressLine1, string AddressLine2, string City, string Country)
    {
        _FullName = FullName;
        Address = clsAddress(AddressLine1, AddressLine2, City, Country)
    }
};

But this is less efficient since you are creating two clsAddress objects and copying one to the other.


On a side note: you have several warnings in your code about missing return statements in your setter methods. None of them should have a return type declared on them, use void instead, eg:

void setAddressLine1(string AddressLine1)
{
    _AddressLine1 = AddressLine1;
}

// etc ...