I'm trying to implement a = operater to set one circle or rectangle to another and this is giving me all kind of bugs. - #include <iostream.h>
-
-
-
class Shape
-
{
-
private:
-
int x_Center, y_Center;
-
-
public:
-
Shape()
-
{
-
cout <<"In defualt constructer";
-
}
-
Shape(Shape& sh)
-
{
-
x_Center=sh.x_Center;
-
y_Center=sh.y_Center;
-
}
-
// Constructors
-
Shape(int cx, int cy)
-
{
-
x_Center=cx;
-
y_Center=cy;
-
}
-
-
// Member functions
-
int getCenter_X();
-
int getCenter_Y();
-
virtual void draw()=0;
-
};
-
-
class Rectangle : public Shape
-
{
-
private:
-
int length,width;
-
-
public:
-
-
// Default constructor
-
Rectangle (Rectangle& r) : Shape(r)
-
{
-
length=r.length;
-
width=r.width;
-
}
-
-
// Constructor
-
Rectangle (int cx, int cy,int l,int w) : Shape(cx, cy)
-
{
-
length=l;
-
width=w;
-
cout << "In Rectangle constructor" << endl;
-
}
-
-
-
-
// Destructor
-
~Rectangle ()
-
{
-
cout << "In Rectangle destructor" << endl;
-
}
-
-
void draw()
-
{
-
cout <<"In drawing of Rectangle"<<endl;
-
}
-
-
Rectangle & operator=(const Rectangle &obj)
-
{
-
return Rectangle(obj.getCenter_X(),obj.getCenter_Y(),obj.length,obj.width);
-
}
-
// Methodds
-
void printAllValues();
-
};
-
-
class Circle : public Shape
-
{
-
private:
-
int diameter;
-
-
public:
-
-
// Default Constructor
-
Circle ()
-
{
-
cout << "In the default constructor of Circle" << endl;
-
}
-
-
Circle (Circle& r) : Shape(r)
-
{
-
diameter=r.diameter;
-
}
-
// Constructor
-
Circle (int cx, int cy, int dia) : Shape (cx, cy)
-
{
-
cout << "In Circle Constructer" << endl;
-
diameter=dia;
-
}
-
-
// Destructor
-
~Circle ()
-
{
-
cout << "In Circle destructor" << end}
-
}
-
void draw()
-
{
-
cout <<"In drawing of Circle"<<endl;
-
}
-
Circle & operator=(const Circle &obj)
-
{
-
return Circle(getCenter_X(),getCenter_Y(),diameter);
-
}
-
// Methods
-
void printAllValues();
-
};
Could someone please help point me in the right diretion please.
4 1363
Let's look at the situation:
You have a Shape.
Therefor you need a Shape assignment operator.
You have Circle deriving from Shape.
Therefore you need a Circle assignment operator.
In each case the assignment operator must change the this object since iot is that object that in to the left of the operator (and thereby, the hidden firstargument of the assignment operator function).
For Shape:
1) assign the values of the argument to the this object.
2) do not assign the this obect ot itself. -
Shape Shape::operator=(const Shape& rhs)
-
{
-
if (this == rhs) return *this; //nothing to do if self-assignment
-
this->x_Center = rhs.x_Center;
-
this->y_Center = rhs.y_Center;
-
return *this;
-
}
-
For Circle:
1) assign the base class object first
2 assign the values of the argument to the this object.
3) do not assign the this obect ot itself. -
Circle Circle::operator=(const Circle& rhs)
-
{
-
if (this == rhs) return *this; //nothing to do if self-assignment
-
Shape::operator=(*this); //assign the base class object
-
this->diameter= rhs.diameter;
-
return *this;
-
}
-
That said, it does not look like your Circle IS-A kind of Shape since it does not use x_Center or y_Center at all. I question whether your class inheritance model is correct.
Thanks a lot, but shouldn't I create a new object in the heap and then copy all attribtes and then give the pointer to that? Does the code you just exampled make a copy operater?
I am a little confused on why you return itself. Please clarify.
Reuben
gpraghuram 1,275
Recognized Expert Top Contributor
Thanks a lot, but shouldn't I create a new object in the heap and then copy all attribtes and then give the pointer to that? Does the code you just exampled make a copy operater?
I am a little confused on why you return itself. Please clarify.
Reuben
You need not create a new object and retun it.
Becos the function is called by a object and the values you change for this affects the object calling it.
Raghuram
Thanks a lot, but shouldn't I create a new object in the heap and then copy all attribtes and then give the pointer to that? Does the code you just exampled make a copy operater?
I am a little confused on why you return itself. Please clarify.
Reuben
The fact that your assignment operator returns a Circle means that a copy is returned. That means you should have a copy constructor to make that copy.
If you create your own copy on the heap and try to return it, you still need a copy constructor to make the copy top be returned. Plus now you have to delete the copy you made before returning.
It's not my code that requires a copy. It's the fact that the function returns a Circle.
By returning *this, I am calling Circle::Circle( const Circle& rhs), which os your copy copnstructor.
If you don't have one, the compiler trots out its default copy constructor which makes memberwise copies of each of your data members. If all of your data members a built-in types, like int, then this default will work fine.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: raj |
last post by:
I need explanation of the behavior of the following code. When run as it is, it gives error.
//-------------------------<start>------------------
#include <iostream.h>
#include <stdlib.h>
class ABC
{
private: int x;
|
by: |
last post by:
Hi all,
I've been using C++ for quite a while now and I've come to the point where I need to overload new
and delete inorder to track memory and probably some profiling stuff too. I know that discussions of
new and delete is a pretty damn involved process but I'll try to stick to the main information I'm looking for
currently. I've searched around for about the last too weeks and have read up on new and overloading it to
some extent but...
|
by: Zork |
last post by:
Hi, I am a little confused with const functions, for instance (here i am
just overloading the unary+ operator) if i define:
1) Length Length :: operator+ ( void ) const {return * this;}
... I get no error...
2) Length & Length :: operator+ ( void ) const {return * this;}
... I get the error -> 'return' : cannot convert from 'const class Length' to
|
by: Chris Mabee |
last post by:
Hello all, and Merry Christmas,
I'm having a problem understanding an example of an array based
implementation of a stack in a textbook of mine. The code in question
is written below. The syntax is directly as in the book, except for
where I added the comments at the lines I wanted to refer to or to skip
sections of code.
template <class Element_Type>
class Stack
{
|
by: Kelly Mandrake |
last post by:
I've implemented a class with operator+ overloaded and I also provided
my own copy constructor; The problem is my copy constructor is being
called twise and I dont understand why. I believe the copy constructor
is being called when I add the two class objects. But if this is so,
then why because I have overloaded the + operator should I not get the
result from that and not from copy constructor?
I have the following:
int main(int...
| |
by: luca regini |
last post by:
I have this code
class M
{
.....
T operator()( size_t x, size_t y ) const
{ ... Operator overloading A ....}
T& operator()( size_t x, size_t y )
|
by: brzozo2 |
last post by:
Hello, this program might look abit long, but it's pretty simple and
easy to follow. What it does is read from a file, outputs the contents
to screen, and then writes them to a different file. It uses map<and
heavy overloading.
The problem is, the output file differs from input, and for the love of
me I can't figure out why ;p
#include <iostream>
#include <fstream>
#include <sstream>
|
by: y-man |
last post by:
Hi,
I am trying to get an overloaded operator to work inside the class it
works on. The situation is something like this:
main.cc:
#include "object.hh"
#include "somefile.hh"
object obj, obj2 ;
|
by: Colonel |
last post by:
It seems that the problems have something to do with the overloading of
istream operator ">>", but I just can't find the exact problem.
// the declaration
friend std::istream & operator>(std::istream & in, const Complex & a);
// the methods correspond to the friend
std::istream & operator>(std::istream & in, const Complex & a)
{
std::cout << "real: ";
|
by: Wayne Shu |
last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition).
When I read chapter 11 Operator Overloading, I have two questions.
1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,
operator =, operator, operator(), and operator-must be nonstatic
member function; this ensures that their first operands will be
lvalues". I know that these operators must be nonstatic member
functions, but why this ensure their first operands will...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |