CS 162 Lecture Notes - Lecture 12: Object Copying, Feeling
CS 162 – Lecture 12 – Pitfalls and Intro Inheritance
Pitfalls
o Student s();
▪ Gut feeling, you thinking it is calling the default constructor
▪ Actually a prototype
▪ It will call the default constructor if you get rid of the parentheses
▪ Student s = student ();
o Student x = s;
▪ Gut feeling, assignment operator overload
▪ Actually, the copy constructor will get called
• Because the left side is not already initialized
• Assignment operator assumes the left side is already initialized
o If we only do shallow copy in Copy Constructor and Assignment Operator
Overload
▪ Could run into issue of both being on the heap, and pointing to garbage
memory that is does not have access to
▪ S1 = s2
o One of these will be on the exam
Inheritance
o The process by which a new class is created from another class
o Base (parent) class == more general class which derived class are created from
▪ Hold things that are common to all children
• Everything in that category
o Derived (child) class == new class
▪ Has all of the member variables and functions as base class
▪ Add things that are particular to that child
o Not limited to two levels, can be multiple
▪ Have ancestors and descendants
▪ Like a family tree
find more resources at oneclass.com
find more resources at oneclass.com
Document Summary
Cs 162 lecture 12 pitfalls and intro inheritance. Pitfalls: student s(), gut feeling, you thinking it is calling the default constructor, actually a prototype. If we only do shallow copy in copy constructor and assignment operator. Overload: could run into issue of both being on the heap, and pointing to garbage memory that is does not have access to, s1 = s2, one of these will be on the exam. Not inherited: base class constructor though it can be invoked from the derived class, child::child(): parent() {, to call the base constructor, gets called first. Initializes all that gets inherited: can be overwrited, base is called first to initialize all of the base member variables, can be called in child class, but not inherited. If (cid:271)ase (cid:272)onstru(cid:272)tor isn"t spe(cid:272)ified the (cid:271)ase default (cid:272)onstru(cid:272)tor will (cid:271)e used: copy constructor, cannot relay on base constructor to manage all the dynamic memory, assignment operator overload, destructor.