Inheritance

Similar documents
UNIVERSITI TEKNIKAL MALAYSIA MELAKA FACULTY INFORMATION TECHNOLOGY AND COMMUNICATION (FTMK) BITE 1513 GAME PROGRAMMING I.

Friend Functions, Inheritance

Inheritance, and Polymorphism.

CS OBJECT ORIENTED PROGRAMMING

C++ TEMPLATES. Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.

The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is called inheritance.

Programming in C++: Assignment Week 6

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

C++ Quick Guide. Advertisements

INHERITANCE IN OBJECT ORIENTED PROGRAMMING EASIEST WAY TO TEACH AND LEARN INHERITANCE IN C++ TWINKLE PATEL

A A B U n i v e r s i t y

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

L4: Inheritance. Inheritance. Chapter 8 and 10 of Budd.

Midterm Exam 5 April 20, 2015

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak

CHAPTER 9 INHERITANCE. 9.1 Introduction


Developed By Strawberry

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Developed By Strawberry

UNIT 1 OVERVIEW LECTURE NOTES

C++ Memory Map. A pointer is a variable that holds a memory address, usually the location of another variable in memory.

Developed By Strawberry

Polymorphism CSCI 201 Principles of Software Development

Data Structures (INE2011)

Exercise1. // classes first example. #include <iostream> using namespace std; class Rectangle. int width, height; public: void set_values (int,int);

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING REWRAP TEST I CS6301 PROGRAMMING DATA STRUCTURES II

STUDY ON INHERITANCE OF CLASSES

International Journal of Advance Research in Computer Science and Management Studies

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

I SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK a) What is the difference between Hardware and Software? Give one example for each.

Inheritance: Single level inheritance:

Arrays Classes & Methods, Inheritance

CSC1322 Object-Oriented Programming Concepts

Recharge (int, int, int); //constructor declared void disply();

Inheritance and Overloading. Week 11

Sahaj Computer Solutions OOPS WITH C++

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat Chapter 6 Inheritance Extending a Class

Chapter 1: Object-Oriented Programming Using C++

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Java Object Oriented Design. CSC207 Fall 2014

COMPUTER SCIENCE C++(083) ASSIGNMENT BOOKLET CLASS XII. Academic Session:

Extending Classes (contd.) (Chapter 15) Questions:

INHERITANCE - Part 1. CSC 330 OO Software Design 1

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

JAVA MOCK TEST JAVA MOCK TEST II

Get Unique study materials from

Constructor - example

K.Yellaswamy Assistant Professor CMR College of Engineering & Technology

Lecture 6. Inheritance

C++ 프로그래밍실습. Visual Studio Smart Computing Laboratory

CS1150 Principles of Computer Science Objects and Classes

CHAPTER 6 Class-Advanced Concepts - Inheritance

Syntax to define a Structure: struct structurename { datatype membername1; datatype membername2;... } ; For Example:

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

COP 3530 Discussion Session #2 Ferhat Ay

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School

B.Sc. (Hons.) Computer Science I B.Sc. (Hons.) Electronics. (i) Runtime polymorphism and compile time polymorphism

Polymorphism Part 1 1

Introduction to Object-Oriented Programming with C++

C++ Structures Programming Workshop 2 (CSCI 1061U)

Use the template below and fill in the areas in Red to complete it.

CS 162, Lecture 25: Exam II Review. 30 May 2018

COMP 249: Object Oriented Programming II. Tutorial 2: Intro to Inheritance

Inheritance (Deitel chapter 9)

Programming in C++: Assignment Week 5

SSE2034: System Software Experiment 3 Spring 2016

Chapter 6: Inheritance

CS-202 Introduction to Object Oriented Programming

Computer Programming : C++

Objectives. INHERITANCE - Part 1. Using inheritance to promote software reusability. OOP Major Capabilities. When using Inheritance?

INHERITANCE PART 2. Constructors and Destructors under. Multiple Inheritance. Common Programming Errors. CSC 330 OO Software Design 1

Partha Sarathi Mandal

WARM UP LESSONS BARE BASICS

Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.

Inheritance and Encapsulation. Amit Gupta

Class CSE F. *slides are from CSE S at SKKU & at MIT

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

ITI Introduction to Computing II

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) Subject Name: Object Oriented Programming

Object Oriented Programming: Inheritance Polymorphism

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

Object Oriented Software Design II

Object Oriented Software Design II

ITI Introduction to Computing II

Programming in C++: Programming Test-2

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

22316 Course Title : Object Oriented Programming using C++ Max. Marks : 70 Time: 3 Hrs.

Exception with arguments

END TERM EXAMINATION

Chapter 3: Inheritance and Polymorphism

Review Questions for Final Exam

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

Laboratory 7. Programming Workshop 2 (CSCI 1061U) Faisal Qureshi.

Classes and Data Abstraction: struct

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Procedural Programming

C++ Important Questions with Answers

C++ Inheritance. Dr. Md. Humayun Kabir CSE Department, BUET

Transcription:

Inheritance 23-01-2016

Inheritance Inheritance is the capability of one class to acquire properties and characteristics from another class. For using Inheritance concept in our program we must use at least two classes. The class whose properties are inherited by other class is called the Parent or Base or Super class. And, the class which inherits properties of other class is called Child or Derived or Sub class.

Syntax: Class subclass : < access specifier (public, protected, private) > Superclass. class Person {........ class MathsTeacher : public Person {........

Access specifiers in Inheritance If access specifier is Public i.e., Class subclass: public Superclass Then the protected member of super class becomes protected members of sub class and public becomes public

Access specifiers in Inheritance (Continuation) If access specifier is protected i.e., Class subclass: protected Superclass Then the protected and public members of super class becomes protected members of sub class.

Access specifiers in Inheritance (Continuation) If access specifier is private i.e., Class subclass: private Superclass Then the protected and public members of super class becomes private members of sub class.

Simple Inheritance program: #include <iostream> using namespace std; // Base class class Shape { protected: int width; int height; public: void setwidth(int w) { width = w; void setheight(int h) { height = h; class Rectangle: public Shape { public: int getarea() { return (width * height); int main(void) { Rectangle Rect; Rect.setWidth(2); Rect.setHeight(2); cout << "Total area: " << Rect.getArea() << endl; return 0;

Advantages of Inheritance Inheritance makes the code reusable. When we inherit an existing class, all its methods and fields become available in the new class, hence code is reused. Fast implementation time

Types of Inheritance: In C++, we have 5 different types of Inheritance. Namely, 1. Single Inheritance 2. Multiple Inheritance 3. Hierarchical Inheritance 4. Multilevel Inheritance 5. Hybrid Inheritance (also known as Virtual Inheritance)

Single Inheritance Single Inheritance: In this type of inheritance one derived class inherits from only one base class. It is the simplest form of Inheritance. Syntax: class derived-class: access-specifier Base-class Class B: public A{

Multiple Inheritance: Multiple Inheritance: In this type of inheritance a single derived class may inherit from two or more than two base classes. Syntax: class derived-class: access-specifier basea, access-specifier baseb... Class c: public A, public B{

Example for Multiple Inheritance: #include <iostream> using namespace std; class Shape { protected: int width; int height; public: void setwidth(int w) { width = w; void setheight(int h) { height = h; class Rectangle: public Shape, public PaintCost { public: int getarea() { return (width * height); int main(void) { Rectangle Rect; int area; int w,h; cout<<"enter the Widhth and Height \n"; cin>>w>>h; Rect.setWidth(w); Rect.setHeight(h); class PaintCost { public: int getcost(int area) { int cost; cout<<"enter the paint cost for per inches \n"; cin>>cost; return area * cost; area = Rect.getArea(); cout << "Total area: " << Rect.getArea() << endl; cout << "Total paint cost: $" << Rect.getCost(area) << endl; return 0;

Hierarchical Inheritance: Hierarchical Inheritance: In this type of inheritance, multiple derived classes inherits from a single base class class A { //content of base class i.e., A class B :public A { //content of derived class i.e., B class C :public A { //content of derived class i.e., C class D :public A { //content of derived class i.e., D

Example for Hierarchical Inheritance: #include<iostream> using namespace std; class arithmetic{ protected: int op1,op2; public: void getop(int a, int b){ op1=a; op2=b; int main(){ sum s1; s1.getop(3,4); cout<<"addition is "<<s1.addition(); mul m1; m1.getop(3,4); cout<<"\n Multiplication is "<<m1.multiplication(); return 0; class sum: public arithmetic{ public: int addition(){ return op1+op2; class mul: public arithmetic{ public: int multiplication(){ return op1*op2;

Multilevel Inheritance: Multilevel Inheritance: class. class A { a Derived class is a base class for another //content of base class i.e., A class B :public A { //content of class i.e., B class C :public B { //content of derived class i.e., C

Example for Multilevel Inheritance: #include<iostream> using namespace std; class person{ protected: int age; char name[10]; public: void getdata(int a, char b[]){ age=a; for(int i=0;i<10;i++) name[i]=b[i]; class student: public person{ protected: char reg[10]; public: void getstu(){ cout<<"\n Enter the Register number :"; cin>>reg; void show(){ cout<<"\n student information \n "; cout<<"name :"<<name<<"\n"; cout<<"age :"<<age<<"\n"; cout<<"rollno :"<<reg<<"\n"; class nitr: public student{ protected: char branch[10]; int y; public: void getcol(){ cout<<"\n Enter the Branch :"; cin>>branch; cout<<"\n Enter the Year :"; cin>>y; void totaldata(){ cout<<"\n student information \n "; cout<<"name :"<<name<<"\n"; cout<<"age :"<<age<<"\n"; cout<<"rollno :"<<reg<<"\n"; cout<<"branch :"<<branch<<"\n"; cout<<"year :"<<y<<"\n";

int main(){ nitr p1; int a; char b[10]; cout<<"enter the name "; cin>>b; cout<<"enter the age"; cin>>a; p1.getdata(a,b); p1.getstu(); p1.getcol(); p1.totaldata(); return 0;

Hybrid Inheritance: Hybrid Inheritance (also known as Virtual Inheritance): Hybrid Inheritance is combination of Hierarchical and Mutilevel Inheritance. class A { //content of base class i.e., A class B :public A { //content of class i.e., B class C :public B { //content of derived class i.e., C Class D: public B, public C{

Try the following Example for hybrid Inheritance class Grandparent { //content of grandparent class class Child1 :public virtual Grandparent { //content of Child1 class class Child2 :public virtual Grandparent { //content of Child2 class class grandson :public Child1, public Child2 { //content of grandson class

Assignment 1 (Date: - 9/01/2017) 1. Write a sample C++ program demonstrating Variable scope. 2. Write a C++ program for finding factorial of a given number using recursion. 3. Write a C++ program to find the roots of a quadratic equation. Use switch or if statements to handle different values of the discriminant (b2 4ac). 4. Write a C++ program to create student class, which contains name, Register-Number & 5-subject marks as data members calculate average & grade by using member functions.

Assignment 2 (Date: - 16/01/2017) 1. Write a C++ program to calculate area of rectangle and square using constructor (default) and destructor 2. Write a C++ program to calculate area of circle using constructor (parameterized) and destructor 3. Write a C++ program to calculate area of triangle using constructor (copy) and destructor

Assignment 3 (Date: - 23/01/2017) Write C++ program to ready student information such as Name, DOB, Address (village/city, district, state and country, pin code), Education details like RegNO, Department, Course (b-tech, m-tech or dual degree) by illustrating following Inheritance concepts. 1. Single Inheritance 2. Multiple Inheritance 3. Hierarchical Inheritance 4. Multilevel Inheritance 5. Hybrid Inheritance (also known as Virtual Inheritance)