#include <iostream.h>
typedef unsigned short USHORT;
class SimpleCircle
{
public:
SimpleCircle();
SimpleCircle(USHORT radius);
SimpleCircle(const SimpleCircle& rhs);
~SimpleCircle();
void SetRadius(USHORT newRadius) { *itsRadius = newRadius; }
USHORT GetRadius() const { return *itsRadius; }
const SimpleCircle& operator++();
const SimpleCircle operator++(int);
SimpleCircle& operator=(const SimpleCircle & rhs);
private:
USHORT *itsRadius;
};
SimpleCircle::SimpleCircle()
{
itsRadius = new USHORT(5);
}
SimpleCircle::SimpleCircle(USHORT radius)
{
itsRadius = new USHORT(radius);
}
SimpleCircle::SimpleCircle(const SimpleCircle& rhs)
{
USHORT val = rhs.GetRadius();
itsRadius = new USHORT(val);
}
SimpleCircle::~SimpleCircle()
{
delete itsRadius;
itsRadius = 0;
}
SimpleCircle& SimpleCircle::operator=(const SimpleCircle & rhs)
{
if(this == &rhs)
return *this;
delete itsRadius;
itsRadius = new USHORT;
*itsRadius = rhs.GetRadius();
return *this;
}
const SimpleCircle& SimpleCircle::operator++()
{
++(itsRadius);
return *this;
}
const SimpleCircle SimpleCircle::operator++(int)
{
SimpleCircle temp(*this);
++(itsRadius);
return temp;
}
int main()
{
SimpleCircle CircleA;
SimpleCircle CircleB(9);
cout << "\nCircleA: " << CircleA.GetRadius();
cout << "\nCircleB: " << CircleB.GetRadius();
++CircleA;
++CircleB;
cout << "\nCircleA: " << CircleA.GetRadius();
cout << "\nCircleB: " << CircleB.GetRadius();
CircleA = CircleB;
cout << "\nCircleA: " << CircleA.GetRadius();
cout << "\nCircleB: " << CircleB.GetRadius();
return 0;
}
Thanks
Need Help =(
Hey, I''m currently learning how to program C++ in "Teach Yourself C++ in 21 Days" and there is a sample program in the book that I''m trying to code. So I coded it and it compiled perfectly and all, but when I go to run it, it gives some "Debug Assertion Failed!" error or something. I have no idea what this means, yet someone said it may have to do with a memory leak. I have searched my code throughly and I''m stumped on what could be the problem, if it is a memory leak. So could somoene check it over and make sure I''m not doing something wrong. It''s a really easy program so it shouldn''t be too hard. It''s not commented or anything because it was supposed to just be a sample program in the book and I feel like I can''t move further on in the book without knowing exactly what I am doing wrong. I even compared my code to the books code and it seems to be about the same, so anywho, please help
*More Information*:
When the Assertion error comes up, it also says:
"Expression:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)"
You may replace all ++(itsRadius) statement with ++(*itsRadius).
After adding the star, you will get an output:
CircleA: 5
CircleB: 9
CircleA: 6
CircleB: 10
CircleA: 10
CircleB: 10
Is this output what you want?
After adding the star, you will get an output:
CircleA: 5
CircleB: 9
CircleA: 6
CircleB: 10
CircleA: 10
CircleB: 10
Is this output what you want?
Ah it worked and it seem so clear now. So without the * on ++itsRadius, I was trying to increment the address the pointer pointed at which is what was causing the problem? The book''s example of this program didn''t have the * on ++itsRadius which is why I didn''t see it when I compared the two. I copied the books example exactly and it didn''t even work so I dunno whats up. Anywho, thanks for the help
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement