CHAPTER 6 Class-Advanced Concepts - Inheritance

Similar documents
CHAPTER 5 Class - Basic Concepts

Programming in C# Inheritance and Polymorphism

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Inheritance Motivation

Inheritance (Deitel chapter 9)

Object Oriented Programming. CISC181 Introduction to Computer Science. Dr. McCoy. Lecture 27 December 8, What is a class? Extending a Hierarchy

Problem: Given a vector of pointers to Shape objects, print the area of each of the shape.

Chapter 19 - C++ Inheritance

Inheritance

Introduction to inheritance

Chapter 19 C++ Inheritance

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

Chapter 9 - Object-Oriented Programming: Inheritance

Inheritance: Single level inheritance:

Inheritance, and Polymorphism.

Fig. 9.1 Fig. 9.2 Fig. 9.3 Fig. 9.4 Fig. 9.5 Fig. 9.6 Fig. 9.7 Fig. 9.8 Fig. 9.9 Fig Fig. 9.11

Inheritance Introduction. 9.1 Introduction 361

Developed By Strawberry

Data Structures (INE2011)

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

CHAPTER 4 Structures

Developed By Strawberry

Object Oriented Programming

PES Institute of Technology

Exam Duration: 2hrs and 30min Software Design

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

Object Oriented Programming 2012

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

Programming C++ Lecture 3. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor

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

Developed By Strawberry

AP Computer Science. Interactions with superclass

INHERITANCE - Part 1. CSC 330 OO Software Design 1

Inheritance and Overloading. Week 11

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

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Developed By Strawberry

drawobject Circle draw

Classes and Data Abstraction: struct

QUESTIONS AND ANSWERS. 1) To access a global variable when there is a local variable with same name:

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

ENCAPSULATION AND POLYMORPHISM

Information System Design (IT60105)

Java Object Oriented Design. CSC207 Fall 2014

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

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

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

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

JAVA MOCK TEST JAVA MOCK TEST II

Base class or Super class. Subclass or Derived class

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

OBJECT ORIENTED PROGRAMMING

CS111: PROGRAMMING LANGUAGE II

CS OBJECT ORIENTED PROGRAMMING

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

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name:

1.00 Lecture 13. Inheritance

2. ARRAYS What is an Array? index number subscrip 2. Write array declarations for the following: 3. What is array initialization?

Lecture 5: Inheritance

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

An Introduction to Object Orientation

ITI Introduction to Computing II

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:

VALLIAMMAI ENGINEERING COLLEGE

Object Oriented Programming

Review Questions for Final Exam

Object oriented programming

C++ (classes) Hwansoo Han

Lecture 15: Inheritance II

ITI Introduction to Computing II

Introduction to C++ (using Microsoft Visual C++)

//employee.h class employee { private: string name; double salary; string gender; int empid; static int empcounter; public: employee() {

CMSC 132: Object-Oriented Programming II. Inheritance

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

Classes and Inheritance Extending Classes, Chapter 5.2

Java Programming Lecture 7

Software and Programming 1

CS105 C++ Lecture 7. More on Classes, Inheritance

For the partial fulfillment of CBSE Exam Made By: Ashish kumar jha Class : XII Science Roll No :

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

02. Brieflydescribe the following fundamental concepts of object oriented world

COMPUTER PROGRAMMING (ECE 431) TUTORIAL 9

CSCI 1370 APRIL 26, 2017

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.

Programming in C++: Programming Test-2

STUDY ON INHERITANCE OF CLASSES

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

Nets and Drawings for Visualizing Geometry. Unit 1 Lesson 1

SSE2034: System Software Experiment 3 Spring 2016

UML - Class Diagrams

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

Inheritance and Polymorphism

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Java and OOP. Part 3 Extending classes. OOP in Java : W. Milner 2005 : Slide 1

INHERITANCE - Part 1. CSC 330 OO Software Design 1

Object- Oriented Programming: Inheritance

Lab Manual Object Oriented Programming with JAVA (15ECSP203)

Rani Durgavati Vishwavidyalaya Jabalpur (M.P.) (UICSA) Master of Computer Application (MCA) Practical List of. MCA III SEM Session -2010

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

Transcription:

CHAPTER 6 Class-Advanced Concepts - Inheritance Page 1

Introduction: The idea of deriving a new class from the existing class. Provides the idea of code reusability. Existing class is called as base class (or) super class. Derived class is called as sub class. Derived class inherits the data members and member functions from the previously defined base class. Example: Base class Student Shape Loan Employee Account Derived classes GraduateStudent UndergraduateStudent Circle Triangle Rectangle CarLoan HomeImprovementLoan MortgageLoan FacultyMember StaffMember CheckingAccount SavingsAccount Types of Inheritance 1. Single Inheritance - One base class and one derived class. ssblc esbb ssac esbb 2. Multiple Inheritance - A class is derived from more than one base classes ssblc esbb1c ssblc esbbc2c Page 2 ssac esbb

3. Multilevel Inheritance - A sub class inherits from a class which inherits from another class. ssblc esbbc ssac esbbc1 ssac esbbc2 4. Hierarchical Inheritance - More than one subclass inherited from a single base class. ssblc esbbc Implementation of public inheritance: The general format of derived class from base class in public mode is: class subclass_name:public baseclass_name.. ; ssac esbbc1 ssac esbbc2 ssac esbbc3 Page 3

protected: Intermediate level of protection between private and public access specifies. Derived-class members can refer to public and protected members of the base class simply by using the member names Note that protected data breaks encapsulation Note: When you derive the sub class from the base class in public mode, specify the base class data members as protected members. The protected members are accessible in derived class. But the private members are not accessible. This is the only difference between the private and protected access specifiers. Single Inheritance: Example Program: Write a C++ program to implement the single inheritance on the classes employee and salary. Derive the class salary from the class employee in public mode. The class employee consists of employee number, employee name and designation as data members, and getdata() as a member function. The class salary consists of basic pay, human resource allowance, dearness allowance and Profitability Fund as data members. The member functions are getsalary(), calculatesalary() and display(). #include<iostream> using namespace std; class employee protected: int eno; char name[20]; char des[20]; public: void getdata(); ; Page 4

void employee::getdata() cout<<"enter the employee number"<<endl; cin>>eno; cout<<"enter the employee name"<<endl; cin>>name; cout<<"enter the designation"<<endl; cin>>des; class salary:public employee private: float bp,hra,da,pf,np; public: void getsal(); void calsal(); void display(); ; void salary::getsal() cout<<"enter the basic pay"<<endl; cin>>bp; cout<<"enter the Human Resource Allowance"<<endl; cin>>hra; cout<<"enter the Dearness Allowance"<<endl; cin>>da; cout<<"enter the Profitability Fund"<<endl; cin>>pf; void salary::calsal() np=bp+hra+da-pf; Page 5

void salary::display() cout<<""<<endl; cout<<"employee Number is :"<<eno<<endl; cout<<"employee Name is :"<<name<<endl; cout<<"employee Designation is :"<<des<<endl; cout<<"employee Basic pay is :"<<bp<<endl; cout<<"employee Human Resource Allowance is :"<<hra<<endl; cout<<"employee Dearness Allowance is :"<<da<<endl; cout<<"employee Profitability Fund is :"<<pf<<endl; cout<<"employee Net pay is :"<<np<<endl; cout<<""<<endl; int main() int i,n; salary s[10]; cout<<"enter the number of employee"<<endl; cin>>n; for(i=0;i<n;i++) s[i].getdata(); s[i].getsal(); s[i].calsal(); for(i=0;i<n;i++) s[i].display(); return(0); Page 6

Output: Enter the number of employee 3 Enter the employee number 1234 Enter the employee name ahmed Enter the designation manager Enter the basic pay 1200 Enter the Human Resource Allowance 34 Enter the Dearness Allowance 45 Enter the Profitability Fund 67 Enter the employee number 2345 Enter the employee name salim Enter the designation accountant Enter the basic pay 1000 Enter the Human Resource Allowance 25 Enter the Dearness Allowance 34 Enter the Profitability Fund 48 Enter the employee number 3456 Enter the employee name fathma Page 7

Enter the designation receptionist Enter the basic pay 700 Enter the Human Resource Allowance 23 Enter the Dearness Allowance 25 Enter the Profitability Fund 33 Employee Number is :1234 Employee Name is :ahmed Employee Designation is :manager Employee Basic pay is :1200 Employee Human Resource Allowance is :34 Employee Dearness Allowance is :45 Employee Profitability Fund is :67 Employee Net pay is :1212 Employee Number is :2345 Employee Name is :salim Employee Designation is :accountant Employee Basic pay is :1000 Employee Human Resource Allowance is :25 Employee Dearness Allowance is :34 Employee Profitability Fund is :48 Employee Net pay is :1011 Employee Number is :3456 Employee Name is :fathma Employee Designation is :receptionist Employee Basic pay is :700 Page 8

Employee Human Resource Allowance is :23 Employee Dearness Allowance is :25 Employee Profitability Fund is :33 Employee Net pay is :715 Exercises: 1. Write a C++ program to implement the single inheritance on the classes student and mark. Derive the class mark from the class student in public mode. The class student consists of student id, student name and sex as data members, and getdata() as a member function. The class mark consists of mark1, mark2, mark3 and average as data members. The member functions are getmark(), calculateaverage() and display(). Page 9