Question #200040

define a class person, which has two data member they are pointer to character to store address of memory block to store name of person cnic data member to store id card number



Expert's answer

#include <iostream> 

using namespace std;


class Person{
private:
	char* name;
	int cnic;
public:
	Person(){
		this->name=(char*)malloc(20*sizeof(char));
		this->cnic=0;
	}
	~Person(){
		delete name;
	}
	void getData(){
		cout<<"Enter the person name: ";
		gets(name);
		cout<<"Enter the person ID: ";
		cin>>cnic;
		cin.ignore();
	}
	void display(){
		cout<<"The person name: "<<name<<"\n";
		cout<<"The person ID: "<<cnic<<"\n";
	}
};
int main (){
	
	Person* person=new Person();
	person->getData();
	person->display();


	delete person;
	
	system("pause");
	return 0;
}
LATEST TUTORIALS
APPROVED BY CLIENTS