Download presentation
Presentation is loading. Please wait.
Published byKathryn Spencer Modified over 9 years ago
1
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
2
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)
3
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
4
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; };
5
Inheritance // Constructor plane_t:: plane_t(ifstream &infile, const string & type) : sobj_t(infile, type) { point = myvector(); // initalize point to normal = myvector(); // initalize normal to load(infile, type); normal = orient1.cross(orient2); } This is a critically important element of inheritance. It specifies which constructor of the parent class should be used. Leaving this off will create a befuddling error condition in which the default sobj_t constructor is called.
6
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
7
Inheritance Base class access specifier Type of inheritance publicprotectedprivate publicpublic 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. protectedprotected 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. privateHidden 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.
8
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.
9
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.
10
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.
11
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; };
12
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.