C++ Classes – Inheritance (Pt.2)
C. Papachristos
Robotic Workers Lab
University of Nevada, Reno
CS-202
Course , Projects , Labs:
Your 5th Project will be announced today Thursday 2/27.
4th Project Deadline was this Wednesday 2/26.
Course Week
CS-202 C. Papachristos
Monday | Tuesday | Wednesday | Thursday | Friday | | Sunday |
|
|
| Lab (8 Sections) |
| | |
| CLASS | PASS Session | CLASS |
| | |
PASS Session | | Project DEADLINE | NEW Project | PASS Session | | PASS Session |
Today’s Topics
CS-202 C. Papachristos
Method Overriding
Inheritance Rules
Polymorphism Prelude
Advanced Examples
Faculty |
Research Area |
Advisees |
Inheritance
CS-202 C. Papachristos
Inheritance Relationship
Inheritance Syntax:
class BaseClass {
public:
//operations
private:
//data
};
class DerivedClass : public BaseClass {
public:
//operations
private:
//data
};
University_Member |
Name |
Address |
Student |
Major |
GPA |
Base Class
Derived
Class(es)
Indicates that this DerivedClass Inherits data and operations from this BaseClass
Inheritance
CS-202 C. Papachristos
Handling Access from within a Derived Class
Derived/Child Class has access to Base/Parent Class’s:
No access to Base/Parent Class’s private Member Variables/Functions:
Remember:
private Member Variables are only directly accessible (“by name”) in Member Functions of their own Class (they one they are defined in).
Inheritance
CS-202 C. Papachristos
Inherited Member(s) of a Derived Class
Vehicle
Parent Class
Car
Child Class
Child Class’ own:
public Fxns & Vars
protected Fxns & Vars
private Vars
private Fxns
Copy-ctor
Assignment Operator
ctor
dtor
public Fxns & Vars
protected Fxns & Vars
private Vars
Not directly accessible!
Can be accessed and invoked
But are not directly Inherited!
Method Overriding
CS-202 C. Papachristos
Overriding
Remember: Interface of a Derived/Child Class:
Implementation of a Derived/Child Class will:
class Vehicle {
public:
int getMileage(){ return m_mileage; }
private:
int m_mileage;
} ;
class Car : public Vehicle {
public:
int getMileage();
} ;
Now that you re-Declared it, you have to Define it!
Method Overriding
CS-202 C. Papachristos
Overriding vs Overloading
Overriding in a Derived/Child class means “Redefining what it does” :
Overloading a Function means “Reusing its name” :
Method Overriding
CS-202 C. Papachristos
Overriding vs Overloading
Overriding in a Derived/Child class means “Redefining what it does” :
Overloading a Function means “Reusing its name” :
Function “Signature” (or the information that participates in Overload Resolution):
(including order, number, types).
Method Overriding
CS-202 C. Papachristos
Overriding vs Overloading
Method Overriding (uses exact same signature):
class Animal {
public:
void eat(){
cout << "I eat stuff" << endl;
}
};
class Lion : public Animal {
void eat(){
cout << "I eat meat" << endl;
}
};
int main(){
Animal animal;
animal.eat(); // I eat stuff
Lion lion;
lion.eat(); // I eat meat
}
Method Overriding
CS-202 C. Papachristos
Overriding vs Overloading
Method Overloading (uses exact different signature):
class Animal {
public:
void eat(){
cout << "I eat stuff" << endl;
}
};
class Lion : public Animal {
public:
void eat(const char * food)
cout << "I ate a " << food << endl;
}
};
int main(){
Lion lion;
lion.eat(); // I eat stuff
lion.eat("Steak"); // I ate a Steak
}
Inheritance Rules
CS-202 C. Papachristos
Inheritance Rules - Further
All “normal” functions in Base/Parent class are Inherited in Derived/Child Class.
Inheritance exceptions to the rules so far are:
Inheritance Rules
CS-202 C. Papachristos
Constructor & Destructor in Derived Class(es)
The Base/Parent Class ctors are not Inherited in Derived(Child) Classes.
Nothing more is required:
The “First thing” that any Derived(Child) Class ctor does is to try and invoke the Base(Parent) Class Default ctor !
Inheritance Rules
CS-202 C. Papachristos
Constructor & Destructor in Derived Class(es)
Constructor(s)
BaseClass |
BaseClass(); |
… |
DerivedClass |
DerivedClass(); |
… |
DerivedClass dClass;
BaseClass();
DerivedClass();
Inheritance Rules
CS-202 C. Papachristos
Constructor & Destructor in Derived Class(es)
Constructor(s)
Destructor
BaseClass |
BaseClass(); |
… |
DerivedClass |
DerivedClass(); |
… |
DerivedClass dClass;
BaseClass();
DerivedClass();
BaseClass |
~BaseClass(); |
… |
DerivedClass |
~DerivedClass(); |
… |
~DerivedClass();
~BaseClass();
{
DerivedClass dClass;
} //end of scope
Inheritance Rules
CS-202 C. Papachristos
Inheritance Rules - Further
All “normal” functions in Base/Parent class are Inherited in Derived/Child Class.
Inheritance exceptions to the rules so far are:
If none is specified for Derived Class, compiler will still generate an “automatically� synthesized” one (which will also invoke the Base Class Copy-ctor.
Inheritance Rules
CS-202 C. Papachristos
Constructor & Destructor in Derived Class(es)
Copy-ctor
BaseClass |
BaseClass(); |
… |
DerivedClass |
DerivedClass(const DerivedClass & other); |
… |
DerivedClass dClass;
BaseClass();
DerivedClass();
DerivedClass dClass_Cpy( dClass );
BaseClass();
DerivedClass(const DerivedClass & other);
Inheritance Rules
CS-202 C. Papachristos
Inheritance Rules - Further
All “normal” functions in Base/Parent class are Inherited in Derived/Child Class.
Inheritance exceptions to the rules so far are:
If none is specified for Derived Class, compiler will still generate an “automatically� synthesized” one.
If none is specified for Derived Class, compiler will still generate an “automatically� synthesized” one (which will also invoke the Base Class assignment operator=) .
If one is implemented for the Derived Class that one will be called alone .
Inheritance Rules
CS-202 C. Papachristos
Assignment operator= in Derived Class(es)
Assignment Operator ( = )
We can also directly call the Base Class Assignment from the Derived Class one !
class Animal{
Animal & operator=(const Animal & rhs_animal ){
cout << "Animal assignment" << endl;
}
};
class Lion : public Animal{
Lion & operator=(const Lion & rhs_lion ){
Animal::operator=( rhs_lion );
cout << "Lion assignment" << endl;
}
};
Inheritance Rules
CS-202 C. Papachristos
Constructor & Destructor in Derived Class(es)
Sequence of Base-Derived ctor & Derived-Base dtor calls
Example:
class Animal {
public:
Animal(){cout << "Base constructor"<<endl;}
~Animal(){cout << "Base destructor"<<endl;}
};
class Lion : public Animal {
public:
Lion(){cout << "Derived constructor"<<endl;}
~Lion(){cout << "Derived destructor"<<endl;}
};
Output:
Base constructor
Derived constructor
Derived destructor
Base destructor
int main(){
Lion lion;
return 0;
}
Inheritance Rules
CS-202 C. Papachristos
Parametrized Constructor in Derived Class(es)
Calling the Base ctor from a Derived ctor explicitly
Example:
class Animal {
public:
Animal(const char * name) {
strcpy(m_name, name); cout<<m_name<<endl;
}
protected:
char m_name[MAXNAME];
};
class Lion : public Animal {
public:
Lion(const char * name) : Animal(name){
/* Rest of Lion Parametrized ctor statements*/
}
};
int main(){
Lion lion("King");
return 0;
}
Output:
King
Note: Initializer-list is the only way that allows calling a Base ctor from a Derived ctor.
Inheritance Rules
CS-202 C. Papachristos
Parametrized Constructor in Derived Class(es)
Calling the Base ctor from a Derived ctor explicitly
Example:
class Animal {
public:
Animal(const char * name) {
strcpy(m_name, name); cout<<m_name<<endl;
}
protected:
char m_name[MAXNAME];
};
class Lion : public Animal {
public:
Lion(const char * name) : Animal(name){
strcat(m_name," Lion"); cout<<m_name<<endl;
}
};
int main(){
Lion lion("King");
return 0;
}
Output:
King
King Lion
Note: Calls Parametrized Base ctor by passing down argument from the Derived ctor.
Polymorphism (prelude)
CS-202 C. Papachristos
Inheritance & Polymorphism
Polymorphism means “the ability to take many forms”.
Pointers of Base Class-type:
BigRig |
… |
Vehicle |
… |
Car |
… |
Plane |
… |
etc… |
… |
SUV |
… |
Sedan |
… |
Van |
… |
Jeep |
… |
Polymorphism (prelude)
CS-202 C. Papachristos
Inheritance & Polymorphism
Base Class-type Pointers :
SUV suv1;
Sedan sedan1, sedan2;
Jeep jeep1;
Car * suv1_Pt = &suv1;
Car * sedan1_Pt = &sedan1, * sedan2_Pt = &sedan2;
Car * jeep1_Pt = &jeep1;
This is valid: A Derived Class (SUV, Sedan, Van, Jeep) “is a type of” Base Class (Car).
A Derived Class Pointer cannot point to a Base Class Object.
Advanced Examples
CS-202 C. Papachristos
Derived to Base Class forwarding
Parent Class “hides” data even from Children (but some Interface exists for its Type!) :
class Animal {
friend ostream & operator<<(ostream & os, const Animal & animal){
os << "I have " << animal.m_legs << " legs" << endl;
return os;
}
private:
int m_legs;
};
class Lion : public Animal {
friend ostream & operator<<(ostream & os, const Lion & lion){
os << "I am a " << lion.m_color << " lion and\n"
<< "I have " << lion.m_legs << " legs" << endl;
return os;
}
private:
char m_color[256];
};
Advanced Examples
CS-202 C. Papachristos
Derived to Base Class forwarding
Parent Class “hides” data even from Children (but some Interface exists for its Type!) :
class Animal {
friend ostream & operator<<(ostream & os, const Animal & animal){
os << "I have " << animal.m_legs << " legs" << endl;
return os;
}
private:
int m_legs;
};
class Lion : public Animal {
friend ostream & operator<<(ostream & os, const Lion & lion){
os << "I am a " << lion.m_color << " lion and\n"
<< "I have " << lion.m_legs << " legs" << endl;
return os;
}
private:
char m_color[256];
};
Not possible !
The function is a friend of Lion,�not a friend of Animal !
Advanced Examples
CS-202 C. Papachristos
Derived to Base Class forwarding
Parent Class “hides” data even from Children (but some Interface exists for its Type!) :
class Animal {
friend ostream & operator<<(ostream & os, const Animal & animal){
os << "I have " << animal.m_legs << " legs" << endl;
return os;
}
private:
int m_legs;
};
class Lion : public Animal {
friend ostream & operator<<(ostream & os, const Lion & lion){
os << "I am a " << lion.m_color << " lion and\n"
<< static_cast< const Animal & >( lion );
return os;
}
private:
char m_color[256];
};
Casting to a Reference of Base class�type invokes the corresponding�function overload for Animal !
Time for Questions !
CS-202
CS-202 C. Papachristos