










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An explanation of object-oriented programming (oop) concepts, specifically focusing on shallow copy and deep copy in c++ using a student class example. The implementation of a shallow copy and deep copy constructor, assignment operator, and output comparison.
Typology: Slides
1 / 18
This page cannot be seen from the preview
Don't miss anything!











class Student: public Person{ class Student: public Person{
char* major; char* major;
public: public:
Student(char *, char *); Student(char *, char *); void Print() const; void Print() const; ~Student(); ~Student();
}; };
► ► The output is as follows:The output is as follows:
Name: Ali Name: Ali Major: Computer Science Major: Computer Science
......
majormajor
namename
sobj2sobj
......
majormajor
namename
sobj1sobj
......
majormajor
namename
sobj2sobj
......
majormajor
namename
sobj1sobj
int main(){ int main(){ Student sobj1( Student sobj1(““AliAli””,, “ “Computer ScienceComputer Science””);); Student sobj2 = sobj1; Student sobj2 = sobj1; sobj2.Print(); sobj2.Print(); return 0; return 0; } }
► ► The output of previous code will be asThe output of previous code will be as follows: follows:
Person Constructor Person Constructor Name: Name: Major: Computer Science Major: Computer Science
Person::Person(const Person& Person::Person(const Person& prhs) { prhs) { // Code for deep copy // Code for deep copy } }
Student::Student(const Student Student::Student(const Student &srhs) &srhs) :Person(srhs):Person(srhs) {{ // Code for deep copy // Code for deep copy } }
int main() int main()
{ {
Student sobj1, sboj2( Student sobj1, sboj2(““AliAli””,, (^) ““CSCS””);); sobj1 = sobj2; sobj1 = sobj2; return 0; return 0;
} }
► ► Programmer has to call operator of baseProgrammer has to call operator of base class, if he is writing class, if he is writing assignment operatorassignment operator of derived class of derived class
► ► The assignment operator of base class isThe assignment operator of base class is not called not called
► ► OutputOutput
Student Assignment Student Assignment
► ► Base class functions can be explicitly called with referenceBase class functions can be explicitly called with reference to base class itself to base class itself
//const char* Person::GetName() {...}; //const char* Person::GetName() {...}; void Student::Print() void Student::Print() { { cout << GetName(); cout << GetName(); cout << Person::GetName(); cout << Person::GetName(); } }