Answer to Question #200040 in C++ for shafeeq ahmed

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



1
Expert's answer
2021-05-28T10:31:17-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog