Answer to Question #199967 in C++ for zaid

Question #199967

Define a class person which has two data members they are

1- pointer to character to store address of memory block to store name of person .

2-CNIC data member to store ID number .

Class person also has a constructor and destructor use class person as parent class and define child class student has two data members . They are pointer to character and roll no. Note that child class also has a constructor and destructor in addition child class has two more member functions to get data and display data . In main function create an object of class student and display information of student?


1
Expert's answer
2021-05-28T10:31:27-0400
#include <iostream> 
#include <string>


using namespace std;


class Person{
private:
	//1- pointer to character to store address of memory block to store name of person .
	char* name;
	//2-CNIC data member to store ID number .
	int ID;
public:
	//constructor 
	Person(){
		this->name=(char*)malloc(30*sizeof(char));
		this->ID=0;
	}
	~Person(){
		delete name;
	}
	virtual void getData(){
		cout<<"Enter the name: ";
		gets(name);
		cout<<"Enter ID: ";
		cin>>ID;
		cin.ignore();
	}
	virtual void displayData(){
		cout<<"The name: "<<name<<"\n";
		cout<<"ID: "<<ID<<"\n";
	}
};
class Student:public Person{
private:
	char* college_name;
	int rollNo;
public:
	//constructor 
	Student(){
		this->college_name=(char*)malloc(30*sizeof(char));
		this->rollNo=0;
	}


	~Student(){
		delete college_name;
		
	}


	void getData(){
		Person::getData();
		cout<<"Enter the college name: ";
		gets(college_name);
		cout<<"Enter roll no: ";
		cin>>rollNo;
	}
	void displayData(){
		Person::displayData();
		cout<<"The college name: "<<college_name<<"\n";
		cout<<"Roll no: "<<rollNo<<"\n";
	}
};


int main (){
	//In main function create an object of class student and display information of student
	Student* student=new Student();
	student->getData();
	student->displayData();


	delete student;
	//delay
	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