Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

C++ Inheritance Question!

inheritance

  • Please log in to reply
3 replies to this topic

#1 Yuriy M

Yuriy M

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 126 posts

Posted 24 November 2010 - 05:30 PM

Hi guys. I've been working on a C++ program which uses inheritance and just want to know one thing:

My derived class is assigned to another object of the same derived class but I'm also wondering if by assigning the object, does it also copy the data associated with the base class to that assigned object?

When I ran my program, the results appeared the same in both derived objects but I'm not entirely sure if the data from the base class was also copied so I'm really in need of a second opinion. Thanks. :)
  • 0
For $1000: Something that is a miserable pile of secrets.

#2 mnirahd

mnirahd

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 328 posts

Posted 24 November 2010 - 05:34 PM

Hi,

When we do assignment on class object: then

1. If an assignment operator has been overridden that would be called. And whatever has been done within that function would impact the object
2. If not, then if they have same class type: the data members are copied: If the have same base class, but they are defined by different derived classes, then there would be some data type casting, that would cause members from base class to be copied.

I hope this helps!

Munir
  • 0

#3 Yuriy M

Yuriy M

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 126 posts

Posted 24 November 2010 - 08:14 PM

It does. Thanks a lot! :)
  • 0
For $1000: Something that is a miserable pile of secrets.

#4 Mathematix

Mathematix

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 103 posts

Posted 25 November 2010 - 08:22 AM

Hi guys. I've been working on a C++ program which uses inheritance and just want to know one thing:

My derived class is assigned to another object of the same derived class but I'm also wondering if by assigning the object, does it also copy the data associated with the base class to that assigned object?

When I ran my program, the results appeared the same in both derived objects but I'm not entirely sure if the data from the base class was also copied so I'm really in need of a second opinion. Thanks. :)

You need to revise copy constructors. When you declare a class, unless you specify otherwise, several defaults are created for the class:

1. The default constructor.
2. The copy constructor.
3. The assignment operator.
4. The destructor.

The behavior that you are seeing is most likely a shallow copy of the object that you are using to create a further object where all object attributes are being copied over.

Here is a very simple example invoking the activity of a copy constructor.

#include <iostream>
using namespace std;

class CSomeClass
{
public:

	CSomeClass(){}

	CSomeClass(const CSomeClass& ref) 
	{
		cout << "Copy Constructor called!" << endl;
	}

	~CSomeClass(){}

};

int main() 
{
	// First object created.
	CSomeClass firstObj;

	// Second object created invoking copy constructor because
	// second object created by copying the first.
	CSomeClass secondObj = firstObj;	

	return 0;
}

  • 0





Also tagged with one or more of these keywords: inheritance