Friend Functions, Inheritance

Similar documents
Inheritance, and Polymorphism.

Inheritance

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

CS OBJECT ORIENTED PROGRAMMING

Midterm Exam 5 April 20, 2015

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

OOP. Unit:3.3 Inheritance

C++ Quick Guide. Advertisements

Computer Programming Inheritance 10 th Lecture

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

Inheritance (Deitel chapter 9)

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Polymorphism Part 1 1

CHAPTER 9 INHERITANCE. 9.1 Introduction

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

Friends and Overloaded Operators

Friends and Overloaded Operators

Chapter 1: Object-Oriented Programming Using C++

UNIT 1 OVERVIEW LECTURE NOTES

C++ Important Questions with Answers

Polymorphism CSCI 201 Principles of Software Development

Lecture 5: Inheritance

SSE2034: System Software Experiment 3 Spring 2016

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

Implementing an ADT with a Class

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

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

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

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

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

Chapter 13. Object Oriented Programming

Programming in C++: Assignment Week 6

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

System Design and Programming II

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Assumptions. History

CS304 Lecture Wise Questions by Virtualians Social

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

OBJ. ORI.& MULT. PROG., M.C.Q. BANK, FOR UNIT -2, SECOND YEAR COMP. ENGG. SEM-4, 2012 PATTERN, U.O.P. UNIT-2

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Lecture-5. Miscellaneous topics Templates. W3101: Programming Languages C++ Ramana Isukapalli

ECE 462 Midterm Exam 1. 10:30-11:20AM, September 21, 2007

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

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

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

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:

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

DELHI PUBLIC SCHOOL TAPI

CS2141 Software Development using C/C++ C++ Basics

ECE 3574: Dynamic Polymorphism using Inheritance

Introduction Of Classes ( OOPS )

EL2310 Scientific Programming

W3101: Programming Languages C++ Ramana Isukapalli

What does it mean by information hiding? What are the advantages of it? {5 Marks}

Chapter 9 - Object-Oriented Programming: Inheritance

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Data Structures (INE2011)

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

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

Java Object Oriented Design. CSC207 Fall 2014

Function Overloading

Introduction to Object-Oriented Programming with C++

CS11 Introduction to C++ Fall Lecture 7

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

EL2310 Scientific Programming

What is Class? Remember

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

IUE Faculty of Engineering and Computer Sciences Spring Semester

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

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

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

by Pearson Education, Inc. All Rights Reserved. 2

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

C++ 8. Constructors and Destructors

The Address-of Operator &: The & operator can find address occupied by a variable. If var is a variable then, &vargives the address of that variable.

Preview from Notesale.co.uk Page 3 of 36

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

Chapter 15: Inheritance, Polymorphism, and Virtual Functions

ECE 462 Object-Oriented Programming using C++ and Java Design Issues and Multiple Inheritance in C++

POLYMORPHISM. Phone : (02668) , URL :

Inheritance and Polymorphism

The Notion of a Class and Some Other Key Ideas (contd.) Questions:


Arrays Classes & Methods, Inheritance

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14)

Class (Inheritance) SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

ITI Introduction to Computing II

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

OBJECT ORIENTED PROGRAMMING USING C++

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

Chapter 6 Introduction to Defining Classes

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Outline 2017/03/17. (sections from

Object Oriented Software Design II

Distributed Systems Recitation 1. Tamim Jabban

Defining Classes and Methods

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

Transcription:

Friend Functions, Inheritance

Friend Function Private data member of a class can not be accessed by an object of another class Similarly protected data member function of a class can not be accessed by any other class except its derived classes The purpose of the friend function is to bring the objects of different classes on same platform and access its private and protected data members. 2

Friend Function Following points must be noted for the use of a friend function Friend function must be defined outside of all classes for which friend function is declared or not The prototype of friend function must be define in each class within scope of any access-specifier The reserve word friend must be only preceded by the prototype of friend function not the definition part of friend function The arguments of friend function may be of class type Friend function can return data of any primitive type but sometime you can use void 3

Friend Function Use of friend function can enhance performance. 4

Example #include "stdafx.h" #include "iostream" class number2; class number1 { private: int x; public: number1(){ x=5; } friend int sum(number1,number2); }; 5

class number2 { private: int y; public: number2(){ y=20; } friend int sum(number1,number2); }; 6

int sum(number1 ob1,number2 ob2) { return ob1.x+ob2.y; } int _tmain(int argc, _TCHAR* argv[]) { number1 obj1; number2 obj2; std::cout<<"sum of private data member "<<sum(obj1,obj2)<<std::endl; system("pause"); return 0; } 7

Example 2 #include "stdafx.h" #include <iostream> using namespace std; class count { private: int x; public: count() : x( 0 ) { } void print() const { cout << x << endl; } friend void setx(count &c, int val); }; 8

Example 2 void setx(count &c, int val) { c.x=val; } int _tmain(int argc, _TCHAR* argv[]) { count obj; cout<<"counter.x after instantiation: "; obj.print(); setx(obj, 8); cout<<"counter.x after call to setx friend function: "; obj.print(); system("pause"); return 0; } 9

Inheritance Basics

What is Inheritance? Inheritance allows a new class to be based on an existing class. The new class inherits all the member variables and functions of the class it is based on. The mechanism by which one class acquires the properties of another class 11

Inheritance This existing class is called the base class, and the new class is called the derived class. Other programming languages, such as Java and C#, refer to the base class as the superclass and the derived class as the subclass. A derived class represents a more specialized group of objects. 12

Relationship between classes In object oriented programming there are two types of relationships between classes which are commonly used Is-a relationship: The is-a relationship represents inheritance. In an is-a relationship, an object of a derived class also can be treated as an object of its base class e.g, a Car is a Vehicle, so any attributes and behaviors of a Vehicle are also attributes and behaviors of a Car Has-a relationship: In a has-a relationship, an object contains one or more objects of other classes as members. e.g, a Car has many components it has a steering wheel, has a brake pedal, has a transmission, etc 13

Arrange concepts into an inheritance hierarchy Concepts at higher levels are more general Concepts at lower levels are more specific (inherit properties of concepts at higher levels) Vehicle Wheeled vehicle Boat Car Bicycle 2-door 4-door

Protected Members and Use in Derived Class Protected members of a base class are like private members, but they may be accessed by derived classes. The base class access specification determines how private, protected, and public base class members may be accessed by derived classes. 15

Inheritance Subgroupings with respect to a parent are called Subclass Derived Class Children The derived class inherits from the parent all characteristics properties capabilities can modify or extend inherited abilities

Inheritance Study the vehicle hierarchy: VEHICLE AUTOMOBILE BICYCLE LANDBASED Note that each entity is also a class Classes are used in an object centered paradigm as a means of encapsulating common data and functions shared by members of the class

Types of Inheritance according to Number of base classes Single Inheritance Each class or instance object has a single parent Multiple Inheritance Classes inherit from multiple base classes ( might not have same ancestors as shown in the example below) Defines a relationship Between several (independent) class types Vehicle Example: Multiple Parents Common Ancestor LandBased Automobile WaterBased MotorBoat AutoBoat

Types of Inheritance According to Accessibility A class(derived Class) can be derived from another class(base Class) by using access specifiers Private, Protected and Public with the name of base class. On the bases of using these access specifiers inheritance is divided into three types Private Inheritance Protected Inheritance Public Inheritance 19

Private inheritance In this type of inheritance access specifiers private is used with the name of base class during declaration of derived class e.g. class child : private parent 20

Protected inheritance In this type of inheritance access specifiers protected is used with the name of base class during declaration of derived class e.g. Class child : protected parent 21

Public inheritance Public inheritance using public access specifier with the name of the base class during the declaration of derived classes. class child : public parent A base class s private members are accessible only within its body and to the friends of that base class. In this section, we introduce the access specifier protected. Using protected access offers an intermediate level of protection between public and private access. A base class s protected members can be accessed within the body of that base class, by members and friends of that base class, and by members and friends of any classes derived from that base class 22

Inherited Member Initialization Initialized in two ways: If the base class has only a default constructor=> initialize the member values in the body of the derived class constructor If the base class has a constructor with arguments=> the initialization list is used to pass arguments to the base class constructors syntax (for Single Base Class) DerivedClass ( derivedclass args ) : BaseClass ( baseclass args ) { DerivedClass constructor body } The set of derived class constructor arguments may contain initialization values for the base class arguments

e.g.1 #include<iostream.h> #include<conio.h> using namespace std; class A { int data; public: void f(int arg) { data = arg; } int g() { return data; } }; 24

class B : public A { }; void main() { B obj; obj.f(20); cout << obj.g() << endl; getch() } 25

e.g; 2 #include <iostream.h> 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; } }; 26

(Cont) // Derived class class Rectangle: public Shape { public: int getarea() { return (width * height); } }; void main(void) { Rectangle Rect; Rect.setWidth(5); Rect.setHeight(7); // Print the area of the object cout << "Total area: " << Rect.getArea() << endl; return 0; } 27

Assignment #3 Q1. Write a program to implement the inheritance process of following defined classes. A base class named calculate which have data members a, b, c, d, e and %age using a member function. Find the percentage of the five subjects marks. Q2. write a program to implement the inheritance process of following defined classes A base class named as shape A derived class named triangle. Data members and member functions must not b defined in both classes. Find the area of triangle by using the derived classes. Submission Date: 11/2/2014. 28