10 inheritance
Upcoming SlideShare
Loading in...5

10 inheritance







Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

10 inheritance 10 inheritance Presentation Transcript

  • Inheritance 09/04/131 VIT - SCSE By G.SasiKumar., M.E., (Ph.D)., Assistant Professor School of Computing Science and Engineering VIT University
  • 09/04/132 VIT - SCSE 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 Inheritance base subclass1 subclass2 Notation:
  • 09/04/133 VIT - SCSE Advantages It permits code reusability. So, save time and increase the program reliability. Improve Program Reliability It Permits code sharing
  • 09/04/134 VIT - SCSE class <derived classname> : public <base classname> { --- ---- }; Syntax for Inheritance
  • Derived classBase 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
  • 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 Base Class sub Class Multiple Inheritance Class A Class B Class C
  • Class A Class B Class C Multilevel Inheritance Hierarchical Inheritance Base Class sub Class 1sub Class 3 sub Class 2
  • Hybrid Inheritance Hybrid is nothing but the combination of Multilevel and multiple Inheritance Lecturer Marks Department Student Multi level Inheritance Multiple Inheritance
  • Function Type Access directly to Private Protected Public Class member Yes Yes Yes Derived class member No Yes Yes Friend Yes Yes Yes Friend class member Yes Yes Yes
  • //Example for Single inheritance #include<iostream.h> class test { public: int a,b; void getdata(); void display(); }; class sample:public test { public: int x,y; void getinfo(); void dis(); void sum(); }; void test::getdata() { cout<<"Enter a & b:"<<endl; cin>>a>>b; } void test::display() { cout<<"a="<<a<<endl; cout<<"b="<<b<<endl; } void sample::getinfo() { cout<<"Enter x & Y:"; cin>>x>>y; } void sample::dis() { cout<<"x="<<x<<endl; cout<<"y="<<y<<endl; } void sample::sum() { int s; s=a+b+x+y; cout<<"Sum="<<s; } void main() { sample s; s.getdata(); s.display(); s.getinfo(); s.dis(); s.sum(); getch(); }
  • 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; };
  • #include<iostream.h> class base1 { protected : int v1; public : void disp_base1() { cout<<“v1 "<<v1<<endl; }}; class base2 { protected : int v2; public : void disp_base2() { cout<<“v2 is"<<v2<<endl; }}; class deri:public base1,public base2 { private: int v3; public : deri(int a,int b,int c) { v1=a; v2=b; v3=c; } void disp_me() { cout<<“v3 is"<<v3; } };
  • void main() { deri d(10,20,30); clrscr(); d.disp_base1(); d.disp_base2(); d.disp_me(); getch(); }
  • AMBIGUITY IN MULTIPLE INHERITANCE (use same member) #include<iostream.h> class base1 { public: int i; void disp() { cout<<"base1 value"<<i<<"n"; } }; class base2 { public: int i; void disp() { cout<<"base2 value"<<i; } }; class deri:public base1,public base2 { }; void main() { deri d; //d.i=10; not allowed //d.disp(); d.base1::i=10; d.base2::i=20; d.base1::disp(); d.base2::disp(); getch(); } RESULT : AMBIGUITY ERROR OCCURRED
  • //Example for MultiLevel inheritance #include<iostream.h> #include<conio.h> class publication { public: int bookno; void getdata() { cout<<"Enter Book Number:"; cin>>bookno; } void putdata() { cout<<bookno<<endl; } }; class author:public publication { public: char aname[20]; void getdata() { publication::getdata(); cout<<"Enter author name"; cin>>aname; } }; class test:public author { public: void display(); }; void test::display() { cout<<"Book Number:"<<bookno<<endl; cout<<"Author Name:"<<aname<<endl; } void main() { test t; t.getdata(); t.putdata(); t.author::getdata(); t.display(); getch(); }
  • //Example for Hierarchical inheritance #include<iostream.h> #include<conio.h> class test { public: int a,b; void getdata() { cout<<"Enter a & b:"<<endl; cin>>a>>b; } }; class simple:public test { public: void display() { cout<<"a="<<a<<endl; cout<<"b="<<b; } }; class sample:public test { public: void sum() { int sum; sum=a+b; cout<<"sum="<<sum; } }; void main() { simple s1; sample s2; s1.getdata(); s1.display(); s2.getdata(); s2.sum(); getch(); }
  • HYBRID INHERITANCE #include<iostream.h> class lecturer { private : char lecturer_name[20]; public : void getdata() { cout<<"Enter Lecturer name"; cin>>lecturer_name; } void putdata() { cout<<"lecturer name is "<<lecturer_name; } }; class department : public lecturer { private : char department_name[20]; public: void getdata() { cout<<"Enter Department name"; cin>>department_name; } void putdata() { cout<<"Department name is "<<department_name; } }; class marks { public : int mark1; int mark2; int mark3; void getdata() { cout<<"Enter the marks for 3 subjects"; cin>>mark1>>mark2>>mark3; } void putdata() { cout<<"The marks for 3 subjects are "<<mark1<<"t"<<mark2<<"t"<<mark3; } }; class student:public department,public marks { private: int roll_no;
  • char student_name[20]; public: void getdata() { department::getdata(); cout<<"Enter the student name"; cin>>student_name; cout<<"Enter the student Enrollment no"; cin>>roll_no; marks::getdata(); } void putdata() { department::putdata(); cout<<"The name & rollno is "<<student_name<<"t"<<roll_no; marks::putdata(); } }; void main() { student s; clrscr(); s.getdata(); s.putdata(); getch(); }
  • 09/04/1320 VIT - SCSE 1. The derived class need not have a constructor as long as the base class has a no-argument constructor. 2. However, if the base class has constructors with arguments (one or more), then it is mandatory for the derived class to have a constructor and pass the arguments to the base class constructor. 3. When an object of a derived class is created, the constructor of the base class is executed first and later the constructor of the derived class. Constructors in Derived Classes