Programming 2. Object Oriented Programming. Daniel POP

Similar documents
Programming 2. Object Oriented Programming. Daniel POP

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Programming 2. Object Oriented Programming. Daniel POP

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

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

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

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Programming in C and C++

Example : class Student { int rollno; float marks; public: student( ) //Constructor { rollno=0; marks=0.0; } //other public members };

Instantiation of Template class

CS304 Object Oriented Programming Final Term

Fast Introduction to Object Oriented Programming and C++

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

Constructor - example

Handout 7. Defining Classes part 1. Instance variables and instance methods.

Object Oriented Software Design II


Short Notes of CS201

C++ C and C++ C++ fundamental types. C++ enumeration. To quote Bjarne Stroustrup: 5. Overloading Namespaces Classes

Introduction to C++ with content from

CS201 - Introduction to Programming Glossary By

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

Object Oriented Programming(OOP).

Interview Questions of C++

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

An Introduction to C++

Operator Dot Wording

Object Oriented Design

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

C How to Program, 6/e, 7/e

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

C++ Programming Fundamentals

Object-Oriented Programming

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

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

public : int min, hour ; T( ) //here constructor is defined inside the class definition, as line function. { sec = min = hour = 0 ; }

Object-Oriented Programming, Iouliia Skliarova

Data Structures using OOP C++ Lecture 3

C++ 8. Constructors and Destructors

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

G52CPP C++ Programming Lecture 13

CS201 Latest Solved MCQs

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI

6.096 Introduction to C++ January (IAP) 2009

Part VII. Object-Oriented Programming. Philip Blakely (LSC) C++ Introduction 194 / 370

Ch. 12: Operator Overloading

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

C++ (Non for C Programmer) (BT307) 40 Hours

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Pointers, Dynamic Data, and Reference Types

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

2 ADT Programming User-defined abstract data types

VALLIAMMAI ENGINEERING COLLEGE

Object Oriented Programming. Solved MCQs - Part 2

AIMS Embedded Systems Programming MT 2017

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Template Issue Resolutions from the Stockholm Meeting

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

pointers & references

CS201 Some Important Definitions

C++ Important Questions with Answers

CE221 Programming in C++ Part 1 Introduction

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

COEN244: Class & function templates

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 12: Classes and Data Abstraction

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

Cpt S 122 Data Structures. Introduction to C++ Part II

CS201- Introduction to Programming Current Quizzes

Tokens, Expressions and Control Structures

Vector and Free Store (Vectors and Arrays)

G52CPP C++ Programming Lecture 20

CS 376b Computer Vision

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Chapter 1 INTRODUCTION SYS-ED/ COMPUTER EDUCATION TECHNIQUES, INC.

CSC1322 Object-Oriented Programming Concepts


Introduction to Programming Using Java (98-388)

Advanced Systems Programming

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

C++: Const Function Overloading Constructors and Destructors Enumerations Assertions

Classes, objects. Static, const, mutable members of a class

QUIZ Friends class Y;

OBJECT ORIENTED PROGRAMMING

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Object Oriented Software Design II

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

G52CPP C++ Programming Lecture 7. Dr Jason Atkin


Classes: Member functions // classes example #include <iostream> using namespace std; Objects : Reminder. Member functions: Methods.

Object-Oriented Programming, Iouliia Skliarova

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Evolution of Programming Languages

Object Oriented Software Design II

Transcription:

Programming 2 Object Oriented Programming Daniel POP

Week 5

Agenda 1. Modifiers: friend 2. Objects

Wrap-up last week Self-reference Modifiers: static const mutable Object Oriented Programming

Friends (I) class Matrix { /* declarations */ ; class Vector { /* declarations */ ; Define a function that: Vector x Matrix Vector Rationale: functions need access to private members of one class Solution: make those functions members of the class What happens if one function need to have access to private members of 2 or more classes? It can be member of only one class :-( Friends help us to do this :-)

Friends (II) DEFINITION [Friend functions] Friend functions are functions that are not members of one class, yet they have access to private members (data + functions) declared in that class. DEFINITION [Friend class] If class X is friend class of class Y, then all member functions of class X are friend functions (i.e. have access to private members) of class Y. It s not relevant the access control modifier (private, public, protected) used to declare a friend function/class, because friend functions are not member of the class for which they are friend. Friends can be either functions that are member of another class, or global functions. Syntax friend ftype fname([arg_list]); friend class X;

Friends (III) class Matrix; // Forward declaration class Vector { float v[4]; public: Vector(float v0=0, float v1=0, float v2=0, float v3=0); friend Vector multiply(const Vector&, const Matrix&); ; class Matrix { Vector rows[4]; friend Vector multiply(const Vector&, const Matrix&); ; Vector multiply(const Vector& v, const Matrix& m) { v[0] = 0; // OK or ERROR? rows[0][0] = 0; // OK or ERROR void main() { Matrix m; Vector v(1.0, 2.5, 5.0, 6.0); Vector w = multiply(v, m); // w = v x m

Friends (IV) One of the key principle OO is data hiding (encapsulation), but sometimes is to restrictive and needs to be broken =====> Not recommendable to use friends; use only if it s impossible to solve the problem otherwise.

Friends (V) More examples: see operator overloading Finding friends: declared before or identifiable by argument type Friendship is NOT reflexive nor transitive Friendship is not inherited Action Non-static member function Static member function Friend function Has access to private members x x x Is declared in the class scope x x (transparently) Receives this pointer x

Member or Friend? Some operations can be performed only by members: constructor, destructor, virtual functions A function that has to access the members of a class should be a member unless there s a specific reason for it not to be a member. Prefer member functions over friends because of clearer syntax. Member functions must be invoked for objects of their class only; no user defined conversions are applied. For example, if transpose function transposes its calling object instead of creating a new transposed matrix then it should be a member of class. Example: (see also operator overloading for more examples) class X { public: X(int); void m1(); friend int f1(x&); friend int f2(const X&); friend int f3(x); ; void f() { 7.m1(); // err: X(7).m1() is not tried! f1(7); // err: f1(x(7)); is not tried because no implicit conversion is used for non-const references f2(7); // ok: f3(x(7)); f3(7); // ok: f3(x(7)); Nota: Solution for m1() - declare it as global function, friend of X

Exercise GAME OF SOCCER: Add details to classes that model Soccer game.

Objects DEFINITION [Object] An object is an instance of a class. It can be uniquely identified by its name, it defines a state which is represented by the values of its attributes at a particular time and it exposes a behaviour defined by the set of functions (methods) that can be applied to it. The simplest way to create an object in C++ is by variable declaration. If a variable is an instance of a class/struct then it is also called object. int main(int, char*[]) { Date day1; // <- 1 st object Date day2(25, 12); // <- 2 nd object Date *today = new Date(15, 03, 2004); // <- 3 rd object cout << Today is " << today->getday() << / << today->getmonth() << / << today->getyear(); delete today; return 0;

Object Instantiation - Summary # Type When is constructed When is destroyed 1 Local variable Each time its declaration is encountered in the execution of the program Each time the program exits the block in which it occurs 2 Dynamic allocation (Free-store) Using new operator Using delete operator 3 Local static variable First time its declaration is encountered in the execution of the program Termination of the program 4 Array of objects When array is created When array is destroyed 5 Non-local object Once at the start of the program Once at the termination of the program 6 Temporary object When the expression is evaluated At the end of the full expression in which it occurs 7 Dynamic allocation in user-supplied memory Using an overloaded version of operator new Using delete operator 8 Union member 9 Non-static member object When the enclosing object is created When the enclosing object is destroyed

Local variable int f(int a) { Date d1, d2(03, 03, 2007); // creating the objects if(a>0) { Date d3=d1; // creating object using copy-constructor // d3 is destroyed Date d4; // destroying objects d4, d2, d1 (in this order) Objects are destroyed in reverse order of their creation.

Free store (Dynamic allocation) int f(int a) { Date *d1 = new Date(03, 03, 2007); // creating the object if(a>0) { Date *d2 = new Date; cout << *d2; delete d2; delete d1; Operator new allocates and initializes memory from free store Operator delete de-allocates and cleanup memory DO NOT FORGET TO DESTROY ALL OBJECTS ALLOCATED USING NEW OPERATOR! Syntax X* p = new X; X* p = new X(init_value); delete p;

Local static variable int f(int a) { static Date d(03, 03, 2007); // static object // d is not destroyed! The destructors for static objects are called in reverse order when the program terminates.

Array of objects int f(int a) { Date week[7]; // an array of 7 Date objects Date *year = new Date[365]; Syntax X array[size]; X* p = new X[size]; delete [] year; delete [] p; A default constructor for Date type is mandatory. There is no way to specify explicit arguments for a constructor in an array declaration. The destructor for each element of an array is invoked when the array is destroyed. Don t forget the squared brackets in delete to free all the elements (delete [] array)

Non-local variable Date gdate1; int f(int a) { cout << gdate1; class X { static Date referencedate; ; Date X::referenceDate(1,1,1970); Date gdate2(1,1,1970); Non-local = global, namespace or class static variables Any global object (declared outside any function) is constructed before function main is invoked, in the order of their definitions. The destructors for static objects are called in reverse order when the program terminates. No implementation-independent guarantees are made about the order of construction/destruction of non-local objects in different compilation units (files).

Temporary object class String { char *s; public: char* c_str(); ; // TODO: add the rest // of required members void f(string& s1, String& s2) { //... cout << s1 + s2; //... String temp = s1 + s2; cout << temp; destroy temp; - Temporary objects are results of expression evaluation (e.g. arithmetic operations) - A temporary object can be used as initializer for a const reference or named object: const string& s = s1+s2; string s = s1+s2; - A temporary object can also be created by explicitly invoke the constructor handlecomplex(complex(1,2)); - Problems may arise. See below. void f(string& s1, String& s2) { // c_str() returns a C-style, zero-terminated array of chars const char* pch = (s1+s2).c_str(); cout << pch; // pch points to an invalid memory address that was destroyed together with // the temporary obj. s1+s2

User-supplied memory void* operator new(size_t, void* p) { return p; // memory area // Explicit placement operator void* buf = reinterpret_cast<void*>(0xf00f); // nasty, nasty! // placement syntax X* p2 = new (buf) X; // invokes operator new(sizeof(x), buf); class PersistentStorage { virtual void* alloc(size_t) = 0; virtual void free(void*)=0; ; void* operator new(size_t s, PersistentStorage* p) { return p->alloc(s); void destroy(x* p, PersistentStorage* storage) { p->~x(); // explicit call of destructor storage->free(p); // release the memory PersistentStorage* ps = new FileStorage( a.bin ); void f() { X* p1 = new (ps) X; X* p2 = new (ps) X[10]; X* p3 = new (ps) X(3); destroy(p1, ps); destroy(p1, ps); destroy(p1, ps); Should be used with caution! Ask experienced colleagues first.

Union members class X { ; union AUnion { int x; char name[12]; X obj; // OK: No constructors or destructor defined for type X Date date; // ERROR: Date has constructors and destructor ; void f(); A union can have member functions. A union can t have static members. A union can t have members with custom constructors or destructor.

Non-static members (I) class Student { Date dob; // non-static member String name; // non-static member public: Student(const char* n, const Date& d); ; Student::Student(const char* n, const Date& d) { dob = d; name = String(n); void f() { Date d(7, 8, 1988); Student astudent( Popescu, d); Steps to initialize astudent object: (1) Memory allocation = sizeof(date) + sizeof(string) (2) Call default constructor for Date to initialize dob member. (3) Call default constructor for String to initialize name member. (4) Call the Student::Student(n,d) constructor. (5) Call assignment operator in line dob=d; (6) Call assignment operator in line name=n; The real initialization of dob and name objects is done in 2 steps: a default constructor + an assignment operator (steps 2+5 and 3+6).

Non-static members (II) class Student { Date dob; // non-static member String name; // non-static member public: Student(const char* n, const Date& d); ; Student::Student(const char* n, const Date& d) : name(n), dob(d) { void f() { Date d(7, 8, 1988); Student astudent( Popescu, d); Steps to initialize astudent object: (1) Memory allocation = sizeof(date) + sizeof(string) (2) Call the copy constructor of Date to initialize dob in dob(d) (3) Call the custom constructor of String to initialize name in name(n) (4) Call Student::Student(n,d) The real initialization of dob/name objects is done in one step: calling the appropriate constructor.

Non-static members (III) Order of initialization: 1. Initialize members in order of their declaration in class (the order in initialization list is irrelevant). 2. Call the constructor of the class. Order of destroy: 1. Call the destructor of the class. 2. Call the members destructor in reverse order of declaration. The following members can be initialized only in initialization list: References (X& member;) const (const int member;) Types without default constructor (X member;) static const integer members (and only these) can be initialized at declaration. class Club { Club(); // private default constructor public: Club(const char* s); ; class X { // declaration + initialization static const int ci = 1; // error: not int! static const float cf = 9.0; const int i; Date& date; Club club; public: X(int ii, Date& dd, const char* clubname) : i(ii), date(dd), club(clubname) { i = ii; // error const member ; const int X::ci; // definition

Exercise GAME OF SOCCER: Create the two teams, the field and the referees.

Further Reading 1. [Stroustrup, 1997] Bjarne Stroustrup The C++ Programming Language 3rd Edition, Addison Wesley, 1997 [Chapter 10, 11.5] Object Oriented Programming