Type Conversion of class to another

Hello guys! Here is the task : "Write another class Person having the following attributes:
1. String name
2. Integer Age
Convert an object of type Employee to person".
Now I made another class Person in which I want my entered name and age of Employee to be copied. So I did it at line 32 and 33. And then I cout at line 63. But its showing garbage value. It should display the name and age I entered for employees object.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  #include <iostream>
#include <string>
using namespace std;
class employee
{
public:
	string name;
	int age;
	int salary;
	friend istream &operator >> (istream &input, employee &obj);
	friend ostream  &operator << (ostream &output, employee &obj);
	
	int operator ! ()
	{

		return salary / 2;

	}

};
class Person :public employee
{
public:
	string name1;
	int age1;
	Person()
	{

	}
	void operator = (employee &obj)
	{
		name1 = obj.name; 
		age1 = obj.age;
	}

};
istream &operator >> (istream &input, employee &obj)
{
	cout << "Enter name :" << endl;
	input >> obj.name;
	cout << "Enter age :" << endl;
	input >> obj.age;
	cout << "Enter salary :" << endl;
	input >> obj.salary;
	return input;
}
ostream  &operator << (ostream &output, employee &obj)
{

	output << (obj.name) << endl;
	output << (obj.age) << endl;
	return output;
}
int main()
{
	employee obj;
	Person obj1;
	cin >> obj;
	cout << obj;
	int a = !obj;
	cout << a;
	 obj1=obj;  // person = employee
	cout << obj1;
	system("pause");
}

Why is Person an Employee?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>
using namespace std;

struct employee
{
	string name;
	int age;
	int salary;
	
	int operator ! ()
	{
		return salary / 2;
	}
};

istream &operator >> (istream &input, employee &obj)
{
  return input >> obj.name >> obj.age >> obj.salary;
}

ostream  &operator << (ostream &output, employee &obj)
{
  return output << obj.name << '\n' << obj.age << '\n';
}

struct Person
{
	string name;
	int age;

	Person& operator= ( employee &obj )
	{
		name = obj.name; 
		age = obj.age;
		return *this;
	}
};

int main()
{
	employee obj;
	cin >> obj;
	cout << obj;

	Person bob;
	bob = obj;
	cout << bob.name << '\t' << bob.age << '\n';
}
1
2
3
4
5
6
Person& operator= ( employee &obj )
	{
		name = obj.name; 
		age = obj.age;
		return *this;
	}

Why you are doing this?
I think Person should have its own data members e.g name1, and age1 where the Previous entered name should be stored! And why not just write cout << bob ; in main to print out both the data members! I am getting confused because we are just using Employees data members.
My Person is not an employee.
Topic archived. No new replies allowed.