指向继承中的对象的指针
Pointer to an object in inheritance
在这里我没有得到正确的输出,这里我使用了继承概念,但我不知道如何使用指针类型的对象调用方法。
请有人给我一些解决方案。
#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);
您既没有初始化成员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
引用的内存中,但您永远不会为 name
分配任何动态内存。 name
未初始化和未定义。
相关文章:
- 对象指针在c++中是如何工作的
- C++ 对象指针数组的复制构造函数
- 在对象指针上调用 Delete 是否会递归删除其动态分配的成员
- 什么更好?返回对象指针列表?或返回指向对象列表的指针?
- 正确初始化和销毁对象指针的C++数组?
- 如何深度复制链表对象指针
- 对象指针 c++ 的全局向量错误
- 如何将 c++ 类包装到 python 中,以便我可以使用 pybind11 访问其成员的公共方法(成员是一个对象指针)
- 静态对象指针
- 正在将对象指针数组初始化为NULL
- 如何使用条件表达式返回对象指针?
- std::flush可以用于将对象指针转换为其封闭数组指针吗
- 使用C对象指针构建PyObject*
- 如何在使用对象指针时访问成员函数
- 静态强制转换允许转换对象指针,但不允许转换整数
- C++ abort() 在函数内的抽象类对象指针调用上
- 指向函数的对象指针
- 访问指向对象指针向量的指针的第一个元素?
- 如何将对象/指针正确存储到 Qlist 中
- 对象指针打印结果以相反的顺序进行