Thread: problem with constructors

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    76

    problem with constructors

    I cannot seem to initialize my constructor with default parameters. Could anyone give me a good explanation as to why this is happening?

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    using namespace std;
    
    class secretType
    {  
    public:
       void print() const;
       void setName();
       void setAge();
       void setWeight();
       void setHeight();
       string getName() const;
       int getAge() const;
       int getWeight() const;
       double getHeight() const;
       secretType secretType(string = " ", int = 0, int = 0, double = 0);
       
    private:
       string name;
       int age;
       int weight;
       double height;
    };
    int main(int argc, char *argv[])
    {
    
        secretType yourName;
        yourName.setName();
        yourName.setAge();
        yourName.setWeight();
        yourName.setHeight();
        yourName.getName();
        yourName.print();
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    // FUNCTION DEFINITIONS
    
    // function to set name.
    void secretType:: setName()
    {
         cout << "enter your name: "; cin >> name;
    }
    // function to set age.
    void secretType::setAge()
    {
         cout << "enter your age: "; cin >> age;
    }
    // function to set weight.
    void secretType:: setWeight()
    {
         cout << "enter your weight: "; cin >> weight;
    }
    // function to set height.
    void secretType::setHeight()
    {
         cout << "enter your height in decimal format(0ft.0in): "; cin >> height;
    }
    // function to return name.
    string secretType:: getName()const
    {
           return name;
    }
    // function to return age.
    int secretType:: getAge() const
    {
        return age;
    }
    // function to return weight.
    int secretType:: getWeight() const
    {
           return weight;
    }
    // function to return height.
    double secretType:: getHeight() const
    {
           return height;
    }
    // function to print name.
    void secretType:: print() const
    {
         cout << "name: " << name << " age: " << age << " weight: " << weight << " height: " << height << endl;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Constructors have no return type.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Why what is happening? You have no implementation of the constructor. You seem to be misunderstanding something about either constructors or default arguments.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    I believe this is it:

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    using namespace std;
    
    class secretType
    {  
    public:
       void print() const;
       void setName();
       void setAge();
       void setWeight();
       void setHeight();
       string getName() const;
       int getAge() const;
       int getWeight() const;
       double getHeight() const;
       secretType(string = " ", int = 0, int = 0, double = 0);
       
    private:
       string name;
       int age;
       int weight;
       double height;
    };
    int main(int argc, char *argv[])
    {
    
        secretType yourName;
        yourName.setName();
        yourName.setAge();
        yourName.setWeight();
        yourName.setHeight();
        yourName.getName();
        yourName.print();
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    // FUNCTION DEFINITIONS
    secretType::secretType(string name, int age, int weight, double height)
    {
      name = " ";
      age = 0;
      weight = 0;
      height = 0;
    }
    // function to set name.
    void secretType:: setName()
    {
         cout << "enter your name: "; cin >> name;
    }
    // function to set age.
    void secretType::setAge()
    {
         cout << "enter your age: "; cin >> age;
    }
    // function to set weight.
    void secretType:: setWeight()
    {
         cout << "enter your weight: "; cin >> weight;
    }
    // function to set height.
    void secretType::setHeight()
    {
         cout << "enter your height in decimal format(0ft.0in): "; cin >> height;
    }
    // function to return name.
    string secretType:: getName()const
    {
           return name;
    }
    // function to return age.
    int secretType:: getAge() const
    {
        return age;
    }
    // function to return weight.
    int secretType:: getWeight() const
    {
           return weight;
    }
    // function to return height.
    double secretType:: getHeight() const
    {
           return height;
    }
    // function to print name.
    void secretType:: print() const
    {
         cout << "name: " << name << " age: " << age << " weight: " << weight << " height: " << height << endl;
    }

  5. #5
    Registered User SpaceCadet's Avatar
    Join Date
    Oct 2006
    Posts
    23
    A polite observation regarding the constuctor implementation...

    Code:
    secretType::secretType(string name, int age, int weight, double height)
    {
      this->name = name;
      this->age = age;
      this->weight = weight;
      this->height = height;
    }
    You initial code was close to implcitly inlining the constructor. Be careful of this for many reasons. The one that comes to mind is that debuggers can have trouble with inline functions.

  6. #6
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Or?

    Code:
    secretType::secretType(string name, int age, int weight, double height)
    : name(name), age(age), weight(weight), height(height)
    {
    }

  7. #7
    Registered User SpaceCadet's Avatar
    Join Date
    Oct 2006
    Posts
    23
    That is more efficient and preferrable.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    if your serious about this class then you should implement better encapsulation.

    you have encapsulated your private variables (ie name, age, etc), and have used public accessor functions (gets/sets) for them. this is good, but with the implementations of your accessor functions you dont protect your private data at all. ie, there is no error checking. i could enter an age of -2.

    if your just playing around with stuff, otherwise, then its a good start! have fun

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    Thanks for the information. Yes, I am still learning alot about classes and having fun with them but in the same sense I am also serious, so thanks for all of your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM