2015-12-12 63 views
-1

我是編程新手,我想知道如何從類的鍵盤輸入數據。任何人?C++無法從鍵盤輸入tada

#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; 
} 

編輯:

我明白我可以這樣做:

int main() 
{ 

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

而且這樣的:

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

但我想知道我怎麼可以把數據從具有輸入功能的鍵盤。

+0

你的代碼有很多誤解。 – LogicStuff

+2

使用'string *'是一個強烈的暗示,你需要通過[The Definitive C++ Book Guide and List](http://stackoverflow.com/q/388242/1889329)。 – IInspectable

+1

該代碼中存在很多源於缺乏理解的問題。糾正這個問題的最好方法是閱讀教科書或教程,而不是像這樣的論壇 - 所有半面體文本/教程描述了你需要知道的避免這樣的問題。 – Peter

回答

0

Zygis你需要閱讀C++基礎知識教程。指針,*,是程序員可以使用的強大的東西,但只需要。在這種情況下,你不需要。你什麼時候需要他們?我認爲你應該稍後離開,並關注下面的例子。當你明白這一點時,你可以閱讀互聯網上的指針。

#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; 
} 

運行示例:

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! 

希望幫助! ;)

+1

非常感謝!它真的幫了大忙! – Zygis