Answer to Question #180616 in C++ for khanzai

Question #180616

  program in which each destructor should display different message e.g. for object 1 the message should be like this: “Object 1 is going to destroy”


1
Expert's answer
2021-04-12T08:51:33-0400
#include <iostream>
#include <string>


class ExampleFirst
{
public:
	void setDataset(std::string name, int value) 
	{
		m_name = name;
		m_value = value;
	}
	void viewDataset() {
		std::cout << "name: " << m_name << std::endl;
		std::cout << "value: " << m_value << std::endl;
	}
	~ExampleFirst()
	{
		std::cout << "object of class ExampleFirst will be destroyed" << std::endl;
	}


private:
	std::string m_name;
	int m_value{0};
};
class ExampleSecond
{
public:
	void setDataset(std::string name, int value)
	{
		m_name = name;
		m_value = value;
	}
	void viewDataset() {
		std::cout << "name: " << m_name << std::endl;
		std::cout << "value: " << m_value << std::endl;
	}


	ExampleSecond(std::string n, int v)
	{
		m_name = n;
		m_value = v;
	}
	~ExampleSecond()
	{
		std::cout << "object of class ExampleSecond with slanderous data " << std::endl;
		viewDataset();
		std::cout << "will be destroyed" << std::endl;
	}


private:
	std::string m_name;
	int m_value{ 0 };
};
int main()
{
	// example test 
	ExampleFirst first;
	first.setDataset("test1", 1);
	ExampleSecond second("test2", 2);
	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