Presented By: Nazia Hossain Lecturer, Stamford University

Slides:



Advertisements
Similar presentations
Inheritance (2).
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
1 CSC241: Object Oriented Programming Lecture No 21.
Inheritance Math 130 B Smith: Consider using the example in SAMS Teach Yourself C++ in 24 hours B Smith: Consider using the example in SAMS Teach Yourself.
Derived Classes. C++ 2 Outline  Definition  Virtual functions  Virtual base classes  Abstract classes. Pure virtual functions.
C++ Classes & Data Abstraction
LOGO Lecturer: Abdullahi Salad Abdi May 1, Object Oriented Programming in C++
Exercises on Basic OOP TCP1201: 2013/2014. Catch the Bug 1 class Point { private : int x, y; public : Point(int u, int v) : x(u), y(v) { } int getX()
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 28P. 1Winter Quarter Inheritance and Overloading.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
1 Object-Oriented Programming Using C++ CLASS 27.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Inheritance CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CE Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
Inheritance One of the most powerful features of C++
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
Monday, Mar 31, 2003Kate Gregory with material from Deitel and Deitel Week 12 Labs 4 and 5 are back File IO Looking ahead to the final.
LECTURE LECTURE 15 Inheritance Text book p
Chapter 10 Inheritance and Polymorphism
Computer Programming & Applications Mohamed Iqbal Pallipurath Lecture 02P. 1 Inheritance and Overloading Lecture 28.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Inheritance.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
INHERITANCE : Extending Classes. Rickshaw cart Bus Car Pulled Vehicles Inheritance Inheritance Vehicles Inheritance is the capability of one class of.
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
CS1201: Programming Language 2 Classes and objects Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits.
Constructors & Destructors, Proxy Classes, Friend Function and example of static member.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
Class Inheritance SWE 344 Internet Protocols & Client Server Programming.
C# - Inheritance Ashima Wadhwa. Inheritance One of the most important concepts in object- oriented programming is inheritance. Inheritance allows us to.
1 Introduction to Object Oriented Programming Chapter 10.
1.What are the differences between composition(“has a” relationship) and inheritance(“is a” relationship)? 2.What is a base class? 3.How to declare a derived.
Oops concepts Object-orientation Objects and classes Overloading Inheritance Polymorphism Generic Programming Exception Handling Introduction to design.
Classes Sujana Jyothi C++ Workshop Day 2. A class in C++ is an encapsulation of data members and functions that manipulate the data. A class is a mechanism.
CSC241 Object-Oriented Programming (OOP) Lecture No. 14.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
1 Object-Oriented Programming Using C++ CLASS 2 Honors.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 CSE 2341 Object Oriented Programming with C++ Note Set #12.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Overloaded Constructors and Multiple Classes
Inheritance Concept of Inheritance . Types of Inheritance .
C# - Inheritance and Polymorphism
Chapter 5 Classes.
group work #hifiTeam
Polymorphism Lec
Object Oriented Programming Language (OOP)
Dynamic Memory Allocation Reference Variables
Inheritance Dr. Bhargavi Goswami Department of Computer Science
មេរៀនទី៩ Class & Object C/C++ for beginner
CS1201: Programming Language 2
CS1201: Programming Language 2
C++ Inheritance.
Object-Oriented Programming (OOP) Lecture No. 22
What about multi-dimensional arrays?
Institute of Petroloeum Technology, Gandhinagar
TOPIC: FUNCTION OVERLOADING
Classes: Arrays group many objects of the same type (in order)
Introduction to Algorithms and Programming COMP151
C++ Object Oriented 1.
Computer Science II for Majors
Presentation transcript:

Presented By: Nazia Hossain Lecturer, Stamford University Lab 4: Inheritence Presented By: Nazia Hossain Lecturer, Stamford University

Inheritence One of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class, that provides an opportunity to reuse the code functionality and fast implementation time. When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.

Base & Derived Classes To define a derived class, we use a class derivation list to specify the base class(es). A class derivation list names one or more base classes and has the form: class derived-class: access-specifier base-class Where access-specifier is one of public, protected, or private, and base-class is the name of a previously defined class. If the access-specifier is not used, then it is private by default.

Consider a base class Shape and its derived class Rectangle as follows: #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; What is the output of this program? Ans:

Access Control and Inheritance: Public Protected Private Same Class Yes Derived Class No Outside Class

A derived class inherits all base class methods with the following exceptions: Constructors, destructors and copy constructors of the base class. Overloaded operators of the base class. The friend functions of the base class.

Type of Inheritance: Derived Class Visibility Base Class visibility Public derivation Private derivation Protected derivation Private Not Inherited Not inherited Protected Public

Multiple Inheritances A C++ class can inherit members from more than one class and here is the extended syntax: class derived-class: access baseA, access baseB....

Example of Multiple Inheritance #include <iostream> using namespace std; // Base class Shape class Shape { public: void setWidth(int w) width = w; } void setHeight(int h) height = h; protected: int width; int height; };

// Base class PaintCost class PaintCost { public: int getCost(int area) return area * 70; } }; // Derived class class Rectangle: public Shape, public PaintCost int getArea() return (width * height);

int main(void) { Rectangle Rect; int area; Rect. setWidth(5); Rect int main(void) { Rectangle Rect; int area; Rect.setWidth(5); Rect.setHeight(7); area = Rect.getArea(); // Print the area of the object. cout << "Total area: " << Rect.getArea() << endl; // Print the total cost of painting cout << "Total paint cost: $" << Rect.getCost(area) << endl; return 0; } What is the output of this program? Answer:

Hybrid Inheritance Hybrid Inheritance is the combination of two or more inheritances : single, multiple,multilevel or hierarchical Inheritances. The following is a C++ Program to for Calculating the marks secured by a student.A Parent class with student identification is created and another class called marks is inherited from the main class.This class marks is further inherited by another class called sports and finally the sports class is inherited by the percentage class to calculated the percentage of marks.

#include<iostream> #include<conio. h> #include<stdio #include<iostream> #include<conio.h> #include<stdio.h> #include<string.h> using namespace std; class student_id { //c-madeeasy.blogspot.com int rno; char name[20]; public: void read_id() { cout<<"\n\nEnter the Name of the Student : "; gets(name); cout<<"\n\nEnter the Roll No. : "; cin>>rno; } void display_nr() cout<<"\n\n\t\tSTUDENT REPORT\n\nNAME : "; puts(name); cout<<"\n\nROLL NO. : "<<rno; };

class marks:public student_id { public: int i,mark[3]; void read_m() read_id(); for(i=0;i<3;i++) { cout<<"\n\nEnter the Marks Secured in SUBJECT "<<i+1<<" out of 100 : "; cin>>mark[i]; } void display_m() { display_nr(); cout<<"\n\n\tMarks Secured "; for(i=0;i<3;i++) cout<<"\n\nSUBJECT "<<i+1<<" : "<<mark[i]; };

class sports { public: int sm; void read_sportm() { cout<<"\n\nEnter the marks in SPORTS out of 10 : "; cin>>sm; } };

class percentage:public marks,public sports { public: float total,prcntge; void calculate() read_m(); read_sportm(); total=0; for(i=0;i<3;i++) { total+=mark[i]; } total+=sm; prcntge=(total/310)*100; void display_totp() display_m(); cout<<"\n\nTOTAL = "<<total; cout<<"\n\nPERCENTAGE = "<<prcntge; };

int main() { int cont; percentage pc; do pc. calculate(); pc int main() { int cont; percentage pc; do pc.calculate(); pc.display_totp(); cout<<"\n\nDo You Want to Continue?(1-YES/0-NO)"; cin>>cont; }while(cont==1); getch(); return 0; }