SlideShare uma empresa Scribd logo
1 de 195
Inheritance
[object Object],[object Object],[object Object],[object Object],Reuse
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Vehicle wheels engine Car wheels engine trunk Truck wheels engine trailer trunk trailer base class sub-classes or derived classes
[object Object],[object Object],[object Object],[object Object],[object Object]
 
 
 
 
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object]
 
[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Base class Derived  class Derived class Shape class Triangle  class Circle class Right-angle Triangle Equilateral Triangle Isosceles Triangle
[object Object],[object Object],Shape class Triangle  class Circle class Right-angle Triangle Equilateral Triangle Isosceles Triangle Shape class Triangle  class Circle class Right-angle Triangle Equilateral Triangle Isosceles Triangle
student print() year_group() CR print() inherits (isa) student_id, year, name dept, thesis
 
[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],The derived class inherits from the base class:  all public members  (see later) The additional members defined can have the same name (and type) as those   of the base class (as when some base members are to be redefined)
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object]
class  Shape {  protected :  int  width, height;  public :  void  setDims ( int  a,  int  b){  width=a; height=b;}  };  class  Rectangle:  public  Shape {  public :  int  area ( ) {  return  (width * height);  } };   class  Triangle:  public  Shape {  public :  int  area ( ) {  return  (width * height/2);  } }; class  Square:  public  Rectangle {  public : void  setDims ( int  a){  width=a; height=a;} };
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object]
 
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Animal Display() zoo[0]->Display(); zoo[1]->Display(); zoo[2]->Display(); Dog Display() Cat Display()
 
Inheritance Hierarchy for Person-derived Classes
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object]
[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object]
[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object]
[object Object],[object Object],[object Object]
[object Object]
[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object]
[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object]
[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
public inheritance protected inheritance private inheritance public  member in base class public  in derived class protected  in derived class private  in derived class protected  member in base class protected  in derived class protected  in derived class private  in derived class private  member in base class hidden in derived class hidden in derived class hidden in derived class
[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],15-
[object Object],[object Object],[object Object],[object Object],15- Animal Cat Dog Poodle
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],15-
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],15-
[object Object],[object Object],15-
[object Object],[object Object],[object Object],[object Object],15-
[object Object],15-
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],15-
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],15-
[object Object],[object Object],[object Object],15-
[object Object],[object Object],[object Object],15-
[object Object],[object Object],[object Object],[object Object],15-
[object Object],[object Object],[object Object],[object Object],15- B1 B2 D
[object Object],[object Object],15-
[object Object],15- B D1 D2 D3
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],15-
[object Object],[object Object],[object Object],Unit 02
[object Object],[object Object],[object Object],Unit 02 ,[object Object]
[object Object],[object Object],15-
[object Object],[object Object],15-
[object Object],[object Object],[object Object],15-
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],15-
[object Object],[object Object],15-
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Unit 02 Employee class must be abstract because each type of employee has different pay rate and pay calculation is different. So we make is abstract by making the pay function as pure virtual.
class HourlyEmployee: Employee { private: int hours; public:   void getdata(){  {   Employee::getdata();   cout<<&quot; Enter number of hours worked....&quot;;   cin>>hours;    } void toString() {  Employee::toString();   cout<<&quot; Hours worked...&quot;<<hours;   }   double pay() {   return payRate * hours;   } }; Pay() function must to be redefined
[object Object],[object Object],Unit 02
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],We are defining the class person as Abstract class because it provides interface for the class student and professor.  The functions getdata() and is_outstanding() are pure virtual and needs to be redefined in each derived class  This class saves the data of students and professors which are of person type. The program finds the out_standing students and professors.
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],15- We are defining the class person as Abstract class because it provides interface for the class student and professor.  The functions getdata() and is_outstanding() are pure virtual and needs to be redefined in each derived class. A student is outstanding if his GPA is more than 3.75 and getdata() fucntion calls getname() function of person class.
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],15- Professor class creates the getdata() function and calls the person class function getname() and number of publications. Professor is outstanding if his publications are more than 10.
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],15- Ptr is a pointers array of base class (person). Due to pointer it can point to derived class objects like student and professor.
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],15- Now the array ptr has the record of students and professors. Because of pointers the dynamic binding is applied which needs the function to be defined as virtual. Because of virtual function is_outstanding() is called accordingly.
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Unit 02
[object Object],[object Object],Unit 02
[object Object],[object Object],[object Object],[object Object],[object Object],Unit 02
[object Object],[object Object],15-
[object Object],[object Object],[object Object],[object Object],[object Object],Unit 01
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Unit 02
[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],Car Vehicle A car is ‘a kind of’ vehicle car class can inherit from  vehicle class
[object Object],Car Wheel  A wheel isn’t ‘a kind of’ car. A wheel is ‘a part of’ a car  - this is dealt with by aggregation which is this week’s topic
[object Object],[object Object]
[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object]
class Wheel { private: int diameter; public: Wheel(int diameter_in){ diameter = diameter_in; } int getDiameter() { return diameter; } }; class Engine  { private: int cc; public: Engine(int cc_in)  { cc = cc_in; } int getCC()return cc; } };
class Car { private: Wheel nearside_front, offside_front,  nearside_rear, offside_rear; Engine engine; int passengers; public: Car(int diameter_in, int cc_in, int passengers_in); void showSelf(); };
Car::Car(int diameter_in, int cc_in, int passengers_in) : nearside_front(diameter_in), offside_front(diameter_in), nearside_rear(diameter_in), offside_rear(diameter_in), engine(cc_in) ‏ { passengers = passengers_in; }
15-
HL 1. Main component / sub component Composition Aggregation &quot;contains &quot; relation &quot;has&quot; relation: Main component &quot;has&quot; a sub component as a part. 2. Association &quot;owns&quot; &quot;knows&quot; &quot;uses&quot; &quot;serves“ “ observes” etc. 3.  Generalisation / specialisation Inheritance General case / Special case Super class / Sub class Base class / Derived class Base class / Inherited class &quot;is&quot; relation. An instance of derived class &quot;is&quot; an instance of base class.
HL Composition means same as  - contains - is part of Example 1. Point can be a sub component of Circle. class Circle { public: Circle(float x0=0.0, float y0=0.0, r0=0.0); Circle(const Point &cp0, r0=0.0); private: float r; Point cp;  //sub component }; Constuctor implementation on the next page.   Example 2. Date can be used as a data member in class Person: class Person {   public:   Person(const string& name, int year,  int month, int day);   Person(const string& name, const Date& bd);   private:   string name;  // sub component 1   Date birthDay;  // sub component 2 };
HL An object can have an object as a subcomponent. In this example circles have a point as a subcomponent (representing their center point  */ #include <iostream.h> //Class definition of Point class Point { public: Point(float x0=0.0, float y0=0.0); void read(); void print(); private: float x; float y; }; //Class definition of Circle class Circle{ public: Circle(float x0=0.0, float y0=0.0, float r0=0.0); Circle(const Point &cp0, float r0=0.0); void read(); void print(); private: float radius; Point centerPoint; };
HL //Application void main (void) { Point p(11.0, 12.0); Circle c1, c2(1.0, 2.0, 10.0), c3(p, 20.0); c1.print(); c2.print(); c3.print(); } //Member function implementations //Member functions of Point Point::Point(float x0, float y0) { x = x0; y = y0; } void Point :: read() { cout << &quot; x-coordinate: &quot;; cin >> x; cout << &quot; y-coordinate: &quot;; cin >> y; } void Point :: print() { cout << &quot;(&quot;  << x << &quot;,&quot; << y <<  &quot;)&quot;; }
HL //Member functions of Circle //Constructor 1 version 1 Circle::Circle(float x0, float y0, float r0): centerPoint(x0, y0){ radius = r0; } /* Constructor 1 version 2  (Point constructor is called twice) Circle::Circle(float x0, float y0, float r0) { radius = r0; centerPoint = Point(x0, y0); } */ //Constructor 2 version 1 Circle::Circle(const Point &cp0, float r0):centerPoint(cp0){ radius = r0; } /*Constructor 2 version 2  (Point constructor is called twice) Circle::Circle(const Point &cp0, float r0) { radius = r0; centerPoint = Point(cp0);  //Default copy constructor of Point } */ void Circle :: read() { cout << &quot;Enter radius : &quot;; cin >> radius; cout << &quot;Enter centerpoint :&quot;; centerPoint.read(); } void Circle :: print() { cout << &quot;The circle data is :&quot;; cout << &quot;Radius is : &quot; << radius; cout << &quot;Center point is :&quot;; centerPoint.print(); }
HL Memory allocation is done at the same time (as one block of memory) for the main component and for sub component, because sub component is part of the memory area of the main component. The object is created in the following phases: 1. Memory is allocated for the whole object. 2. The constructor call of sub component class that is defined in the initialisation list of main component is executed. If there is no sub component constructor call in the initialisation list, the default constructor of sub component is called.  3. The function body of main component constructor is called. This rule of order means that it is guaranteed that sub component is already constructed when constructor body on main component is executed. It also guarantees that sub components are always initialised at least with default constructor of sub component class. This is useful as you can understand thinking about the differences between defining a name member in the Person class as char *name or  string name!. The implementor of the Person class needs to do many things in the former case but almost nothing in the latter case.
HL When an object containing a sub-component object is de-allocated, the things happen in the following order: 1.The destructor of the main component is called first. 2. The destructor of the sub-component is called next. 3. The memory area is de-allocated as a whole. The  default assignment  operator  of the main component:  If we don’t overload the assignment operator in the main component class the default assignment is used. If we have overloaded assignment for the sub-component that is used in the default assignment when sub-component part is copied. The  default copy constructor  of the main component:  If we don’t write the copy constructor for the main component class the default copy constructor is used. If we have written copy constructor for the sub-component it is used in the default copy constructor when sub-component part is constructed.
[object Object],[object Object],[object Object],[object Object],Unit 02
[object Object],[object Object],15-
[object Object],[object Object],[object Object],[object Object],[object Object],Unit 01

Mais conteúdo relacionado

Mais procurados

Support for Object-Oriented Programming (OOP) in C++
Support for Object-Oriented Programming (OOP) in C++Support for Object-Oriented Programming (OOP) in C++
Support for Object-Oriented Programming (OOP) in C++Ameen Sha'arawi
 
#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop InheritanceHadziq Fabroyir
 
C++ Multiple Inheritance
C++ Multiple InheritanceC++ Multiple Inheritance
C++ Multiple Inheritanceharshaltambe
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorialsFALLEE31188
 
2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - AbstractionMichael Heron
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7kamal kotecha
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructorsPraveen M Jigajinni
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classDeepak Singh
 
14. Defining Classes
14. Defining Classes14. Defining Classes
14. Defining ClassesIntro C# Book
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending ClassesNilesh Dalvi
 
Learn Concept of Class and Object in C# Part 3
Learn Concept of Class and Object in C#  Part 3Learn Concept of Class and Object in C#  Part 3
Learn Concept of Class and Object in C# Part 3C# Learning Classes
 
pointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismpointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismrattaj
 

Mais procurados (20)

Support for Object-Oriented Programming (OOP) in C++
Support for Object-Oriented Programming (OOP) in C++Support for Object-Oriented Programming (OOP) in C++
Support for Object-Oriented Programming (OOP) in C++
 
#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance
 
Class and object
Class and objectClass and object
Class and object
 
C++ oop
C++ oopC++ oop
C++ oop
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
OOP C++
OOP C++OOP C++
OOP C++
 
C++ Multiple Inheritance
C++ Multiple InheritanceC++ Multiple Inheritance
C++ Multiple Inheritance
 
inhertance c++
inhertance c++inhertance c++
inhertance c++
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - Abstraction
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructors
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-class
 
14. Defining Classes
14. Defining Classes14. Defining Classes
14. Defining Classes
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending Classes
 
Learn Concept of Class and Object in C# Part 3
Learn Concept of Class and Object in C#  Part 3Learn Concept of Class and Object in C#  Part 3
Learn Concept of Class and Object in C# Part 3
 
pointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismpointers,virtual functions and polymorphism
pointers,virtual functions and polymorphism
 

Destaque

20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental PrinciplesIntro C# Book
 
STL ALGORITHMS
STL ALGORITHMSSTL ALGORITHMS
STL ALGORITHMSfawzmasood
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingfarhan amjad
 
Improving The Quality of Existing Software
Improving The Quality of Existing SoftwareImproving The Quality of Existing Software
Improving The Quality of Existing SoftwareSteven Smith
 
C++ Advanced
C++ AdvancedC++ Advanced
C++ AdvancedVivek Das
 
Inheritance chapter-6-computer-science-with-c++ opt
Inheritance chapter-6-computer-science-with-c++ optInheritance chapter-6-computer-science-with-c++ opt
Inheritance chapter-6-computer-science-with-c++ optdeepakskb2013
 
abitha-pds inheritance presentation
abitha-pds inheritance presentationabitha-pds inheritance presentation
abitha-pds inheritance presentationabitha ben
 
Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1PRN USM
 
Bjarne Stroustrup - The Essence of C++: With Examples in C++84, C++98, C++11,...
Bjarne Stroustrup - The Essence of C++: With Examples in C++84, C++98, C++11,...Bjarne Stroustrup - The Essence of C++: With Examples in C++84, C++98, C++11,...
Bjarne Stroustrup - The Essence of C++: With Examples in C++84, C++98, C++11,...Complement Verb
 
Chapter 04 inheritance
Chapter 04 inheritanceChapter 04 inheritance
Chapter 04 inheritanceNurhanna Aziz
 
An Introduction to Part of C++ STL
An Introduction to Part of C++ STLAn Introduction to Part of C++ STL
An Introduction to Part of C++ STL乐群 陈
 
Inheritance question class 12th
Inheritance question class 12thInheritance question class 12th
Inheritance question class 12thAAKASH KUMAR
 

Destaque (20)

20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles
 
Inheritance
InheritanceInheritance
Inheritance
 
Idiomatic C++
Idiomatic C++Idiomatic C++
Idiomatic C++
 
The Style of C++ 11
The Style of C++ 11The Style of C++ 11
The Style of C++ 11
 
STL ALGORITHMS
STL ALGORITHMSSTL ALGORITHMS
STL ALGORITHMS
 
Distributed Systems Design
Distributed Systems DesignDistributed Systems Design
Distributed Systems Design
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Improving The Quality of Existing Software
Improving The Quality of Existing SoftwareImproving The Quality of Existing Software
Improving The Quality of Existing Software
 
C++ Advanced
C++ AdvancedC++ Advanced
C++ Advanced
 
Inheritance chapter-6-computer-science-with-c++ opt
Inheritance chapter-6-computer-science-with-c++ optInheritance chapter-6-computer-science-with-c++ opt
Inheritance chapter-6-computer-science-with-c++ opt
 
Lecture4
Lecture4Lecture4
Lecture4
 
abitha-pds inheritance presentation
abitha-pds inheritance presentationabitha-pds inheritance presentation
abitha-pds inheritance presentation
 
Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1
 
Bjarne Stroustrup - The Essence of C++: With Examples in C++84, C++98, C++11,...
Bjarne Stroustrup - The Essence of C++: With Examples in C++84, C++98, C++11,...Bjarne Stroustrup - The Essence of C++: With Examples in C++84, C++98, C++11,...
Bjarne Stroustrup - The Essence of C++: With Examples in C++84, C++98, C++11,...
 
Web Service Basics and NWS Setup
Web Service  Basics and NWS SetupWeb Service  Basics and NWS Setup
Web Service Basics and NWS Setup
 
Chapter 04 inheritance
Chapter 04 inheritanceChapter 04 inheritance
Chapter 04 inheritance
 
Polymorphism (2)
Polymorphism (2)Polymorphism (2)
Polymorphism (2)
 
Operator overloading
Operator overloading Operator overloading
Operator overloading
 
An Introduction to Part of C++ STL
An Introduction to Part of C++ STLAn Introduction to Part of C++ STL
An Introduction to Part of C++ STL
 
Inheritance question class 12th
Inheritance question class 12thInheritance question class 12th
Inheritance question class 12th
 

Semelhante a Inheritance, Encapsulation, Polymorphism and Reuse in Object Oriented Programming

Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methodsfarhan amjad
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++Swarup Boro
 
Chapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).pptChapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).ppthenokmetaferia1
 
Hello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdfHello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdfirshadkumar3
 
Lecture 5 Inheritance
Lecture 5 InheritanceLecture 5 Inheritance
Lecture 5 Inheritancebunnykhan
 
Java Generics
Java GenericsJava Generics
Java Genericsjeslie
 
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptxinheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptxurvashipundir04
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.pptEmanAsem4
 
OOP Concepets and UML Class Diagrams
OOP Concepets and UML Class DiagramsOOP Concepets and UML Class Diagrams
OOP Concepets and UML Class DiagramsBhathiya Nuwan
 

Semelhante a Inheritance, Encapsulation, Polymorphism and Reuse in Object Oriented Programming (20)

Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 
00ps inheritace using c++
00ps inheritace using c++00ps inheritace using c++
00ps inheritace using c++
 
inheritance
inheritanceinheritance
inheritance
 
OOP Lab Report.docx
OOP Lab Report.docxOOP Lab Report.docx
OOP Lab Report.docx
 
Inheritance
InheritanceInheritance
Inheritance
 
Java Basic day-2
Java Basic day-2Java Basic day-2
Java Basic day-2
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
Chapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).pptChapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).ppt
 
Hello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdfHello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdf
 
class c++
class c++class c++
class c++
 
Chap-3 Inheritance.pptx
Chap-3 Inheritance.pptxChap-3 Inheritance.pptx
Chap-3 Inheritance.pptx
 
Lecture 5 Inheritance
Lecture 5 InheritanceLecture 5 Inheritance
Lecture 5 Inheritance
 
Java Generics
Java GenericsJava Generics
Java Generics
 
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptxinheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.ppt
 
INHERITANCE.pptx
INHERITANCE.pptxINHERITANCE.pptx
INHERITANCE.pptx
 
OOP Concepets and UML Class Diagrams
OOP Concepets and UML Class DiagramsOOP Concepets and UML Class Diagrams
OOP Concepets and UML Class Diagrams
 
Ppt of c++ vs c#
Ppt of c++ vs c#Ppt of c++ vs c#
Ppt of c++ vs c#
 
Inheritance
InheritanceInheritance
Inheritance
 

Último

BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 

Último (20)

BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 

Inheritance, Encapsulation, Polymorphism and Reuse in Object Oriented Programming

  • 2.
  • 3.
  • 4.
  • 5.  
  • 6.  
  • 7.  
  • 8.  
  • 9.
  • 10.
  • 11.  
  • 12.
  • 13.
  • 14. Base class Derived class Derived class Shape class Triangle class Circle class Right-angle Triangle Equilateral Triangle Isosceles Triangle
  • 15.
  • 16. student print() year_group() CR print() inherits (isa) student_id, year, name dept, thesis
  • 17.  
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23. class Shape { protected : int width, height; public : void setDims ( int a, int b){ width=a; height=b;} }; class Rectangle: public Shape { public : int area ( ) { return (width * height); } }; class Triangle: public Shape { public : int area ( ) { return (width * height/2); } }; class Square: public Rectangle { public : void setDims ( int a){ width=a; height=a;} };
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.  
  • 31.
  • 32.
  • 33.
  • 34. Animal Display() zoo[0]->Display(); zoo[1]->Display(); zoo[2]->Display(); Dog Display() Cat Display()
  • 35.  
  • 36. Inheritance Hierarchy for Person-derived Classes
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110. public inheritance protected inheritance private inheritance public member in base class public in derived class protected in derived class private in derived class protected member in base class protected in derived class protected in derived class private in derived class private member in base class hidden in derived class hidden in derived class hidden in derived class
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161. class HourlyEmployee: Employee { private: int hours; public: void getdata(){ { Employee::getdata(); cout<<&quot; Enter number of hours worked....&quot;; cin>>hours; } void toString() { Employee::toString(); cout<<&quot; Hours worked...&quot;<<hours; } double pay() { return payRate * hours; } }; Pay() function must to be redefined
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182. class Wheel { private: int diameter; public: Wheel(int diameter_in){ diameter = diameter_in; } int getDiameter() { return diameter; } }; class Engine { private: int cc; public: Engine(int cc_in) { cc = cc_in; } int getCC()return cc; } };
  • 183. class Car { private: Wheel nearside_front, offside_front, nearside_rear, offside_rear; Engine engine; int passengers; public: Car(int diameter_in, int cc_in, int passengers_in); void showSelf(); };
  • 184. Car::Car(int diameter_in, int cc_in, int passengers_in) : nearside_front(diameter_in), offside_front(diameter_in), nearside_rear(diameter_in), offside_rear(diameter_in), engine(cc_in) ‏ { passengers = passengers_in; }
  • 185. 15-
  • 186. HL 1. Main component / sub component Composition Aggregation &quot;contains &quot; relation &quot;has&quot; relation: Main component &quot;has&quot; a sub component as a part. 2. Association &quot;owns&quot; &quot;knows&quot; &quot;uses&quot; &quot;serves“ “ observes” etc. 3. Generalisation / specialisation Inheritance General case / Special case Super class / Sub class Base class / Derived class Base class / Inherited class &quot;is&quot; relation. An instance of derived class &quot;is&quot; an instance of base class.
  • 187. HL Composition means same as - contains - is part of Example 1. Point can be a sub component of Circle. class Circle { public: Circle(float x0=0.0, float y0=0.0, r0=0.0); Circle(const Point &cp0, r0=0.0); private: float r; Point cp; //sub component }; Constuctor implementation on the next page. Example 2. Date can be used as a data member in class Person: class Person { public: Person(const string& name, int year, int month, int day); Person(const string& name, const Date& bd); private: string name; // sub component 1 Date birthDay; // sub component 2 };
  • 188. HL An object can have an object as a subcomponent. In this example circles have a point as a subcomponent (representing their center point */ #include <iostream.h> //Class definition of Point class Point { public: Point(float x0=0.0, float y0=0.0); void read(); void print(); private: float x; float y; }; //Class definition of Circle class Circle{ public: Circle(float x0=0.0, float y0=0.0, float r0=0.0); Circle(const Point &cp0, float r0=0.0); void read(); void print(); private: float radius; Point centerPoint; };
  • 189. HL //Application void main (void) { Point p(11.0, 12.0); Circle c1, c2(1.0, 2.0, 10.0), c3(p, 20.0); c1.print(); c2.print(); c3.print(); } //Member function implementations //Member functions of Point Point::Point(float x0, float y0) { x = x0; y = y0; } void Point :: read() { cout << &quot; x-coordinate: &quot;; cin >> x; cout << &quot; y-coordinate: &quot;; cin >> y; } void Point :: print() { cout << &quot;(&quot; << x << &quot;,&quot; << y << &quot;)&quot;; }
  • 190. HL //Member functions of Circle //Constructor 1 version 1 Circle::Circle(float x0, float y0, float r0): centerPoint(x0, y0){ radius = r0; } /* Constructor 1 version 2 (Point constructor is called twice) Circle::Circle(float x0, float y0, float r0) { radius = r0; centerPoint = Point(x0, y0); } */ //Constructor 2 version 1 Circle::Circle(const Point &cp0, float r0):centerPoint(cp0){ radius = r0; } /*Constructor 2 version 2 (Point constructor is called twice) Circle::Circle(const Point &cp0, float r0) { radius = r0; centerPoint = Point(cp0); //Default copy constructor of Point } */ void Circle :: read() { cout << &quot;Enter radius : &quot;; cin >> radius; cout << &quot;Enter centerpoint :&quot;; centerPoint.read(); } void Circle :: print() { cout << &quot;The circle data is :&quot;; cout << &quot;Radius is : &quot; << radius; cout << &quot;Center point is :&quot;; centerPoint.print(); }
  • 191. HL Memory allocation is done at the same time (as one block of memory) for the main component and for sub component, because sub component is part of the memory area of the main component. The object is created in the following phases: 1. Memory is allocated for the whole object. 2. The constructor call of sub component class that is defined in the initialisation list of main component is executed. If there is no sub component constructor call in the initialisation list, the default constructor of sub component is called. 3. The function body of main component constructor is called. This rule of order means that it is guaranteed that sub component is already constructed when constructor body on main component is executed. It also guarantees that sub components are always initialised at least with default constructor of sub component class. This is useful as you can understand thinking about the differences between defining a name member in the Person class as char *name or string name!. The implementor of the Person class needs to do many things in the former case but almost nothing in the latter case.
  • 192. HL When an object containing a sub-component object is de-allocated, the things happen in the following order: 1.The destructor of the main component is called first. 2. The destructor of the sub-component is called next. 3. The memory area is de-allocated as a whole. The default assignment operator of the main component: If we don’t overload the assignment operator in the main component class the default assignment is used. If we have overloaded assignment for the sub-component that is used in the default assignment when sub-component part is copied. The default copy constructor of the main component: If we don’t write the copy constructor for the main component class the default copy constructor is used. If we have written copy constructor for the sub-component it is used in the default copy constructor when sub-component part is constructed.
  • 193.
  • 194.
  • 195.

Notas do Editor

  1. The reusability is a key benefit of OO programming. By reusing the code the development effort is thus reduced. The time can further be spent in making the software reliable and ROBUST
  2. Example: Design an application, teachers and the principal (director) in a school. First, assume that we have a class to define teachers, then we can use this class to model the principal. Because the principal is a teacher.
  3. See inheritance4.h and pr15-02.cpp
  4. See inheritance6.h and pr15-05.cpp
  5. See pr15-04.cpp