Answer to Question #184116 in C++ for Uzair Ali

Question #184116

Q.2. Copy constructor

 Define class Student with the following attributes and member functions:

i. Date of Birth (constant data member)

ii. Pointer to char for name

iii. Pointer to char for Department

iv. Student ID (constant data member)

v. Member function Constructors

vi. Member function Destructor

vii. Member function to modify state of the object

viii. Member function to display state of the object (should be 

constant)

Note that pointers in (ii) and (iii) should point to the dynamic memory allocated 

for name and Department name. Get information of the student object in 

constructor. Memory for name and Department will be allocated in constructor

using dynamic memory allocation. Destructor will release memory to the 

operating system.

Q.3. Extend Q.2 for shallow and deep copy constructor. 


1
Expert's answer
2021-04-24T15:14:04-0400
#include <iostream>
#include <ctime>
using namespace std;

class Student
{
public:
//task Q.2
	Student(const char* Name, const char* Department, const int ID, const tm DateOfBirth)
			: m_Name(NULL), m_Department(NULL), m_ID(ID), m_DateOfBirth(DateOfBirth)
	{
		SetName(Name);

		SetDepartment(Department);
	}
	~Student()
	{
		if(m_Name) {delete[] m_Name; m_Name = NULL;}

		if(m_Department) {delete[] m_Department; m_Department = NULL;}
	}

	const char* GetName() const {return m_Name;}
	const char* GetDepartment() const {return m_Department;}
	const int GetID() const {return m_ID;}
	const tm GetDateOfBirth() const {return m_DateOfBirth;}

	void SetName(const char* Name)
	{
		if(m_Name) delete m_Name;

		m_Name = new char[strlen(Name) + 1];
		std::strcpy(m_Name, Name);
	}
	void SetDepartment(const char* Department)
	{
		if(m_Department) delete m_Department;

		m_Department = new char[strlen(Department) + 1];
		std::strcpy(m_Department, Department);
	}
//task Q.3 uncomment copy constructor you need for demonstration
	//shallow copy
//	Student(const Student& otherStudent) : m_Name(NULL), m_Department(NULL), m_ID(otherStudent.m_ID), m_DateOfBirth(otherStudent.m_DateOfBirth)
//	{
//		m_Name = otherStudent.m_Name;
//		m_Department = otherStudent.m_Department;
//	}
	//deep copy
	Student(const Student& otherStudent) : m_Name(NULL), m_Department(NULL), m_ID(otherStudent.m_ID), m_DateOfBirth(otherStudent.m_DateOfBirth)
	{
		SetName(otherStudent.m_Name);
		SetDepartment(otherStudent.m_Department);
	}

private:
	char* m_Name;
	char* m_Department;

	const int m_ID;
	const tm m_DateOfBirth;//time struct from <ctime>
};

int main()
{
	tm Date = {0};
	   Date.tm_mday = 1;
	   Date.tm_mday = 1;
	   Date.tm_year = 1995;

	//for shallow/deep copy demonstration, uncomment corresponding piece of code in class definition
	{
		Student student_1("Mike", "Chemistry", 112233, Date);

		Student student_2(student_1);

		//now explicitly delete student_2 and student_1
		student_2.~Student();//it is all right
		student_1.~Student();//in case of shallow copying here will be an error!!!
							 //in case of deep copying - everything goes alright
	}

	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
APPROVED BY CLIENTS