-5

I'm told to read a name from a constructor (homework), however the class constructor is not supposed to take any parameters - something I find weird.

I have tried to simply put cout's and cin.getline's inside the constructor, butt that doesn't work. I don't get how I can read data from user inside a constructor that does not have any parameters. Is it even possible?

E.g

class Animal
{
  private:
    char name[20];

  public:
    Animal() { SOMEHOW READ NAME HERE WITHOUT CON. PARAMETER }
};

int main() {

  Animal a1; // should ask for name and read it, add it to data
  return 0;
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
Richardo
  • 1
  • 1
  • 4
  • 3
    Why doesn't it work? It should. Please create a [mcve]. – Rakete1111 Feb 20 '18 at 19:52
  • Use std::string. – DeiDei Feb 20 '18 at 19:54
  • If I edit "SOMEHOW READ NAME HERE WITHOUT CON. PARAMETER" to "cout << "Name of animal? "; cin.getline(name, 20);", that doesn't work for me. And yeah, of course with using namespace std; include #cstring and #include – Richardo Feb 20 '18 at 19:54
  • 2
    @Richardo what do you mean by "doesn't work"? Did it do something that you didn't expect? – eerorika Feb 20 '18 at 19:54
  • 1
    @Richardo Provide the additional information in the question rather than in a comment. – François Andrieux Feb 20 '18 at 19:54
  • 2
    @Richardo "It doesn't work" is not a helpful description of the problem. Please explain *how* it doesn't work. Does it fail to compile? Does it behave unexpectedly? How does the observed behavior differ from what you expected? – François Andrieux Feb 20 '18 at 19:55
  • It just runs as if there's nothing inside the constructor. Doesn't ask for name or anything. – Richardo Feb 20 '18 at 19:59
  • 1
    *I'm told to read a name from a constructor* That is terrible. That is very poor practice. If you can, find a better instructor. It will be better to gather the input in `main` and construct the `Animal` object from the data read in `main`. – R Sahu Feb 20 '18 at 20:06
  • If you have written code that you think should do what you are trying to do but doesn't work then you should post that code, assuming it is minimal, complete, and verifiable. – Garrett Gutierrez Feb 20 '18 at 20:10
  • 1
    By the way, your instinct is correct - the constructor should take this information as an argument instead, in a good design. – Lightness Races in Orbit Feb 20 '18 at 20:27

3 Answers3

0
#include  <iostream>
#include <sstream>

class Animal
{
public:
 Animal() : name()
{
    // Not the best design approach.Just show it possible for the question.
    std::cout << "Name of animal?" << std::endl;
    std::getline(std::cin, name);
    std::cout << name << std::endl;
}
private: 
   std::string name;
};

int main(int argc, char * argv[])
{
Animal a1; // should ask for name and read it, add it to data
return 0;
}
Santhosh
  • 1
  • 2
0

I believe the code below is self explanatory with comments to guide you. In object oriented, a class should contain setter and getter methods. I have created a class Animal with one private string variable name. In the constructor, I ask for a name and assign it to the name variable of the object which is being created. I then display the name using a method called getName() which returns the current object's name and it is also known as a getter method. I believe you are new to Object Oriented and I hope I have made these concepts understandable to you.

#include <iostream>

using namespace std;

class Animal
{
  private:string name;

  public: Animal() {
        cout<<"Enter the animal's name?";
        getline(cin,this->name); //sets the current obj's name (storing the value)
        cout<<"The animal's name is "<<getName(); 
    }

  public: string getName(){ return this->name; } //return current obj's name value (getter method)

};

int main()
{
    Animal a1;
    //cout<<a1.getName(); //it will get the name of a1's object
    return 0;
}
atish.s
  • 1,534
  • 11
  • 19
0
 #include <iostream>
using namespace std; //Don't kill me

class animal
{
private:

    char name [20];
public:
    animal () //Constructor without parameters
    {
        cout<<"Please enter animal name: ";
            cin>>name;
    }

    void getAnimal();
};

void animal :: getAnimal()
{
    cout<<name; 
}

int main ()
{
animal a1;
     a1.getAnimal();
}

Remember that there are 3 types of constructors. In this case it seems that you have to use a default constructor which does require a parameter as it is just setting name to a default value. To get a user defined value you can use cin within the constructor. When you create an object in main and run your program it will allow the user to enter a name. To read and print the name I found that a getter method was easier. https://www.geeksforgeeks.org/constructors-c/