1 CSC241: Object Oriented Programming Lecture No 21.

Slides:



Advertisements
Similar presentations
Complete Structure class Date {class Date { private :private : // private data and functions// private data and functions public :public : // public data.
Advertisements

CPS 235 Object Oriented Programming Paradigm
Operator overloading redefine the operations of operators
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
Starting Out with C++, 3 rd Edition 1 Chapter 14 – More About Classes.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;
4/14/2015Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23m2 OOP Friend Functions Ref: Sebesta, Chapter 12; Lafore, Chapter 11.
Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
Introduction to Programming Lecture 39. Copy Constructor.
EC-241 Object-Oriented Programming
Derived Classes. C++ 2 Outline  Definition  Virtual functions  Virtual base classes  Abstract classes. Pure virtual functions.
C++ Classes & Data Abstraction
1 Class Vehicle #include #define N 10../.. 2 Class Vehicle class vehicle { public: float speed; char colour[N+1]; char make[N+1];
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
1 CSC241: Object Oriented Programming Lecture No 28.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation - a language mechanism for restricting access to some.
CSC241: Object Oriented Programming
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
1 CSC241: Object Oriented Programming Lecture No 13.
CMSC 202 Lesson 19 Polymorphism 2. Warmup What is wrong with the following code? What error will it produce? (Hint: it already compiles) for (unsigned.
1 CSC241: Object Oriented Programming Lecture No 12.
$100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300.
1 CSC241: Object Oriented Programming Lecture No 06.
1 CSC241: Object Oriented Programming Lecture No 22.
1 CSC241: Object Oriented Programming Lecture No 16.
1 CSC241: Object Oriented Programming Lecture No 25.
1 CSC241: Object Oriented Programming Lecture No 02.
Review of Last Lecture. What we have learned last lecture? What does a constructor do? What is the way to define a constructor? Can it be defined under.
1 CSC241: Object Oriented Programming Lecture No 11.
Chapter -6 Polymorphism
1 CSC241: Object Oriented Programming Lecture No 05.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
1 CSC241: Object Oriented Programming Lecture No 03.
1 CSC241: Object Oriented Programming Lecture No 17.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
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.
1 CSC241: Object Oriented Programming Lecture No 08.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
CSC 143 O 1 CSC 143 Inheritance and Object Oriented Design.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Unified Modeling Language
CSC241: Object Oriented Programming
Overloaded Constructors and Multiple Classes
CSC241: Object Oriented Programming
7. Inheritance and Polymorphism
Object-Oriented Programming (OOP) Lecture No. 45
CSC241: Object Oriented Programming
CSC241: Object Oriented Programming
Class A { public : Int x; A()
CSC241: Object Oriented Programming
Polymorphism Lec
Static Data Member and Functions
Introduction to Programming
Object Oriented Programming Using C++
Introduction to Programming
Introduction of Programming
Inheritance: Polymorphism and Virtual Functions
Object-Oriented Programming (OOP) Lecture No. 22
Introduction to Programming
Function Overloading.
Polymorphism 2 CMSC 202.
CMSC 202 Lesson 19 Polymorphism 2.
Object-Oriented Programming (OOP) Lecture No. 23
Objects as Function Arguments
Presentation transcript:

1 CSC241: Object Oriented Programming Lecture No 21

2 Previous Lecture Polymorphism Example program – person class Virtual destructor – base class destructor is virtual, derived class destructor also become virtual Friend functions – can access private member of a class

3 Today’s Lecture Friend function – example program: Distance class – Friend function for functional notation Friend classes static functions

4 Distance Example class Distance { private: int feet; float inches; public: Distance() : feet(0), inches(0.0) { } Distance (float fltfeet) { feet = fltfeet; inches = 12*(fltfeet-feet); } Distance(int ft, float in) : feet(ft), inches(in) { } void showdist() const { cout << feet << “ : ” << inches ; } Distance operator + ( Distance ) const; }; Distance Distance::operator + ( Distance d2 ) const { int f = feet + d2.feet; float i = inches + d2.inches; if(i >= 12.0) { i -= 12.0; f++; } return Distance(f,i); } When such constructor exists, following statements are allowed d3 = 15.5; d3 = d ;

5 Cont. d3 = 10.5; – 10.5 is passed to one argument constructor, – 10.5 is converted into Distance object – d3 = nameless object of Distance; d3 = d ; – 9.75 is converted into Distance object by calling one argument constructor – Then operator + function is invoked by d1 object d3 = d1; d3 = Distance(6.5) + d1;

6 Note When a float value pass as argument to function – Definition: void function (Distance d1) { … } – Call : function (10.75); – One argument constructor is called Distance(float) – Convert float value i.e into Distance object – Call : function (Distance (10.75)) – function (Nameless Distance object) d3 = d1; – Neither float value invoke operator + function – Nor operator knows to convert float to Distance

7 Distance Example – Friend function class Distance {. friend Distance operator + ( Distance, Distance ) const; }; Distance Distance::operator + (Distance d1, Distance d2) const { int f = d1.feet + d2.feet; float i = d1.inches + d2.inches; if(i >= 12.0) i -= 12.0; f return Distance(f,i); } d3 = d1; friend function Overloaded () operator took two argument

8 friends for Functional Notation class Distance { private: int feet; float inches; public: Distance() : feet(0), inches(0.0) { } Distance(int ft, float in) : feet(ft), inches(in) { } void showdist() const { cout << feet << “ : ” << inches ; } float square(); }; float Distance::square() { float fltfeet = feet + inches/12; float feetsqrd = fltfeet * fltfeet; return feetsqrd; } main() { Distance dist(3, 6.0); float sqft; sqft = dist.square(); cout << “\nDistance = “; dist.showdist(); cout << “\nSquare = “ << sqft; } sqft = square(dist);

9 Cont. class Distance { private: int feet; float inches; public: Distance() : feet(0), inches(0.0) { } Distance(int ft, float in) : feet(ft), inches(in) { } void showdist() const { cout << feet << “:” << inches; } friend float square(Distance); }; float square(Distance d) { float fltfeet; fltfeet = d.feet + d.inches/12; float feetsqrd = fltfeet * fltfeet; return feetsqrd; } In general, the friend version of a function requires one more argument than when the function is a member. Go to program

10 Friend classes Same as a function can be friend of a class – Friend function can access private data member A class can be friend of another class – All functions of friend class become friend function of that class

11 Cont. class alpha { private: int data1; public: alpha() : data1(99) { } friend class beta; }; main() { alpha a; beta b; b.func1(a); b.func2(a); cout << endl; } class beta { public: void func1(alpha a) { cout << “\ndata1=” << a.data1; } void func2(alpha a) { cout << “\ndata1=” << a.data1; } }; In class alpha the entire class beta is proclaimed a friend. Now all the member functions of beta can access the private data of alpha

12 Static function A static data member is not duplicated for each object; rather a single data item is shared by all objects of a class Example: a class that keep track of how many objects of itself have created Function can also be static A static function can only access static data member of class

13 Example program class gamma { private: static int total; int id; public: gamma() { total++; id = total; } ~gamma() { total--; cout << “Destroying ID “ << id << endl; } static void showtotal() { cout << “Total is “ << total; } void showid() { cout << “ID number is “ << id; } }; int gamma::total = 0;

14 Static vs. non static functions Static function can only access static data member of class – className::static_function(); – Object_of_class.static function(); Non static member function can access static and non static data member of class – Object_of_class.Non_static_function(); Go to program

15 Assignment and Copy Initialization C++ compiler do many things on programmer behalf. For example: – Assignment operator – Copy constructor Assignment a2 = a1; Copy constructor alpha a2(a1); cause the compiler to copy the data from a1, member by member, into a2. compiler creates an object, a2, copies the data from a1, member by member, into a2

16 Cont. Both of these default activities are provided, free of charge, by the compiler If member-by member copying is required, then default activities are enough If assignment or initialization has to do something more complex, then they must be override First, separate example to override default assignment and copy constructor String class in more efficient way by overriding these two activities

17 Overloading the Assignment Operator class alpha { private: int data; public: alpha() { } alpha(int d) { data = d; } void display() { cout << data; } alpha operator = (alpha& a){ data = a.data; cout << “Assignment operator invoked”; return alpha(data); } }; main() { alpha a1(37); alpha a2; a2 = a1; cout << “\na2=”; a2.display(); alpha a3 = a2; cout << “\na3=”; a3.display(); } Program Output Assignment operator invoked a2=37 a3=37 Go to program

18 Note Initialization Is Not Assignment – alpha a3 = a2; // copy initialization is not an assignment but an initialization, with the same effect as – alpha a3(a2); Passing by Reference – Save memory space – Avoid creation of unwanted objects Returning a Value -Cannot do return by reference

19 Assignment operator – inheritance The assignment operator is unique among operators in that it is not inherited If you overload the assignment operator in a base class, you can’t use this same function in any derived classes

20 The Copy Constructor An object can be define and initialize with values of another object of same class – alpha a3(a2); // copy initialization – alpha a3 = a2; // copy initialization, alternate syntax Both invoke a copy constructor: – That creates a new object and copies its argument into it Default copy construct perform member by member copy Distance d2 = d1; feet inches d1 feet inches d

21 Cont. It is similar to what assignment operator (d2 = d1) does The difference is that copy initialization also creates a new object

22 Example program – assignment and copy constructor class alpha { private: int data; public: alpha() { } alpha(int d) { data = d; } alpha(alpha& a) { data = a.data; cout << “\nCopy constructor invoked”; } void display() { cout << data; } void operator = (alpha& a) { data = a.data; cout << “\nAssignment operator invoked”; } }; a2 = a1; alpha a3(a1); Program Output Assignment operator invoked Copy constructor invoked

23