Default parameters in fist ever class

I am stuck trying to write my first class. I have no idea if this is correct or not. I have it working like it is and it prints out what I put in, but I need to have default parameters in the constructor and when I do this it doesn't work anymore. Does the constructor w/ default params go up in the class or in the main like I have my object? Take a look and help me out I have an online class and no one to bounce ideas off.

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//secretType class is personType

#include <iostream>
using namespace std;

class secretType 
{
 public:
        void print() const; //prints for testing 
        void setName(string name); 
        void setAge(int age);
        void setWeight(int weight);
        void setHeight(double height);
        string getName()const;
        int getAge()const;
        int getWeight()const;
        double getHeight()const;
         
  private:
           string name;
           int age, weight;
           double height;
 };
 
// class member function definitions 
void secretType::print() const
{
	cout << "Name: " << name <<endl;
	cout << "Age: " << age <<endl;
	cout << "Weight: " << weight <<endl;
	cout << "Height: " << height <<endl;

}
void secretType::setAge(int a)
{
    age = a; 
}

void secretType::setName(string n)
{
	name = n;
}
void secretType::setWeight(int w )
{
	weight = w;
}
void secretType::setHeight(double h)
{
	height = h; 
}
string secretType::getName()const
{
  return name;
}
int secretType::getAge()const
{
  return age;
}
int secretType::getWeight()const
{
  return weight;
}
double secretType::getHeight()const
{
  return height;
}

int main()
{
    // constructor with parameters NOT WORKING!!!!
      //  secretType secretObject(string name = " ", int age = 0, int weight = 0, double height = 0);
   secretType secretObject; // create secretType class object to test
   secretObject.setName("Brent Smith");
   secretObject.setAge(36);
   secretObject.setWeight(228);
   secretObject.setHeight(72);
   secretObject.print();
   
    system("PAUSE");
    return 0;
}


Last edited on
It's because of the names of your constructor arguments I think. You cannot name them the exact same names as the members of your class. That would cause name collision in the constructor body. In addition you do not actually end up defining the constructor at any point in your program.
EDIT: By the way, Dev ftf. Use wxdev instead if anything: http://www.cplusplus.com/forum/articles/7263/
Last edited on
I tried to take the parameter names out, no luck. Tried adding s to the end of them to make them unique, no luck. I Also tried w/ no names like this:
secretType secretObject(string = " ", int = 0, int = 0, double = 0); still no luck.

So, I have the constructor in the right place but just wrote it wrong? How do you make this work w/ the default parameters? I am still stuck.
Did you even define the constructor at any point? (I'm not sure if you put the default arguments in the prototype.)
EDIT: And where'd it go?
Last edited on
I don't know how or where to do that, thus the post. Please just show me.
Wait I think I get it now. This on works, is this correct? Am I also declaring my public and private functions and variable correctly??
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//secretType class is personType

#include <iostream>
using namespace std;

class secretType 
{
 public:
        // constructor with parameters
        secretType secretObject(string = " ", int = 0, int = 0, double = 0);
        void print() const; //prints for testing 
        void setName(string name); 
        void setAge(int age);
        void setWeight(int weight);
        void setHeight(double height);
        string getName()const;
        int getAge()const;
        int getWeight()const;
        double getHeight()const;
         
  private:
           string name;
           int age, weight;
           double height;
 };
 
// class member function definitions 
void secretType::print() const
{
	cout << "Name: " << name <<endl;
	cout << "Age: " << age <<endl;
	cout << "Weight: " << weight <<endl;
	cout << "Height: " << height <<endl;

}
void secretType::setAge(int a)
{
    age = a; 
}

void secretType::setName(string n)
{
	name = n;
}
void secretType::setWeight(int w )
{
	weight = w;
}
void secretType::setHeight(double h)
{
	height = h; 
}
string secretType::getName()const
{
  return name;
}
int secretType::getAge()const
{
  return age;
}
int secretType::getWeight()const
{
  return weight;
}
double secretType::getHeight()const
{
  return height;
}

int main()
{
   secretType secretObject; // create secretType class object to test
   secretObject.setName("Brent Smith");
   secretObject.setAge(36);
   secretObject.setWeight(228);
   secretObject.setHeight(78);
   secretObject.print();
   
    system("PAUSE");
    return 0;
}
secretType secretObject(string = " ", int = 0, int = 0, double = 0);
This is not a constructor declaration but a function declaration called secretObject that returns an object of type secretType

a constructor declaration would look like this:
secretType(string str, int a, int b, double c);
Topic archived. No new replies allowed.