【发布时间】:2015-12-12 23:14:24
【问题描述】:
我是编程新手,我想知道如何在课堂上从键盘输入数据。任何人?
#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;
}
但我想知道如何将键盘中的数据与输入功能一起输入。
【问题讨论】:
-
你的代码有很多误解。
-
string*的使用强烈表明你需要通过The Definitive C++ Book Guide and List。 -
该代码中有很多问题源于您缺乏理解。纠正这种情况的最佳方法是阅读教科书或教程,而不是像这样的论坛 - 所有半体面的文本/教程都描述了避免此类问题所需了解的内容。