Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation...

12
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation - a language mechanism for restricting access to some of the object's components. • inheritance • polymorphism

Transcript of Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation...

Page 1: Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation - a language mechanism for restricting access to some.

Inheritance

Modern object-oriented (OO) programming languages provide 3 capabilities:• encapsulation - a language mechanism for

restricting access to some of the object's components.

• inheritance• polymorphism

Page 2: Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation - a language mechanism for restricting access to some.

Inheritance

• Allows a new class to be based on an existing class.

• The new class inherits all the member variables and functions (except constructors and destructors)

Page 3: Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation - a language mechanism for restricting access to some.

Inheritance

C++ class hierarchy is based upon the principle of increased specialization.• The base class carries attributes that are common

to all classes and virtual functions that may or may not be overridden.

• Attributes that are esoteric to a particular entity plane/normal or sphere/radius are not defined

• in the base class• The derived class inherits the attributes of classes

above it in the class hierarchy • The specialization can continue over multiple

levels

Page 4: Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation - a language mechanism for restricting access to some.

Inheritance

The plant_t class is derived from the sobj_t class.class plane_t : public sobj_t { public: plane_t(ifstream &infile, const string & type); void load(ifstream &infile, const string & token); myvector getpoint(); myvector getnormal(); int hits(myvector &base, myvector &dir, hitinfo_t &hit); void dump();

private: myvector point; myvector normal; myvector orient1; myvector orient2;};

Page 5: Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation - a language mechanism for restricting access to some.

Inheritance

// Constructorplane_t:: plane_t(ifstream &infile, const string & type) : sobj_t(infile, type) { point = myvector(); // initalize point to <0,0,0> normal = myvector(); // initalize normal to <0,0,0>

load(infile, type); normal = orient1.cross(orient2);}

This is a critically important element ofinheritance. It specifies whichconstructor of the parent class should beused. Leaving this off will create abefuddling error condition in which thedefault sobj_t constructor is called.

Page 6: Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation - a language mechanism for restricting access to some.

Inheritance

Protection attributes: Base class member access specifier• public - Any holder of a pointer to an instance of

the class may invoke any public method or modify any public data item.

• private – methods/data members may be accessed only by methods belonging to the class

• protected - methods/data members may be access by derived classes but not others

Page 7: Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation - a language mechanism for restricting access to some.

InheritanceBase class access specifier

Type of inheritance

public protected private

public public in derived class.

Can be accessed directly by member functions, friend functions, and nonmember functions.

protected in derived class.

Can be accessed directly by member functions and friend functions.

private in derived class.Can be accessed directly by member functions and friend functions.

protected protected in derived class.

Can be accessed directly by member functions and friend functions.

protected in derived class.

Can be accessed directly by member functions and friend functions.

private in derived class.

Can be accessed directly by member functions and friend functions.

private Hidden in derived class.

Can be accessed by member functions and friend functions through public or protected member functions of the base class.

Hidden in derived class.

Can be accessed by member functions and friend functions through public or protected member functions of the base class.

Hidden in derived class.

Can be accessed by member functions and friend functions through public or protected member functions of the base class.

Page 8: Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation - a language mechanism for restricting access to some.

Late Binding / Dynamic Binding

• technique of waiting until runtime to determine the implementation of a function.

• The decision of which version of a member function is appropriate is decided at runtime.

• Polymorphism is another word for late binding.

Page 9: Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation - a language mechanism for restricting access to some.

Polymorphism

• Enables us to write programs that process objects of classes that are part of the same class hierarchy as if they were all objects of the hierarchy’s base class.

Page 10: Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation - a language mechanism for restricting access to some.

Virtual Functions (C++ only)

• By default, C++ matches a function call with the correct function definition at compile time. This is called static binding.

• You can specify that the compiler match a function call with the correct function definition at run time; this is called dynamic binding.

• You declare a function with the keyword virtual if you want the compiler to use dynamic binding for that specific function.

Page 11: Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation - a language mechanism for restricting access to some.

Virtual Functions (C++ only)

class entity_t { public: // Constructor entity_t(ifstream &infile, const string & entTypeVal); virtual ~entity_t(); // Note virtual destructor // Methods virtual void load(ifstream &infile, const string & token); virtual void dump(); virtual int hits(myvector &base, myvector &dir, hitinfo_t &hit); string gettype(); string getname(); private: string type; string name; };

Page 12: Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation - a language mechanism for restricting access to some.

Virtual Functions (C++ only)

• The virtual keyword indicates to the compiler that it should choose the appropriate definition of f() not by the type of reference, but by the type of object that the reference refers to.

• Therefore, a virtual function is a member function you may redefine for other derived classes, and can ensure that the compiler will call the redefined virtual function for an object of the corresponding derived class, even if you call that function with a pointer or reference to a base class of the object.

• A class that declares or inherits a virtual function is called a polymorphic class.

• You redefine a virtual member function, like any member function, in any derived class.