KEMBAR78
10.Inheritance.ppt for oops programinggg | PPT
Inheritance
10-2
What is Inheritance?
Generalization vs. Specialization
• Real-life objects are typically specialized versions of
other more general objects.
• The term “insect” describes a very general type of
creature with numerous characteristics.
• Grasshoppers and bumblebees are insects
– They share the general characteristics of an insect.
– However, they have special characteristics of their own.
• grasshoppers have a jumping ability, and
• bumblebees have a stinger.
• Grasshoppers and bumblebees are specialized
versions of an insect.
Inheritance
10-4
The “is a” Relationship
• The relationship between a superclass and an inherited
class is called an “is a” relationship.
– A grasshopper “is a” insect.
– A poodle “is a” dog.
– A car “is a” vehicle.
• A specialized object has:
– all of the characteristics of the general object, plus
– additional characteristics that make it special.
• In object-oriented programming, inheritance is used to
create an “is a” relationship among classes.
10-5
The “is a” Relationship
• We can extend the capabilities of a class.
• Inheritance involves a superclass and a subclass.
– The superclass is the general class and
– the subclass is the specialized class.
• The subclass is based on, or extended from, the superclass.
– Superclasses are also called base classes, and
– subclasses are also called derived classes.
• The relationship of classes can be thought of as parent
classes and child classes.
10-6
Inheritance, Fields and Methods
• Members of the superclass that are marked private:
– are not inherited by the subclass,
– exist in memory when the object of the subclass is created
– may only be accessed from the subclass by public methods
of the superclass.
• Members of the superclass that are marked public:
– are inherited by the subclass, and
– may be directly accessed from the subclass.
Inheritance
• It is the Process of creating a New class from an existing class.
• The Existing class is called Base or Parent class.
• The New class is called as Child or Derived Class
base
subclass1 subclass2
Derived class
Base Class
Methods
and
Properties
Base class methods
+
Additional methods
Inheritance is the property that allows the
reuse of an existing class to build a new class
Advantages
 It permits code reusability. So, save time and
increase the program reliability.
 Improve Program Reliability
 It Permits code sharing
Syntax for Inheritance
class <derived classname > : visibility mode <base classname >
{
---
----
};
visibility mode
• The visibility mode specifies whether the features are
privately derived or publicly derived.
• The default visibility mode is private.
• Privately Inherited: public members of the base class
become private members of the derived class, can be
accessed only by the member functions of the derived
class.
• Publicly inherited: public members of the base class
become public members of the derived class and can be
accessed by the derived class objects.
• Private members of the base class are not inherited in
both the cases.
Inheritance
Multilevel
Inheritance
Single Inheritance
Multiple Inheritance
Hierarchical
Inheritance
Single Inheritance
A class can be derived from a single base class is called single
inheritance
Person
Employee
• #include <iostream>
• using namespace std;
• // Base class
• class Shape {
• public:
• void setWidth(int w) {
• width = w;
• }
• void setHeight(int h) {
• height = h;
• }
• protected:
• int width;
• int height;
• };
• // Derived class
• class Rectangle : public
Shape {
• public:
• int getArea() {
• return (width *
height);
• }
• };
• int main(void) {
• Rectangle Rect;
• Rect.setWidth(5);
• Rect.setHeight(7);
• // Print the area of the object.
• cout << "Total area: " << Rect.getArea() <<
endl;
• return 0;
• }
Visibility of inherited members
Access control to the members of a
class
Multilevel
Inheritance
Vehicle
Car
Racing car
Person
Employee
Part time
Employee
A class be can derived from a derived class which is known as multilevel
inheritance.
It is the process of creating new class from more than one base
classes.
Syntax :
class <derived class >:<access specifier>
base_class1,<access specifier> base_class2...
{
private :
// members;
protected :
// members;
public :
//memebers;
};
Multiple Inheritance
Person Employee
Teacher
Same Data Member Name in
Base and Derived Class
Single level inheretance
• #include <iostream>
• using namespace std;
• class ClassA
• {
• protected :
• int width, height;
• public :
• void set_values(int x, int y)
• {
• width = x;
• height = y;
• }
• };
• class ClassC:public ClassA
• {
• protected:
• int width, height;
•
• public :
• int area()
• {
• return (width *height);
• }
• };
Same Data Member Name in Base
and Derived Class
• int main()
• {
• ClassC Obj;
• Obj.set_values(10, 20);
• cout << Obj.area() <<
endl;
• return 0;
• }
• Output: 0
• #include <iostream>
• using namespace std;
• class ClassA
• {
• protected :
• int width, height;
• public :
• void set_values(int x, int y)
• {
• width = x;
• height = y;
• }
• };
• class ClassC:public ClassA
• {
• protected:
• int width, height;
•
• public :
• int area()
• {
• return (ClassA::width
*ClassA::height);
• }
• };
Same Data Member Name in Base
and Derived Class
• int main()
• {
• ClassC Obj;
• Obj.set_values(10, 20);
• cout << Obj.area() <<
endl;
• return 0;
• }
• Output: 200
Same Data Member Name in
Base and Derived Class
Multiple inheretance
• #include <iostream>
• using namespace std;
• class person
• {
• protected:
• int regno;
• string name;
• };
• class student
• {
• protected:
• string name;
• int regno;
• };
• class studentgrade:public
person,public student
• {
• private:
• string grade;
• public:
• void details()
• {
• cin>>name;
• cin>>regno;
• }
• void display()
• {
• cout<<name;
• cout<<regno;
• }
• };
Same Data Member Name in Base
and Derived Class
• int main()
• {
• studentgrade a;
•
• a.details();
• a.display();
• }
• error: reference to
'name' is ambiguous
25 | cin>>name;
|
^~~~/tmp/TQ6cOvUM4
U.cpp:13:15: note:
candidates are:
'std::string
student::name' 13 |
string name;
• #include <iostream>
• using namespace std;
• class person
• {
• protected:
• int regno;
• string name;
• };
• class student
• {
• protected:
• string name;
• int regno;
• };
• class studentgrade:public
person,public student
• {
• private:
• string grade;
• public:
• void details()
• {
• cin>>person::name;
• cin>>person::regno;
• }
• void display()
• {
• cout<<person::name;
• cout<<person::regno;
Same Data Member Name in Base
and Derived Class
• int main()
• {
• studentgrade a;
•
• a.details();
• a.display();
• }
• saira
• 23
• saira23
Hierarchical Inheritance
Employee
Permanent
Employee
Temporary
Employee
If more than one class is inherited from a base class, it's known as hierarchical
inheritance. In general, all features that are common in child classes are included
in base class in hierarchical inheritance.
Employee: Data members: id and name
Permanent Employee: designation and department, getdetails()
and displaydetails()
Temporary employee: no.of working day and contrctor name,
getdetails and displaydetails
Hybrid Inheritance
The process of combining more than one type
of Inheritance together while deriving
subclasses in a program is called a Hybrid
Inheritance.
Person: name and age[protected], get() and print()
Student: rollno and marks[private], get() and print()
Gate Score: gatescore[private], get() and print()
PG-Student :dept_name[private]. get() and print()
10.Inheritance.ppt for oops programinggg
10.Inheritance.ppt for oops programinggg
10.Inheritance.ppt for oops programinggg
10.Inheritance.ppt for oops programinggg
10.Inheritance.ppt for oops programinggg
10.Inheritance.ppt for oops programinggg

10.Inheritance.ppt for oops programinggg

  • 1.
  • 2.
    10-2 What is Inheritance? Generalizationvs. Specialization • Real-life objects are typically specialized versions of other more general objects. • The term “insect” describes a very general type of creature with numerous characteristics. • Grasshoppers and bumblebees are insects – They share the general characteristics of an insect. – However, they have special characteristics of their own. • grasshoppers have a jumping ability, and • bumblebees have a stinger. • Grasshoppers and bumblebees are specialized versions of an insect.
  • 3.
  • 4.
    10-4 The “is a”Relationship • The relationship between a superclass and an inherited class is called an “is a” relationship. – A grasshopper “is a” insect. – A poodle “is a” dog. – A car “is a” vehicle. • A specialized object has: – all of the characteristics of the general object, plus – additional characteristics that make it special. • In object-oriented programming, inheritance is used to create an “is a” relationship among classes.
  • 5.
    10-5 The “is a”Relationship • We can extend the capabilities of a class. • Inheritance involves a superclass and a subclass. – The superclass is the general class and – the subclass is the specialized class. • The subclass is based on, or extended from, the superclass. – Superclasses are also called base classes, and – subclasses are also called derived classes. • The relationship of classes can be thought of as parent classes and child classes.
  • 6.
    10-6 Inheritance, Fields andMethods • Members of the superclass that are marked private: – are not inherited by the subclass, – exist in memory when the object of the subclass is created – may only be accessed from the subclass by public methods of the superclass. • Members of the superclass that are marked public: – are inherited by the subclass, and – may be directly accessed from the subclass.
  • 7.
    Inheritance • It isthe Process of creating a New class from an existing class. • The Existing class is called Base or Parent class. • The New class is called as Child or Derived Class base subclass1 subclass2
  • 8.
    Derived class Base Class Methods and Properties Baseclass methods + Additional methods Inheritance is the property that allows the reuse of an existing class to build a new class
  • 9.
    Advantages  It permitscode reusability. So, save time and increase the program reliability.  Improve Program Reliability  It Permits code sharing
  • 10.
    Syntax for Inheritance class<derived classname > : visibility mode <base classname > { --- ---- };
  • 11.
    visibility mode • Thevisibility mode specifies whether the features are privately derived or publicly derived. • The default visibility mode is private. • Privately Inherited: public members of the base class become private members of the derived class, can be accessed only by the member functions of the derived class. • Publicly inherited: public members of the base class become public members of the derived class and can be accessed by the derived class objects. • Private members of the base class are not inherited in both the cases.
  • 12.
  • 13.
    Single Inheritance A classcan be derived from a single base class is called single inheritance Person Employee
  • 14.
    • #include <iostream> •using namespace std; • // Base class • class Shape { • public: • void setWidth(int w) { • width = w; • } • void setHeight(int h) { • height = h; • } • protected: • int width; • int height; • }; • // Derived class • class Rectangle : public Shape { • public: • int getArea() { • return (width * height); • } • };
  • 15.
    • int main(void){ • Rectangle Rect; • Rect.setWidth(5); • Rect.setHeight(7); • // Print the area of the object. • cout << "Total area: " << Rect.getArea() << endl; • return 0; • }
  • 18.
  • 20.
    Access control tothe members of a class
  • 23.
    Multilevel Inheritance Vehicle Car Racing car Person Employee Part time Employee Aclass be can derived from a derived class which is known as multilevel inheritance.
  • 26.
    It is theprocess of creating new class from more than one base classes. Syntax : class <derived class >:<access specifier> base_class1,<access specifier> base_class2... { private : // members; protected : // members; public : //memebers; }; Multiple Inheritance Person Employee Teacher
  • 29.
    Same Data MemberName in Base and Derived Class Single level inheretance
  • 30.
    • #include <iostream> •using namespace std; • class ClassA • { • protected : • int width, height; • public : • void set_values(int x, int y) • { • width = x; • height = y; • } • }; • class ClassC:public ClassA • { • protected: • int width, height; • • public : • int area() • { • return (width *height); • } • };
  • 31.
    Same Data MemberName in Base and Derived Class • int main() • { • ClassC Obj; • Obj.set_values(10, 20); • cout << Obj.area() << endl; • return 0; • } • Output: 0
  • 32.
    • #include <iostream> •using namespace std; • class ClassA • { • protected : • int width, height; • public : • void set_values(int x, int y) • { • width = x; • height = y; • } • }; • class ClassC:public ClassA • { • protected: • int width, height; • • public : • int area() • { • return (ClassA::width *ClassA::height); • } • };
  • 33.
    Same Data MemberName in Base and Derived Class • int main() • { • ClassC Obj; • Obj.set_values(10, 20); • cout << Obj.area() << endl; • return 0; • } • Output: 200
  • 34.
    Same Data MemberName in Base and Derived Class Multiple inheretance
  • 35.
    • #include <iostream> •using namespace std; • class person • { • protected: • int regno; • string name; • }; • class student • { • protected: • string name; • int regno; • }; • class studentgrade:public person,public student • { • private: • string grade; • public: • void details() • { • cin>>name; • cin>>regno; • } • void display() • { • cout<<name; • cout<<regno; • } • };
  • 36.
    Same Data MemberName in Base and Derived Class • int main() • { • studentgrade a; • • a.details(); • a.display(); • } • error: reference to 'name' is ambiguous 25 | cin>>name; | ^~~~/tmp/TQ6cOvUM4 U.cpp:13:15: note: candidates are: 'std::string student::name' 13 | string name;
  • 37.
    • #include <iostream> •using namespace std; • class person • { • protected: • int regno; • string name; • }; • class student • { • protected: • string name; • int regno; • }; • class studentgrade:public person,public student • { • private: • string grade; • public: • void details() • { • cin>>person::name; • cin>>person::regno; • } • void display() • { • cout<<person::name; • cout<<person::regno;
  • 38.
    Same Data MemberName in Base and Derived Class • int main() • { • studentgrade a; • • a.details(); • a.display(); • } • saira • 23 • saira23
  • 39.
    Hierarchical Inheritance Employee Permanent Employee Temporary Employee If morethan one class is inherited from a base class, it's known as hierarchical inheritance. In general, all features that are common in child classes are included in base class in hierarchical inheritance. Employee: Data members: id and name Permanent Employee: designation and department, getdetails() and displaydetails() Temporary employee: no.of working day and contrctor name, getdetails and displaydetails
  • 42.
    Hybrid Inheritance The processof combining more than one type of Inheritance together while deriving subclasses in a program is called a Hybrid Inheritance.
  • 43.
    Person: name andage[protected], get() and print() Student: rollno and marks[private], get() and print() Gate Score: gatescore[private], get() and print() PG-Student :dept_name[private]. get() and print()