-1

I am new in programming and i wanted to know how can I input data from keyboard from class. Anyone?

#include <iostream>
#include <string>
using namespace std;

class Human{
private:
    string *name;
    int *age;
public:
    Human(string iname, int iage){
    name = new string;
    age = new int;

    *name = iname;
    *age = iage;
}
void display(){
    cout << "Hi I am " << *name << " and I am " << *age << " years old"  << endl;
}
~Human(){
delete name;
delete age;
cout << "Destructor!";
}
void input(string, int)
{
string name;
int age;
cout << "Name: "; cin >> name;
cout << "Age: "; cin >> age;
}
};

int main()
{

    Human *d1 = new Human(Human::input(?????????????????));
    d1->display();
    delete d1;
    return 0;
}  

EDIT:

I understand what I can do this:

int main()
{

    Human *d1 = new Human("David",24);
    d1->display();
    return 0;
}

And this:

  int main()
{
    string name;
    int age;
    cout << "Name: "; cin >> name;
    cout << "Age: "; cin >> age;
    Human *d1 = new Human(name,age);
    d1->display();
    return 0;
}

But I want to know how can I put the data from keyboard with an input function.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Zygis
  • 13
  • 3
  • There's a lot of misunderstanding in your code. – LogicStuff Dec 12 '15 at 23:22
  • 2
    The use of `string*` is a strong indication that you need to go through [The Definitive C++ Book Guide and List](http://stackoverflow.com/q/388242/1889329). – IInspectable Dec 12 '15 at 23:23
  • 1
    There are plenty of problems in that code that stem from your lack of understanding. The best way to correct that is to read a textbook or tutorial, rather than a forum like this - all half-decent texts/tutorials describe what you need to know to avoid problems like this. – Peter Dec 12 '15 at 23:24

1 Answers1

0

Zygis you need to read a tutorial for the basics in C++. Pointers, the *, is a powerful thing programmers can use, but only when they need to. In this case, you do not need to. When do you need them? I think you should leave that for later and focus on my example below. When you understand that, you can read about Pointers in the internet.

#include <iostream>
#include <string>
using namespace std;

class Human {
private:
    // data members of class
    string name;
    int age;
public:
    // constructor without arguments
    // useful for initializing the data members
    // by an input function
    Human() {
        name = ""; // empty string
        age = -1;  // "empty" age
    }

    // constructor with arguments
    // useful when you know the values
    // of your arguments
    Human(string arg_name, int arg_age) {
        name = arg_name;
        age = arg_age;
    }

    void display() {
        cout << "Hi I am " << name << " and I am " << age << " years old"
                << endl;
    }

    // the data members will go out of scope automatically
    // since we haven't used new anywhere
    ~Human() {
        cout << "Destructor, but the default one would be OK too!\n";
    }

    // prompt the user to giving values for name and age
    void input() {
        cout << "Please input name: ";
        cin >> name;
        cout << "Please input age: ";
        cin >> age;
    }
};

int main() {

    // Let the user initialize the human
    Human human_obj_i;
    human_obj_i.input();
    human_obj_i.display();

    cout << "Now I am going to auto-initialize a human\n";

    // Let the program itseld initialize the human
    Human human_obj("Samaras", 23);
    human_obj.display();

    return 0;
}

Example run:

Please input name: Foo
Please input age: 4
Hi I am Foo and I am 4 years old
Now I am going to auto-initialize a human
Hi I am Samaras and I am 23 years old
Destructor, but the default one would be OK too!
Destructor, but the default one would be OK too!

Hope that helps! ;)

gsamaras
  • 71,951
  • 46
  • 188
  • 305