Array of class with inheritance
Hi,all and thanks in advance. Shape base class, line and Point derived classes.
What should I declare in .h files and implement in .cpp files that this is array will be work.
My major concern refer to operator [] and assign (=) operator. As far as I understand I should overload ([]) and (=) three times for classes shape , line and point or not... or is it possible made through virtual function? How will be code looks like ?
Code:
// part of main.cpp
Shape* shapes[3]; // Array of pointers to Shape
shapes[0] = new Shape();
shapes[1] = new Line ("line from array ", Point(1,22),Point(33,22));
shapes[2] = new Point(11,44);
cout << "using ToString function" << endl;
for(int i=0; i < 3; i++)
cout << s[i]->ToString();
for(i=0; i < 3; i++)
delete s[i];
Re: Array of class with inheritance
Code:
#include <string>
#include <iostream>
using namespace std;
class Shape
{
public:
virtual string ToString() { return "Shape"; }
};
class Point: public Shape
{
public:
Point(int x, int y): Shape()
{
}
virtual string ToString() { return "Point"; }
};
class Line: public Shape
{
public:
Line(string str, const Point& pt1, const Point& pt2): Shape()
{
}
virtual string ToString() { return "Line"; }
};
int main()
{
Shape* shapes[3]; // Array of pointers to Shape
shapes[0] = new Shape();
shapes[1] = new Line ("line from array ", Point(1,22),Point(33,22));
shapes[2] = new Point(11,44);
cout << "using ToString function" << endl;
for(int i=0; i < 3; i++)
{
cout << shapes[i]->ToString() << endl;
}
for(int i=0; i < 3; i++)
{
delete shapes[i];
}
return 0;
}
Re: Array of class with inheritance
Спасибо большое Игорь ! ))
С уважением и благодарностью, Денис )
Thank you very much Igor!
Best regards,
Denis )
Re: Array of class with inheritance
Igor one more question. Main purpose of this exercise is implementation of virtual destruction, so could you help me. I'm correctly understand that I should just put declare in Shape class that this class has virtual destructor and this will all changes in the code ? I put all formulation of the the exercise below:
So in term of code I mean that I should add next line:
Code:
#include <string>
#include <iostream>
using namespace std;
class Shape
{
public:
virtual string ToString() { return "Shape"; }
virtual ~Shape(); //!!!!
};
class Point: public Shape
{
public:
Point(int x, int y): Shape()
{
}
virtual string ToString() { return "Point"; }
};
class Line: public Shape
{
public:
Line(string str, const Point& pt1, const Point& pt2): Shape()
{
}
virtual string ToString() { return "Line"; }
};
int main()
{
{
Shape* shapes[3]; // Array of pointers to Shape
shapes[0] = new Shape();
shapes[1] = new Line ("line from array ", Point(1,22),Point(33,22));
shapes[2] = new Point(11,44);
cout << "using ToString function" << endl;
for(int i=0; i < 3; i++)
{
cout << shapes[i]->ToString() << endl;
}
for(int i=0; i < 3; i++)
{
delete shapes[i];
}
system ("pause");
}
return 0;
}
wording of exercise :
Virtual Destructors
When objects are removed from memory, the destructor is called. When a derived class destructor is called, it will automatically call the base class destructor. But when you have pointers to a base class, deleting objects might not be done correctly.
If not done already, print some text in the destructors of the Shape, Point and Line classes. Then test the following code:
Code (cpp):
Code:
Shape* shapes[3];
shapes[0]=new Shape;
shapes[1]=new Point;
shapes[2]=new Line;
for (int i=0; i!=3; i++) delete shapes[i];
Will the proper destructors (including the destructor of the Shape base class) be called?
In this case, the derived class destructor will only be called when the destructor is declared virtual in the base class. Do this in the Shape class and run the code again. Are the proper destructors called now?
Re: Array of class with inheritance
Without virtual in the base, destructing a derived would invoke just the base destructor. With virtual, it would invoke the base and derived classes destructors.
The only destructor I see in your implementation though is the base class, so without adding destructors to the others they won't be properly called (or at least doing what you may specifically want them to do).
Re: Array of class with inheritance
Quote:
Originally Posted by
chrisGz
Without virtual in the base, destructing a derived would invoke just the base destructor. With virtual, it would invoke the base and derived classes destructors.
The only destructor I see in your implementation though is the base class, so without adding destructors to the others they won't be properly called (or at least doing what you may specifically want them to do).
Please could you check correctness:
Code:
#include <string>
#include <iostream>
using namespace std;
class Shape
{
public:
Shape() {cout << "I'm Shape def constructor "<< endl;}
virtual string ToString() { return "Shape"; }
virtual ~Shape(){cout << "I'm shape killer "<<endl;; } //!!!!
};
class Point: public Shape
{
public:
Point() {cout << "I'm Point def constructor "<< endl;}
Point(int x, int y): Shape()
{
}
~Point(){cout << "I'm point killer "<<endl; ;} //!!!!
virtual string ToString() { return "Point"; }
};
class Line: public Shape
{
public:
Line() {cout << "I'm Line def constructor "<< endl;}
Line(string str, const Point& pt1, const Point& pt2): Shape()
{
}
~Line(){cout << "I'm line killer "<<endl; } //!!!!
virtual string ToString() { return "Line"; }
};
int main()
{
{
Shape* shapes[3]; // Array of pointers to Shape
shapes[0] = new Shape();
shapes[1] = new Line ("line from array ", Point(1,22),Point(33,22));
shapes[2] = new Point(11,44);
cout << "using ToString function" << endl;
for(int i=0; i < 3; i++)
{
cout << shapes[i]->ToString() << endl;
}
for(int i=0; i < 3; i++)
{
delete shapes[i];
}
}
system ("pause");
return 0;
}
Few more questions if you don't mind
If it is correct could you in 3 words tell how it's work in term of preventing memory leak?
Why in my console I saw in very begging 4 times "I'm Shape def constructor " ??? it is correct ???
I'm not sure that I understand meaning and purpose of inheritance in this line, could you explain in 3 words
Code:
...
Line(string str, const Point& pt1, const Point& pt2): Shape() // << and I not sure that I understand logic of this //inheritance
...
Point(int x, int y): Shape()// << and I not sure that I understand logic of this inheritance
Many thanks !!!
Re: Array of class with inheritance
You have an open and end brace in main that shouldn't be there. Besides that, if when you destruct a line for example, you'll destruct the line and the shape. If that's what you want, then correct.
It works for the memory leak in that it constructs both the base and derived and when you delete, it deletes both instances too.
For the constructor part, again, it depends on what you want to do.
For the inheritance, that just means it explicitly calls the specified base constructor when constructing the derived instance.
Re: Array of class with inheritance
Quote:
Originally Posted by
chrisGz
You have an open and end brace in main that shouldn't be there. Besides that, if when you destruct a line for example, you'll destruct the line and the shape. If that's what you want, then correct.
It works for the memory leak in that it constructs both the base and derived and when you delete, it deletes both instances too.
For the constructor part, again, it depends on what you want to do.
For the inheritance, that just means it explicitly calls the specified base constructor when constructing the derived instance.
I'm not sure that I what that anything was deleted before "using ToString functions" appear... So , because of this how should I change my code?
about open and end brace it needed me for control such destructor is worked and in which order
Re: Array of class with inheritance
Quote:
I'm not sure that I understand meaning and purpose of inheritance in this line, could you explain in 3 words
It's not inheritance itself, it's instance initialization.
Quote:
Code:
Line(string str, const Point& pt1, const Point& pt2): Shape() // << and I not sure that I understand logic of this
Literally this means: prior to calling constructor body, call base class Shape constructor.
Quote:
Why in my console I saw in very begging 4 times "I'm Shape def constructor " ??? it is correct ???
Well, I think it should be 5 ones:- new Shape
- implicit Point(int, int) calling Shape (see new Line)
- implicit Point(int, int) calling Shape (see new Line)
- explicit Line calling Shape
- explicit Point(int, int) calling Shape
Re: Array of class with inheritance
Quote:
I'm not sure that I what that anything was deleted before "using ToString functions" appear...
There two temporary Point instances will be deleted immediately after constructing new Line. Did you mean that?
Code:
shapes[1] = new Line ("line from array ", Point(1,22),Point(33,22));
Then if you don't want those to be destroyed implicitly, you have to change the code. ;)
Re: Array of class with inheritance
Quote:
Originally Posted by
chrisGz
Without virtual in the base, destructing a derived would invoke just the base destructor. With virtual, it would invoke the base and derived classes destructors.
The only destructor I see in your implementation though is the base class, so without adding destructors to the others they won't be properly called (or at least doing what you may specifically want them to do).
1. Declaring a function in the base class “virtual” is sufficient for all derived classes function of the same name be virtual.
2. Compiler will generate the destructor for a class if it is not declared.
So in oteel’s code the compiler-generated virtual destructor for all the derived classes will get called.
Re: Array of class with inheritance
Quote:
Originally Posted by
chrisGz
Without virtual in the base, destructing a derived would invoke just the base destructor.
Actually, if there is no virtual destructor in the base class, the behaviour is undefined if you destroy a derived object using a pointer to the base class.
Regards,
Paul McKenzie
Re: Array of class with inheritance
Quote:
Originally Posted by
Igor Vartanov
There two temporary Point instances will be deleted immediately after constructing new Line. Did you mean that?
Code:
shapes[1] = new Line ("line from array ", Point(1,22),Point(33,22));
Then if you don't want those to be destroyed implicitly, you have to change the code. ;)
Quote:
Then if you don't want those to be destroyed implicitly, you have to change the code. ;)
yes, Igor)