Computer Programming

Similar documents
Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Object Oriented Design

Computer Programming

Rushikesh K Joshi. Department of Computer Science and Engineering Indian Institute of Technology Bombay

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

CSCI-1200 Computer Science II Fall 2006 Lecture 23 C++ Inheritance and Polymorphism

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

CLASSES AND OBJECT CHAPTER 04 CLASS XII

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

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula

Inheritance and Polymorphism

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

1st Semester MTCE 601A COMPUTER SYSTEM SOFTWARE

Fundamentals of Programming. Lecture 19 Hamed Rasifard

Computer Programming

Where do we stand on inheritance?

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

6.096 Introduction to C++ January (IAP) 2009

AN OVERVIEW OF C++ 1

Object oriented programming

University of Swaziland Department Of Computer Science Supplementary Examination JULY 2012

Object oriented programming with C++

Run Time Environment

Software Design and Analysis for Engineers

G52CPP C++ Programming Lecture 13

Object-Oriented Programming EE219 Repeat 2004/2005 Page 1 of 8

A SHORT COURSE ON C++

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

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

CS101 Computer programming and utilization

CS304 Object Oriented Programming Final Term

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Partha Sarathi Mandal

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

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

CSCI 1370 APRIL 26, 2017

Packages & Random and Math Classes

International Journal of Advance Research in Computer Science and Management Studies

Inheritance and Overloading. Week 11

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

G Programming Languages - Fall 2012

Midterm Exam 5 April 20, 2015

Relational Query Languages. Preliminaries. Formal Relational Query Languages. Example Schema, with table contents. Relational Algebra

Applications. April 12, Indian Institute of Space Science and Technology. MA122 - Computer Programming and. Applications.

Review Questions for Final Exam

FINAL TERM EXAMINATION SPRING 2010 CS304- OBJECT ORIENTED PROGRAMMING

C++ basics Getting started with, and Data Types.

CS 101 Computer Programming and Utilization. Lecture 5, More Numerical computing (Slides courtesy Prof Ranade)

Chapter 3 Function Overloading

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

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:

6.096 Introduction to C++

A First Program - Greeting.cpp

Introduction to Programming EC-105. Lecture 2

Object Oriented Programming: Inheritance Polymorphism

Object-Oriented Analysis, Design and Implementation. Case Study Part II

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

University of Swaziland Department OfComputer Science Main Examination December 2016

UEE1302 (1102) F10: Introduction to Computers and Programming

W3101: Programming Languages C++ Ramana Isukapalli

CHAPTER 4 Structures

Relational Algebra. Note: Slides are posted on the class website, protected by a password written on the board

C++ Support Classes (Data and Variables)

END TERM EXAMINATION

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Exercises 1. class member { int membernum = 25; float memberpay; public void Input(cin >> membernum >> memberpay); void Output; }

Developed By Strawberry

Assumptions. History

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

CE221 Programming in C++ Part 1 Introduction

FRIEND FUNCTIONS IN CPP

CS 101: Computer Programming and Utilization

Transcription:

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Inheritance in C++ Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1

Overview of This Lecture Inheritance Motivation Compositional vs. inheritance-based approaches Hierarchy of classes 2 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Acknowledgment Much of this lecture is motivated by the treatment in An Introduction to Programming Through C++ by Abhiram G. Ranade McGraw Hill Education 2014 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 3

A Bank Account Example Hierarchy of Accounts Base Account Savings Current Deposit Minor Adult Non- Commercial Commercial Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 4

A Bank Account Example We want to define a hierarchy of classes for representing bank account information id balance name age ATM Base Account duration principal Savings Current Deposit amount overdraft Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 5

A Bank Account Example We want to define a hierarchy of classes for representing bank account information age ATM Base Account id balance name amount overdraft duration principal Savings Current Deposit Minor Adult Non- Commercial Commercial guardianname GovtIDNum workdomain Company registration Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 6

How to define these Classes? Compositional Way class base { int id; float balance; char name[]; class savings { base b; int age; long int ATM; int main() { savings s; s.age = 20; current c; class current { base b; int amount; int overdraft; } c.amount = 15000; return 0; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 7

How to define these Classes? Inheritance Way class base { int id; float balance; char name[]; class savings : public base { int age; long int ATM; int main() { savings s; s.age = 20; current c; class current : public base { int amount; int overdraft; } c.amount = 15000; return 0; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 8

Difference in Access Style Compositional Way int main() { savings s; s.age = 20; current c; Inheritance Way int main() { savings s; s.age = 20; current c; } c.amount = 15000; return 0; } c.amount = 15000; return 0; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 9

Complex Access Control Requirement: Members id & balance of class base to be accessible only from class savings, and not from other classes How do we achieve this? Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 10

Compositional: Private Member & Friend Class class 'savings' is a friend of base class base { private: int id; int balance; friend class savings; class savings { base b; void createaccount() { b.id = 1; b.balance = 0; } access due to friendship Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 11

Compositional: Private Member & Friend Class To extend this privilege to current class class 'savings' is a friend of base class base { private: int id; int balance; friend class savings; friend class current; class savings current{ { base b; void createaccount() { b.id = 1; b.balance = 0; } access due to friendship Can we do without inserting friend class declarations every time? Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 12

Towards Derived Classes Can we explicitly say that a class is derived from a base class and inherits attributes of base class with special privileges to access those attributes? Inheritance in C++ Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 13

How do we specify Inherited Classes We want to define a hierarchy of classes for representing bank account information age ATM Base Account id balance name amount overdraft duration principal Savings Current Deposit Minor Adult Non- Commercial Commercial guardianname GovtIDNum workdomain Company registration Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 14

Class Hierarchy base savings Derived Classes Super/Base Class class savings : public base { int age; long int ATM; class base { int id; float balance; char name[]; class current : public base { int amount; int overdraft; class deposit: public base { int duration; int principal; class minor: public savings{ char guardianname[]; class adult: public savings{ long int GovtIDNum class noncom: public current{ char workdomain[]; class com: public current{ char companyregistration[] Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 15

Complete Program A complete program which uses all public members and shows how to access members of base class from derived class #include<iostream> using namespace std; class base { int id; float balance; char name[]; class savings : public base { int age; long int ATM; class current : public base { int amount; int overdraft; int main() { savings s; s.id = 1; s.age = 20; cout << s.id << s.age; current c; c.id = 2; c.amount = 15000; cout << c.id << c.amount; return 0; } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 16

Summary Compositional vs. inheritance-based approaches Defining hierarchy of classes Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 17