Jump to content

C++ Inheritance Question!

- - - - -

  • Please log in to reply
3 replies to this topic

#1
Yuriy M

Yuriy M

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
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. :)
For $1000: Something that is a miserable pile of secrets.

#2
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
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

#3
Yuriy M

Yuriy M

    Learning Programmer

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

#4
Mathematix

Mathematix

    Programmer

  • Members
  • PipPipPipPip
  • 112 posts

Yuriy M said:

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;

}





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users