如何在c ++ OOP中显示我的ID Name Age

时间:2018-05-26 16:53:03

标签: c++ function class oop

您好,我刚开始使用c ++ OOP。我无法显示我的身份姓名年龄,请帮助我,我试着找到问题,但我不能。我在运行时知道名字的年龄,表示你好我的名字是:我的年龄是:0我的身份证是: 4354512 谢谢你。

#include<iostream>
#include<conio.h>
using namespace std;

class person
 {
  public:
   string name;
   int age;
   int id;
   void set_name( string myname)
    {
     myname = name;
    }
   void set_age( int myage)
   {
    myage = age;
   }
  void set_id( int myid)
  {
    myid = id;
  }
};
class student : public person
 {
  public:
   void display()
   {
    cout<<"Hello my Name is : "<<name<<endl;
    cout<<"My age is : "<<age<<endl;
    cout<<"My ID is : "<<id<<endl;
   }
 };
int main()
 {
  student obj;
  obj.set_name("NuthVireak");
  obj.set_age(19);
  obj.set_id(004);
  obj.display();
  getch();
 }

2 个答案:

答案 0 :(得分:1)

你的二传手被逆转:

void set_name( string myname)
{
    // you are setting the local parameter myname
    // to the member name which is probably defaulted to zero
    // myname = name;
    // try
    name = myname;
}
void set_age( int myage)
{
    // same here
    // myage = age;
    age = myage;
}
void set_id( int myid)
{
    // same here
    // myid = id;
    id = myid;
}

答案 1 :(得分:-1)

void set_name( string myname)
{
 this->name = myname;
}



void set_age( int myage)
   {
    this->age = myage;
   }

void set_id( int myid)
  {
    this->id = myid;
  }