CS计算机代考程序代写 INTRO TO COMPUTER SCIENCE II

INTRO TO COMPUTER SCIENCE II
IMPLEMENTING INHERITANCE
CS162

Inheritance
“is-a” relationship
Create complex objects by directly inheriting the attributes and
behaviors of other objects and then specializing them
Class being inherited from is known as parent, base, or superclass Class inheriting is known as child, derived, or subclass
Helps avoid re-inventing the wheel (duplicate code)

Inheritance
Base class and Derived class
class Base { class Derived : public Base {
}; };
Derived inherits functions and variables from Base
 Members are not copied from Base into Derived
 Derived is a essentially a two-part class: one part Base, one part Derived
Base
• Base member variables • Base member functions
Derived
• Base member variables Base
• Base member functions
• Derived member variables • Derived member functions
Part

Implementing Inheritance
Member access specifiers
 Public – accessed by anybody
 Private – accessed by only Base functions or friends
 This means derived classes can’t access base class private members directly
But the derived class still has a copy of the base private member
 Protected – allows Derived classes to access members, but not accessible outside the class
class Base { public:
int pub; private:
int pri; protected:
}; int pro;
class Derived : public Base { public:
Derived(){
pub = 1; //allowed
pri = 2; //not allowed pro = 3; //allowed
} };
int main(){ Base b;
b.pub = 1; //allowed b.pri = 2; //not allowed b.pro = 3; //not allowed
}

Base Access Specifiers
Combine with base class member access specifiers to determine derived class member access specifiers
3 types
Public Inheritance
class Derived : public Base { };
class Derived : protected Base {
class Derived : private Base { };
 Protected Inheritance };
 Private Inheritance

Public Inheritance
Public Inheritance Base -> Derived
 public -> public
 protected -> protected  private -> no access
Used 99% of the time
class Base { public:
int pub; private:
int pri; protected:
int pro; };
class Derived : public Base { public:
Derived(){
pub = 1; //allowed
pri = 2; //not allowed pro = 3; //allowed
} };
int main(){ Base b;
b.pub = 1; //allowed b.pri = 2; //not allowed b.pro = 3; //not allowed Derived d;
d.pub = 1; //allowed d.pri = 2; //not allowed
} d.pro = 3; //not allowed

Private Inheritance
Private Inheritance Base -> Derived
 public -> private
 protected -> private  private -> no access
class Base { public:
int pub; private:
int pri; protected:
int pro;
class Derived : private Base {
public:
Derived(){
pub = 1; //allowed
pri = 2; //not allowed pro = 3; //allowed
}
int main(){ Base b;
b.pub = 1; //allowed b.pri = 2; //not allowed b.pro = 3; //not allowed Derived d;
d.pub = 1; //not allowed d.pri = 2; //not allowed d.pro = 3; //not allowed
}
Only affects code trying to access base variables through the derived class
 Doesn’t change how derived class accesses base members
Rarely used
};
};

Protected Inheritance
Protected Inheritance Base -> Derived
 public -> protected
 protected -> protected  private -> no access
Almost never used
class Base { public:
int pub; private:
int pri; protected:
int pro; };
class Derived : protected Base {
public:
Derived(){
pub = 1; //allowed
pri = 2; //not allowed pro = 3; //allowed
} };
int main(){ Base b;
b.pub = 1; //allowed b.pri = 2; //not allowed b.pro = 3; //not allowed Derived d;
d.pub = 1; //not allowed d.pri = 2; //not allowed d.pro = 3; //not allowed
}

Construction of Inherited Classes
Base constructor is initialized first, then Derived constructor for Derived class
class Base { };
class Derived : public Base { };
Inheritance construction flow
1. Memory for derived is allocated (enough for both base and derived)
2. Derived constructor is called
3. Base object is constructed first using
Base constructor
4. Initializes Base variables
5. Derived constructor body executes
6. Control is returned to caller
int main(){
Base b; //base constructor called
Derived d; //base constructor called, derived constructor called return 0;
}

Construction of Inherited Classes
C++ prevents classes from initializing member inherited member variables in the initialization list of the constructor
 Why?
 If const variable, can’t allow derived class constructor to change it
after base class initialization
So how do we initialize the member variables of base class when creating a derived object?
 Explicitly choose which Base class constructor is called Derived::Derived(int pub, int pri, int pro) : Base(pub, pri, pro){
};

Destruction of Inherited Classes
Opposite of construction class Base {
};
class Derived : public Base { };
int main(){
Derived d; //base constructor called, derived constructor called
return 0; //derived destructor called, base destructor called
}

Some code…
Let’s look at Animals and Monkeys