Concepts Related with Inheritance Generalization Subtyping (extension) Specialization (restriction)
Generalization In OO models, some classes may have common characteristics We extract these features into a new class and inherit original classes from this new class This concept is known as Generalization
Example – Generalization Circle color vertices radius move setColor computeArea Line color vertices length move setColor getLength Triangle color vertices angle move setColor computeArea
Example – Generalization Shape color vertices move setColor Circle radius computeArea Line length getLength Triangle angle computeArea
Example – Generalization Teacher name age gender designation salary teach takeExam eat walk Student name age gender program studyYear study heldExam eat walk Doctor name age gender designation salary checkUp prescribe eat walk
Example – Generalization Person name age gender eat walk Teacher designation salary teach takeExam Student program studyYear study heldExam Doctor designation salary checkUp prescribe
Sub-typing & Specialization We want to add a new class to an existing model Find an existing class that already implements some of the desired state and behaviour Inherit the new class from this class and add unique behaviour to the new class
Sub-typing (Extension) Sub-typing means that derived class is behaviourally compatible with the base class Behaviourally compatible means that base class can be replaced by the derived class
Example – Sub-typing (Extension) Person name age gender eats walks Student program studyYear study takeExam
Example – Sub-typing (Extension) Shape color vertices setColor move Circle radius computeCF computeArea
Specialization (Restriction) Specialization means that derived class is behaviourally incompatible with the base class Behaviourally incompatible means that base class can’t always be replaced by the derived class
Example – Specialization (Restriction) Person age : [0..100] … Adult age : [18..100] … setAge( a ) … setAge( a ) … age = a If age < 18 then error else age = a
Example – Specialization (Restriction) IntegerSet … NaturalSet … add( elem ) … add( elem ) … add element to the set If elem < 1 then error else add element to the set
Overriding A class may need to override the default behaviour provided by its base class Overriding in case of inheritance. Reasons for overriding Provide behaviour specific to a derived class Extend the default behaviour Restrict the default behaviour Improve performance
Example – Specific Behaviour Shape color vertices draw move setColor Circle radius draw computeArea Line length draw Triangle angle draw computeArea
Example – Extension Window width height open close draw DialogBox controls enable draw 1- Invoke Window’s draw 2- draw the dialog box
Example – Restriction IntegerSet … NaturalSet … add( elem ) … add( elem ) … Add element to the set If elem < 1 then give error else Add element to the set
Example – Improve Performance Class Circle overrides rotate operation of class Shape with a Null operation. Shape color coord draw rotate setColor Circle radius draw rotate
Abstract Classes An abstract class implements an abstract concept Main purpose is to be inherited by other classes. Need hierarchy Can’t be instantiated Promotes reuse
Example – Abstract Classes Teacher Doctor Student Here, Person is an abstract class Person name age gender eat walk
Example – Abstract Classes Bus Truck Car Here, Vehicle is an abstract class Vehicle color model accelerate applyBrakes
Concrete Classes A concrete class implements a concrete concept Main purpose is to be instantiated Provides implementation details specific to the domain context
Example – Concrete Classes Here, Student, Teacher and Doctor are concrete classes Teacher Doctor Student program studyYear study heldExam Person
Example – Concrete Classes Here, Car, Bus and Truck are concrete classes Bus Car Vehicle Truck capacity load unload
Class Class is a tool to realize objects Class is a tool for defining a new type
Example Lion is an object Student is an object Both has some attributes and some behaviors
Uses The problem becomes easy to understand Interactions can be easily modeled
Type in C++ Mechanism for user defined types are Structures Classes Built-in types are like int , float and double User defined type can be Student in student management system Circle in a drawing software
Abstraction Only include details in the system that are required for making a functional system Student Name Address Sibling Father Business Relevant to our problem Not relevant to our problem
Defining a New User Defined Type class ClassName { … DataType MemberVariable ; ReturnType MemberFunction (); … }; Syntax Syntax
Example class Student { int rollNo ; string name; float CGPA; … void setName (string newName ); void setRollNo ( int newRollNo ); … }; Member variables Member Functions
Why Member Function They model the behaviors of an object Objects can make their data invisible Object remains in consistent state
Object and Class Object is an instantiation of a user defined type or a class
Declaring class variables Variables of classes (objects) are declared just like variables of structures and built-in data types TypeName VaraibaleName ; int var ; Student aStudent ;
Accessing members Members of an object can be accessed using dot operator (.) to access via the variable name arrow operator (->) to access via a pointer to an object Member variables and member functions are accessed in a similar fashion
Example class Student{ int rollNo ; void setRollNo ( int aNo ); }; Student aStudent ; aStudent.rollNo ; Error
Access specifiers
Access specifiers
Access specifiers There are three access specifiers ‘public’ is used to tell that member can be accessed whenever you have access to the object ‘private’ is used to tell that member can only be accessed from a member function ‘protected’ to be discussed when we cover inheritance
Example class Student{ private: string name; int rollNo ; public: void setName (string); void setRollNo ( int ); ... }; Cannot be accessed outside class Can be accessed outside class
Example class Student{ ... int rollNo ; public: void setRollNo ( int aNo ); }; int main(){ Student aStudent ; aStudent.SetRollNo (1); }
Default access specifiers When no access specifier is mentioned then by default the member is considered private member
Example class Student { string name; int RollNo ; }; class Student { private: string name; int RollNo ; };
Example class Student { string name; int RollNo ; void SetName (string); }; Student aStudent ; aStudent.SetName (“Ali”); Error
Example class Student { string name; int RollNo ; public: void setName (string); }; Student aStudent ; aStudent.SetName (“Ali”);
Example 1 // Creating and using boxes #include < iostream > using namespace std ; class Box // Class definition at global scope { public: double length; // Length of a box in inches double breadth; // Breadth of a box in inches double height; // Height of a box in inches };
Example 1 Cont.. int main() { Box Box1; // Declare Box1 of type Box Box Box2; // Declare Box2 of type Box double volume = 0.0; // Store the volume of a box here Box1.height = 5.0; // Define the values Box1.length = 4.0; // of the members of Box1.breadth = 5.0; // the object Box1 // Calculate volume of Box1 volume = Box1.height * Box1.length * Box1.breadth; cout << endl ; cout << "Volume of Box1 = " << volume; cout << endl ; cout << endl ; // Display the size of a box in memory cout << "A Box object occupies " ; cout << sizeof (Box1) << " bytes."; cout << endl ; return 0; }