-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;
}
但我想知道我怎么可以把数据从具有输入功能的键盘。
你的代码有很多误解。 – LogicStuff
使用'string *'是一个强烈的暗示,你需要通过[The Definitive C++ Book Guide and List](http://stackoverflow.com/q/388242/1889329)。 – IInspectable
该代码中存在很多源于缺乏理解的问题。纠正这个问题的最好方法是阅读教科书或教程,而不是像这样的论坛 - 所有半面体文本/教程描述了你需要知道的避免这样的问题。 – Peter