2016-01-19 81 views
-2

這裏我沒有得到正確的輸出,在這裏我使用了繼承概念,但我不知道如何使用指針類型的對象調用方法。指向繼承中的對象

請給我一些解決方案。

#include <iostream> 
#include <cstring> 
#include <iomanip> 
using namespace std;  

class person 
{ 
private: 
    char *name,*blood,*dob; 
    int height,weight; 
public: 
    static int count; 
    person() 
    { 
     strcpy(name,"Name"); 
     strcpy(blood,"Blood"); 
     height=0; 
     weight=0; 
    } 
    ~person() 
    { 
    } 
    friend class person_info; 
}; 


class person_info 
{ 
private: 
    char *add,*tel,*lic,*insu; 
public: 
    void getdata(person *obj,int n); 
    void display(person *obj); 

}; 

void person_info::getdata(person *obj,int n) 
{ 
    for(int i=0;i<n;i++) 
    { 
     cin.ignore(); 
     cout<<"Enter Name Of the Person : "; 
     cin.getline(obj->name,30); 

     cout<<"Enter Blood group Of the Person : "; 
     cin.getline(obj->blood,30); 

     cout<<"Enter date of birth of the Person : "; 
     cin.getline(obj->dob,30); 

     cout<<"Enter Height of the Person : "; 
     cin>>obj->height; 

     cout<<"Enter Weight of the Person : "; 
     cin>>obj->weight; 

     cout<<"Enter Address Of the Person : "; 
     cin.getline(this->add,30); 

     cout<<"Enter Insurance no. Of the Person : "; 
     cin.getline(this->insu,30); 

     cout<<"Enter Telephone no Of the Person : "; 
     cin.getline(this->tel,30); 

     cout<<"Enter License no the Person : "; 
     cin.getline(obj->blood,30); 
    } 
} 
void person_info::display(person *obj) 
{ 
    cout<<setw[10]<<"Name"<<setw[10]<<"Address"<<setw[10]<<"D. O. B."<<setw[3]<<"Blood G."<<setw[4]<<"Height"<<setw[10]<<"Weight"<<setw[10]<<"Insrn No."<<setw[10]<<"Tele No."<<setw[11]<<"Licence No."; 
    cout<<setw[10]<<obj->name<<setw[10]<<this->add<<setw[10]<<obj->dob<<setw[3]<<obj->blood<<setw[4]<<obj->height<<setw[10]<<obj->weight<<setw[10]<<this->insu<<setw[10]<<this->tel<<setw[11]<<this->lic; 
} 

int main() 
{ 
int ch=0,i=0,n; 
    do 
    { 
     cout<<"1.getdata"<<endl; 
     cout<<"2.display data"<<endl; 
     cout<<"Enter choice"; 
     cin>>ch; 
     person_info *p[2];  \\I think this part is not correct 
     p[2]=new person_info(); \\I think this part is not correct 
     person *p1[2];   \\I think this part is not correct 
     p1[2]=new person();  \\I think this part is not correct 
     switch(ch) 
     { 
     case 1: 
      cout<<"Enter No. Entries to be Entered :"; 
      cin>>n; 
      p[2]->getdata(p1[2],n); 
      break; 
     case 2: 
      for(int j=0;j<i;j++) 
      { 
       p[i]->display(p1[i]); 
      } 
      break; 
     } 
    }while(ch!=3); 
+1

你從來沒有爲'name','strcpy(name,「Name」)分配內存;'這個調用了未定義的行爲。而是使用'std :: string'而不是'char *'。 –

+0

'p [2] = new person_info();'訪問數組末尾的一個。這會導致未定義的行爲。 – NathanOliver

+0

你聲稱是「使用繼承」,但在你的代碼中實際上沒有繼承。你的意思是你的一個「人」班是否從另一個班繼承? – Edward

回答

3

您既沒有初始化您的成員char *name,*blood,*dob;,也沒有爲它們分配內存。我建議使用std::string而不是char*

#include <string> 

class person 
{ 
    private: 
    std::string name; 
    std::string blood; 
public: 
    person() 
     : name("Name") 
     , blood("Blood") 
    {} 
    person(const char *n, const char *b) 
     : name(n) 
     , blood(b) 
    {} 
}; 

注:strcpy(name,"Name");的字符串複製"Name"char *name引用的內存,但你永遠不分配任何動態內存namename未初始化且未定義。