Reputation: 17
I started learning C++ recently and I'm having a problem with an assignment I need to write, my assignment requires that I will create a class constructor that has another variable of a different class type. my main class is 'person' and I have two other classes 'address and 'job', in my assignment I need to create setter and getter for each variable I have in the different classes, and in each setter and getter I have to print if the operation was successful or not. so here is the problem I haveing, when I call the 'person' parameter constructor in my main file its immediately call 'address' and 'job' default constructor which prints the default setter and getter in their constructors and that is something I want to avoid because I don't want it to print the default values, is there any way of avoiding the default constructor? unfortunately, I can't remove the printד from the setter and getter and I can't change the variables of the 'person' constructor, also I have to use the set and get in the default constructor
class person
{
private:
char *name;
char *phoneNumber;
char *email;
int savings;
address Address;
job Job;
public:
person();
person(const char *name, const char *phoneNumber, const char *email, address Address, job Job);
~person();}
address:
class address {
private:
char* street;
char* city;
int postalCode;
public:
address();
address(const char* street,const char* city, int postalCode);
~address();}
job:
class job {
private:
char* title;
int salary;
departmentEnum department;
public:
job();
job(const char* title, int salary, departmentEnum department);
~job();}
person constructor - it actually prints both the default values of 'address' and 'job' and the values I sent to the person constructor.
person::person(const char *nameP, const char *phoneNumberP, const char *emailP, address AddressP, job JobP)
{
setName(nameP);
setPhoneNumber(phoneNumberP);
setEmail(emailP);
Address.setCity(AddressP.getCity());
Address.setPostalCode(AddressP.getPostalCode());
Address.setStreet(AddressP.getStreet());
Job.setSalary(JobP.getSalary());
Job.setDepartment(JobP.getDepartment());
Job.setTitle(JobP.getTitle());
}
address default constructor
address::address()
{
setStreet("test");
setCity("test");
setPostalCode(12345);
}
Upvotes: 1
Views: 125
Reputation: 4291
If all you want is to avoid the default constructor being called, all you have to is use an initializer instead.
person::person(person::person(const char *nameP, const char *phoneNumberP, const char *emailP, address AddressP, job JobP) :
Address(AddressP),
Job(JobP)
{
//Do your stuff
}
This way, the default constructor of the internal address
and job
are skipped. Instead the (implicit) copy constructor is used.
You could also use the (explicit) secondary constructor, if you were to pass the raw values instead of the already done object.
Example:
person::person(const char *nameP, const char *phoneNumberP, const char *emailP, address AddressP, char* title, int salary, departmentEnum department) :
Address(AddressP),
Job(title, salary, department)
{
//Do your stuff
}
In the lower example, you will find that Job's second constructor is called.
The upper will not produce any output by the way, because you have not defined Job::Job(const Job &otherJob);
(the copy constructor).
Upvotes: 1