Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,859 questions

51,780 answers

573 users

How to use union with constructor and method in C++

1 Answer

0 votes
#include <iostream>

using std::cout;
using std::endl;

union Test {
	Test(int number);  
	void show(void);
	int n;
	char arr[4];
};

Test::Test(int number)
{
	n = number;
}

void Test::show(void)
{
	cout << arr[0] << " ";
	cout << arr[1] << "";
	cout << arr[2] << "";
	cout << arr[3] << endl;
}

int main(void)
{
	Test u1(97);
	u1.show();
	
	Test u2(987);
	u2.show();

	return 0;
}

/*
run:

a
█ ♥

*/

 



answered Mar 17, 2018 by avibootz

Related questions

1 answer 114 views
4 answers 263 views
1 answer 153 views
153 views asked Dec 11, 2020 by avibootz
1 answer 136 views
136 views asked Mar 17, 2018 by avibootz
...