CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Jun 2012
    Posts
    127

    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];

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    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;
    }
    Last edited by Igor Vartanov; June 6th, 2012 at 06:50 AM.
    Best regards,
    Igor

  3. #3
    Join Date
    Jun 2012
    Posts
    127

    Re: Array of class with inheritance

    Спасибо большое Игорь ! ))
    С уважением и благодарностью, Денис )

    Thank you very much Igor!
    Best regards,
    Denis )
    Last edited by oteel; June 6th, 2012 at 06:54 AM.

  4. #4
    Join Date
    Jun 2012
    Posts
    127

    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?

  5. #5
    Join Date
    Apr 2008
    Posts
    6

    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).

  6. #6
    Join Date
    Jun 2012
    Posts
    127

    Re: Array of class with inheritance

    Quote Originally Posted by chrisGz View Post
    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 !!!

  7. #7
    Join Date
    Apr 2008
    Posts
    6

    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.

  8. #8
    Join Date
    Jun 2012
    Posts
    127

    Re: Array of class with inheritance

    Quote Originally Posted by chrisGz View Post
    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

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: Array of class with inheritance

    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.

    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.

    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:
    1. new Shape
    2. implicit Point(int, int) calling Shape (see new Line)
    3. implicit Point(int, int) calling Shape (see new Line)
    4. explicit Line calling Shape
    5. explicit Point(int, int) calling Shape
    Last edited by Igor Vartanov; June 6th, 2012 at 09:23 AM.
    Best regards,
    Igor

  10. #10
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: Array of class with inheritance

    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.
    Best regards,
    Igor

  11. #11
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Array of class with inheritance

    Quote Originally Posted by chrisGz View Post
    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.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Array of class with inheritance

    Quote Originally Posted by chrisGz View Post
    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

  13. #13
    Join Date
    Jun 2012
    Posts
    127

    Re: Array of class with inheritance

    Quote Originally Posted by Igor Vartanov View Post
    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.


    Then if you don't want those to be destroyed implicitly, you have to change the code.
    yes, Igor)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured