Upload
duongtruc
View
224
Download
0
Embed Size (px)
Citation preview
Inheritance
Process of deriving a new class using another class as a base.
Base/Parent/Super Class −→ Derived/Child/Sub Class
Umut Oztok CS32 - Week 4
Inheritance
class Animal{
public:
Animal();
~Animal();
int getAge() const;
void speak() const;
private:
int m_age;
};
class Dog : public Animal{
public:
Dog();
~Dog();
string getName() const;
void setName(string name);
private:
string m_name;
};
Dog inherits Animal.
Umut Oztok CS32 - Week 4
Inheritance
When to use?
If there exists an ”is-a relationship”.
Dog is an Animal.Student is a Person.SpeedShip is a Ship.
What is inherited?
All member variables.
All member funtions except assignment operator, constructorsand destructor.
Umut Oztok CS32 - Week 4
Inheritance
What is accessable?
A derived class cannot access private member variables of itsbase class.
A derived class cannot access private member functions of itsbase class.
A derived class can access public member variables of its baseclass.
A derived class can access public member functions of itsbase class.
What if we want to access private functions of the base class?
Use keyword protected instead of private.
Making private member variables protected violates encapsulation!!
Umut Oztok CS32 - Week 4
Inheritance
How to implement Constructor?
Dog objects are Animal objects. So, when we create a Dogobject, we also create an Animal object.
In general, when a class object is constructed:1 Base class’s constructor is called.2 Data members of the object are created.3 Body of the constructor is executed.
Umut Oztok CS32 - Week 4
Inheritance
Is Dog’s constructor valid?
class Animal{
public:
Animal(){}
...
private:
int m_age;
};
class Dog : public Animal{
public:
Dog(int age, string name){
m_age = age;
m_name = name;
}
...
private:
int m_name;
};
NO, m age cannot be accessed by Dog.
Umut Oztok CS32 - Week 4
Inheritance
This one works for now.
class Animal{
public:
Animal(){}
void setAge(int age);
...
private:
int m_age;
};
class Dog : public Animal{
public:
Dog(int age, string name){
setAge(age);
m_name = name;
}
...
private:
int m_name;
};
Umut Oztok CS32 - Week 4
Inheritance
What if Animal’s constructor takes one argument?
class Animal{
public:
Animal(int age){
m_age = age;
}
void setAge(int age):
...
private:
int m_age;
};
class Dog : public Animal{
public:
Dog(int age, string name){
setAge(age);
m_name = name;
}
...
private:
int m_name;
};
Does not work anymore!
Umut Oztok CS32 - Week 4
Inheritance
Solution is to use initializer list.
class Animal{
public:
Animal(int age){
m_age = age;
}
...
private:
int m_age;
};
class Dog : public Animal{
public:
Dog(int age, string name)
: Animal(age){
m_name = name;
}
...
private:
int m_name;
};
Umut Oztok CS32 - Week 4
Inheritance
Destructor : reverse order of constructor
1 Body of the destructor is executed.
2 Member variables are removed.
3 Base class’s destructor is called.
We will revisit destructors!!
Umut Oztok CS32 - Week 4
Overriding
Assume speak() is implemented as follows:
void Animal::speak() const{
cout << "..." << endl;
}
Dog inherits this function.But we expect a dog to make a specific sound.What to do?
Umut Oztok CS32 - Week 4
Overriding
We can override member functions.
class Dog : public Animal{
public:
...
void speak() const;
private:
string m_name;
};
void Dog::speak() const{
cout << "Woof!" << endl;
}
Example:
Animal a1;
a1.speak();
Output: ...
Dog d1;
d1.speak();
Output: Woof!
Umut Oztok CS32 - Week 4
Overriding
We can still call the base class’s speak() function on a Dog object.
Dog d1;
d1.Animal::speak();
Umut Oztok CS32 - Week 4
Polymorphism
Is the following allowed?: Animal* ani = new Dog;
Yes, as ”Dog is an Animal”.
Does the following output Woof!?: ani->speak();
No, because ani is a pointer to an Animal instance.But, I really want ani to say ”Woof!”.
Solution: insert the virtual keyword in front of both theoriginal and the replacement functions!
Polymorphism: using a base pointer/reference to access anyvariable that is of a type derived from the base class.
Umut Oztok CS32 - Week 4
Polymorphism
class Animal{
public:
Animal();
virtual ~Animal();
int getAge() const;
virtual void speak() const;
private:
int m_age;
};
class Dog : public Animal{
public:
Dog();
virtual ~Dog();
string getName() const;
void setName(string name);
virtual void speak() const;
private:
string m_name;
};
Compiler figures out which object ani points to and calls thecorresponding function.
Umut Oztok CS32 - Week 4
Polymorphism
Virtual functions allow late binding or dynamic binding:Appropriate version of a method is decided at execution time(runtime), instead of at compilation time.
Called as polymorphism as ani is allowed to have multipleforms.
Umut Oztok CS32 - Week 4
Polymorphism
Animal* ani;
int x;
cin >> x;
switch (x){
case 1:
ani = new Dog;
break;
case 2:
ani = new Cat;
break;
default:
ani = new Animal;
break;
}
ani->speak();
Umut Oztok CS32 - Week 4
Polymorphism
class Animal{
public:
void tickle () {
speak();
}
virtual void speak() {
cout << "...";
}
};
class Dog: public Animal {
public:
virtual void speak() {
cout << "Woof!";
}
};
main() {
Animal* ani= new Dog;
ani->tickle();
delete ani;
}
What is the output?
Umut Oztok CS32 - Week 4
Virtual Destructor
Always make destructors virtual if you use inheritance.
Otherwise, when you use polymorphism, only base class’sdestructor will be called.
Umut Oztok CS32 - Week 4
Pure Virtual Functions
virtual void speak() { cout << "..."; }
speak function of Animal class doesn’t make sense.
Make it pure virtual by putting ”=0” in declaration of baseclass.
virtual void speak() = 0;
You don’t implement pure virtual functions in base class.
However, derived classes should implement pure virtualfunctions to be able to be instantiated.
Umut Oztok CS32 - Week 4
Abstract Base Classes
If a base class has at least one pure virtual function, it iscalled abstract base class (ABC).
An ABC cannot be instantiated. So, Animal ani; is notallowed.
Umut Oztok CS32 - Week 4
Abstract Base Classes
Why do we use pure virtual functions?
Avoid implementing ”dummy” functions when it doesn’t makesense.
Force the programmer to implement functions in a derivedclass to prevent bugs.
Umut Oztok CS32 - Week 4
Slides
Slides will be available at http://www.cs.ucla.edu/~umut/cs32
Umut Oztok CS32 - Week 4