C++ class inheritance

Hi, I need help with my '=' assignment operator as shown below. Basically I need to also assign an int value along with the point. Refer below. May I know how I can achieve that? Your help will be much appreciated.

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


class Circle : public Point 
	{ 
		private:
		Point p1;
		// Point p2;
		int x;
	public:
		Circle() : p1(Point(0, 0)) {}
		Circle(const Point & p1, int value) : p1(p1), x(value) {}

	public:
 
   friend std::ostream& operator<<(std::ostream& stream, Circle& circle);
   Circlee& operator= (const Circle&);
   Circlee& operator= (int value);
	};

	std::ostream& operator<<(std::ostream& stream, Circle& circle)
	{
		std::cout << "Circle: " << circle.p1 << ", " << circle.value << 
                '\n';
		return stream;
	}

	Circle& Circlee::operator= (const Circle& a) // need to have int value 
	{
		p1 = a.p1;

		return *this;
	}

	int main() //No modifications
{       Circlee c1; 				//Output: Circle construction
	Circlee c3(Point(), 10);	//Output: Circle construction
	c1 = c3;
	std::cout << c1; 		//Output: Circle (0,0) 10
	
		return 0; 			
} 		
Last edited on
Perhaps you meant this?
1
2
3
4
5
6
7
	Circle& Circle::operator= (const Circle& a) // need to have int value 
	{
		p1 = a.p1;
                x = a.x;

		return *this;
	}


Or if you meant to, you could introduce another overload to assignment which takes an int as argument.
1
2
3
4
5
6
	Circle& Circle::operator= (const int& a) // need to have int value 
	{
		x = a;

		return *this;
	}

If that's what you want to achieve then you probably also want an overload accepting Point.
1
2
3
4
5
6
	Circle& Circle::operator= (const Point& a) // need to have int value 
	{
		p1 = a;

		return *this;
	}


But it wouldn't make sense to use the assignment operator for modifying particular data members.
You could instead use methods.
void Circle::set_int (int value) { x = value; }

void Circle::set_point (Point value) { p1 = Point; }
Topic archived. No new replies allowed.