Ctor with parameters

Hi everyone, i have just started reading about Ctor with parameters, and i had
written this Ctor:

 
  person::person(char* name=" ",int age=0,bool isMale=false):_name(name),_age(age),_isMale(isMale)	{}

in the main section, i just wrote:
person p;
and there are errors. can you please explain me why? doesnt it suppose to
be created with the parametered ctor?

thanks !
You didn't post all of your code or any of your errors, so I can't see where you went wrong, but here is a working example of a constructor with default values for parameters:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>

class Person
{
public:
	Person(const char* name,int age,bool isMale);
	std::string toString();
private:
	const char* name;
	int age;
	bool isMale;
};

Person::Person(const char* name = " ", int age = 0, bool isMale = false) : name(name), age(age), isMale(isMale) {}

std::string Person::toString()
{
	return "Name: " + std::string(this->name) + "\nAge: " + std::to_string(this->age) + "\nThis person is " + (this->isMale ? "" : "not ") + "male.";
}

int main()
{
	Person p;
	std::cout << p.toString() << std::endl;
	return 0;
}


This prints:

Name:  
Age: 0
This person is not male.
Last edited on
First of all- thank you for your investment!
and sorry. i add it below. can you please explain me why it doesnt work?
i didnt learn string yet..
H file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#define PERSON_H

class person
{
public:
	char *_name;
	int _age;
	bool _isMale;
	person(char*,int,bool);
	~person(void);
	char* getName()	const;
	int getAge()	const;
	bool getSex()	const;

};

Cpp
1
2
3
4
5
6
7
person::person(char* name=" ",int age=0,bool isMale=false):_name(name),_age(age),_isMale(isMale)	{}

person::~person(void)	{}

char* person::getName()	const	{return _name;}
int person::getAge()	const	{return _age;}
bool person::getSex()	const	{return _isMale;}

Main
1
2
	person p1;
	person p2("A");
Last edited on
What do you mean "doesn't work"? Please be specific.

.h line 1: Your include guard is incomplete.
1
2
3
4
#ifndef PERSON_H
#define PERSON_H
//  contents of header file
#endif 


.h line 9 and .cpp line 1: Your default parameters need to be specified in the class declaration, not the definition.
1
2
// .h
  person(char* name=" ",int age=0,bool isMale=false); 


1
2
// .cpp
person::person(char* name,int age,bool isMale):_name(name),_age(age),_isMale(isMale)

i did it.. doesnt work :/ can u please explain me why?
im adding the code excatly as it written:
H:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#define PERSON_H

class person
{
public:
	char *_name;
	int _age;
	bool _isMale;
	person(char* name=" ",int age=0,bool isMale=false);
	~person(void);
	char* getName()	const;
	int getAge()	const;
	bool getSex()	const;

};
#endif 

CPP
1
2
3
4
5
6
7
8
9
#include "person.h"

person::person(char* name=" ",int age=0,bool isMale=false):_name(name),_age(age),_isMale(isMale)	{}

person::~person(void)	{}

char* person::getName()	const	{return _name;}
int person::getAge()	const	{return _age;}
bool person::getSex()	const	{return _isMale;}

MAIN
1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;
#include "person.h"

int main()
{
	person p1;
	person p2("A");
}


Thanks a lot !
When asked to be more specific about "what does not work", that means if you're getting compile errors, post the exact text of the compile errors. If you're getting run time errors, post what errors you're getting at run time. Don't just repeat "it doesn't work".

As I explained above, default parameters go in the declaration, not the implementation. I see you added the default values in the declaration of the person constructor, but you have not removed them from the implementation. I already gave you the corrected code for the implementation of the constructor.

Your include guard is still not correct. See the example I posted.

You also have a problem with how you're passing name into your constructor. You're passing a pointer to a char array, then storing that pointer in your class. What happens if the pointer to the char array goes out of scope? Your copy of that pointer is now invalid.
Last edited on
ok i understand my mistake.
but in the first code you wrote, you had initialized it in the constructor itself in the cpp file and not in the H file.

can you tell me where exactly i need to do it?
in the first code you wrote, you had initialized it in the constructor itself in the cpp file and not in the H file.

Initialization and specifying defaults are two different things.

Specifying defaults belongs in the header file (class declaration).
Initializing member variables belongs in the implementation (.cpp file).

You were attempting to specify defaults in both the .h file AND the .cpp file.

The code I posted is correct.
1
2
// .h
  person(char* name=" ",int age=0,bool isMale=false); 

Underlined elements are defaults.

1
2
// .cpp
person::person(char* name,int age,bool isMale):_name(name),_age(age),_isMale(isMale)

Underlined elements are initialization.

Last edited on
Topic archived. No new replies allowed.