Object oriented programming with C++

Similar documents
Object Oriented Programming. A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions.

Introduction Of Classes ( OOPS )

Object oriented programming C++

Object oriented programming C++

And Even More and More C++ Fundamentals of Computer Science

G52CPP C++ Programming Lecture 13

2 nd Week Lecture Notes

18. Polymorphism. Object Oriented Programming: Pointers to base class // pointers to base class #include <iostream> using namespace std;

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

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES IV

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

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

Thus, to declare billy as shown above it would be something as simple as the following sentence:

ROOT Course. Vincenzo Vitale, Dip. Fisica and INFN Roma 2

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

Next week s homework. Classes: Member functions. Member functions: Methods. Objects : Reminder. Objects : Reminder 3/6/2017

9. Arrays. Compound Data Types: type name [elements]; int billy [5];

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

Tokens, Expressions and Control Structures

11. Arrays. For example, an array containing 5 integer values of type int called foo could be represented as:

A A B U n i v e r s i t y

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

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula

M.EC201 Programming language

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Fast Introduction to Object Oriented Programming and C++

M.CS201 Programming language

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Variables. Data Types.

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

6.096 Introduction to C++ January (IAP) 2009

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name:

Object Oriented Programming. Solved MCQs - Part 2

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

Classes and Objects. Class scope: - private members are only accessible by the class methods.

CS304 Object Oriented Programming Final Term

Object Oriented Design

CS201 Some Important Definitions

Lecture 18 Tao Wang 1

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in:

Short Notes of CS201

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

COMP 2355 Introduction to Systems Programming

Reference operator (&)

8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );

PROGRAMMING LANGUAGE 2

CS201 - Introduction to Programming Glossary By

More C++ : Vectors, Classes, Inheritance, Templates

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

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

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

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

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

A brief introduction to C++

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

Object-Oriented Programming

Tentative Teaching Plan Department of Software Engineering Name of Teacher Dr. Naeem Ahmed Mahoto Course Name Computer Programming

Object Oriented Programming(OOP).

Unit IV Contents. ECS-039 Object Oriented Systems & C++

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Pointers. Reference operator (&) ted = &andy;

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

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

C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13

Review. What is const member data? By what mechanism is const enforced? How do we initialize it? How do we initialize it?

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

Introduction to C++ with content from

Abstraction in Software Development

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science

Question: How can we compare two objects of a given class to see if they are the same? Is it legal to do: Rectangle r(0,0,3,3);,, Rectangle s(0,0,4,4)

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

Programming 2. Object Oriented Programming. Daniel POP

OOP THROUGH C++(R16) int *x; float *f; char *c;

Introduction to C++ Systems Programming

Introduction to Programming Using Java (98-388)

AN OVERVIEW OF C++ 1

Instantiation of Template class

Implementing Abstract Data Types (ADT) using Classes


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

Inheritance, and Polymorphism.

Object-Oriented Programming

Problem Solving with C++

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Intermediate Programming & Design (C++) Classes in C++

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

Object-Oriented Programming, Iouliia Skliarova

Chapter 10 Introduction to Classes

ADTs & Classes. An introduction

Functions and Recursion

Classes Ch

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

aggregate data types member functions cis15 advanced programming techniques, using c++ lecture # II.1

CMSC202 Computer Science II for Majors

I BSc(IT) [ Batch] Semester II Core: Object Oriented Programming With C plus plus - 212A Multiple Choice Questions.

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

Lesson Plan. Subject: OBJECT ORIENTED PROGRAMMING USING C++ :15 weeks (From January, 2018 to April,2018)

Constructors and Destructors. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Transcription:

http://uranchimeg.com/ Object oriented programming with C++ T.Uranchimeg Prof. Dr. Email uranchimeg@must.edu.mn Power Engineering School M.EC203* -- OOP (C++) -- Lecture 10

Subjects Overloading operators The keyword this Static members Relationships between classes M.EC203* -- OOP (C++) -- Lecture 10 2

Overloading operators C++ incorporates the option to use language standard operators between classes in addition to between fundamental types. For example: int a, b, c; a = b + c; is perfectly valid, since the different variables of the addition are all fundamental types. M.EC203* -- OOP (C++) -- Lecture 10 3

Some moment Nevertheless, is not so obvious that we can perform the following operation (in fact it is incorrect): struct { char product [50]; float price; } a, b, c; a = b + c; The assignation of a class (or struct) to another one of the same type is allowed (default copy constructor). What would produce an error would be the addition operation, that in principle is not valid between non-fundamental types. M.EC203* -- OOP (C++) -- Lecture 10 4

C++ benefits But thanks to the C++ ability to overload operators, we can get to do that. Objects derived from composed types such as the previous one can accept operators which would not be accepted otherwise, and we can even modify the effect of operators that they already admit. M.EC203* -- OOP (C++) -- Lecture 10 5

list of all the operators that can be overloaded + - * / = < > += -= *= /= << >><<= >>= ==!= <= >= ++ -- % & ^! ~ &= ^= = && %= [] () new delete M.EC203* -- OOP (C++) -- Lecture 10 6

Prototype of overloading operators To overload an operator we only need to write a class member function whose name is operator followed by the operator sign that we want to overload, following this prototype: type operator sign (parameters); M.EC203* -- OOP (C++) -- Lecture 10 7

An example the operator + We are going to sum the bidimensional vectors a(3,1) and b(1,2). The addition of two bidimensional vectors is an operation as simple as adding the two x coordinates to obtain the resulting x coordinate and adding the two y coordinates to obtain the resulting y. In this case the result will be (3+1,1+2) = (4,3). M.EC203* -- OOP (C++) -- Lecture 10 8

The example 10-1 (1) M.EC203* -- OOP (C++) -- Lecture 10 9

The example 10-1 (2) M.EC203* -- OOP (C++) -- Lecture 10 10

The example 10-1 (3) 4,3 M.EC203* -- OOP (C++) -- Lecture 10 11

Explian If you are baffled seeing CVector so many times, consider that some of them make reference to the class name CVector and others are functions with that name. Do not confuse them: CVector (int, int); // function name //CVector (constructor) CVector operator+ (CVector); // function //operator+ that returns CVector type M.EC203* -- OOP (C++) -- Lecture 10 12

overloading the arithmetic operator + The function operator+ of class CVector is the one that is in charge of overloading the arithmetic operator +. This one can be called by any of these two ways: c = a + b; c = a.operator+ (b); M.EC203* -- OOP (C++) -- Lecture 10 13

the assignation operator (=) between two classes As well as a class includes by deafult an empty and a copy constructor, it also includes a default definition for the assignation operator (=) between two classes of the same type. This copies the whole content of the non-static data members of the parameter object (the one at the right side of the sign) to the one at the left side. Of course, you can redefine it to any other functionality that you want for this operator, like for example, copy only certain class members. M.EC203* -- OOP (C++) -- Lecture 10 14

how the different operator functions must be declared M.EC203* -- OOP (C++) -- Lecture 10 15

To prev. Table * where a is an object of class A, b is an object of class B and c is an object of class C. M.EC203* -- OOP (C++) -- Lecture 10 16

Two ways You can see in this panel that there are two ways to overload some class operators: as member function and as global function. Its use is indistinct, nevertheless I remind you that functions that are not members of a class cannot access the private or protected members of the class unless the global function is friend of the class M.EC203* -- OOP (C++) -- Lecture 10 17

The keyword this The keyword this represents within a class the address in memory of the object of that class that is being executed. It is a pointer whose value is always the address of the object. It can be used to check if a parameter passed to a member function of an object is the object itself. M.EC203* -- OOP (C++) -- Lecture 10 18

The Program 10-2 (1) M.EC203* -- OOP (C++) -- Lecture 10 19

The Program 10-2 (2) M.EC203* -- OOP (C++) -- Lecture 10 20

operator= It is also frequenty used in operator= member functions that return objects by reference. Following with the vector's examples seen before we could have written an operator= function like this: CVector& CVector::operator= (const CVector& param) { } x=param.x; y=param.y; return *this; M.EC203* -- OOP (C++) -- Lecture 10 21

Static members A class can contain static members, either data or functions. Static data members of a class are also known as "class variables", because their content does not depend on any object. There is only one unique value for all the objects of that same class. M.EC203* -- OOP (C++) -- Lecture 10 22

The example For example, it may be used for a variable within a class that can contain the number of objects of that class that have been declared, as in the following example: M.EC203* -- OOP (C++) -- Lecture 10 23

The Program 10-3 (1) M.EC203* -- OOP (C++) -- Lecture 10 24

The Program 10-3 (2) M.EC203* -- OOP (C++) -- Lecture 10 25

The explian In fact, static members have the same properties as global variables but they enjoy class scope. For that reason, and to avoid that they may be declared several times, according to ANSI-C++ standard, we can only include the protype (declaration) in the class declaration but not the definition (initialization). In order to initialize a static data-member we must include a formal definition outside the class, in the global scope, as in the previous example. M.EC203* -- OOP (C++) -- Lecture 10 26

unique variable Because it is a unique variable for all the objects of the same class, it can be referred to as a member of any object of that class or even directly by the class name (of course this is only valid for static members): cout << a.n; cout << CDummy::n; These two calls included in the previous example are referring to the same variable: the static variable n within class CDummy. M.EC203* -- OOP (C++) -- Lecture 10 27

Additional Just as we may include static data within a class, we can also include static functions. They represent the same: they are global functions that are called as if they were object members of a given class. They can only refer to static data, in no case to nonstatic members of the class, as well as they do not allow the use of the keyword this, since it makes reference to an object pointer and these functions in fact are not members of any object but direct members of the class. M.EC203* -- OOP (C++) -- Lecture 10 28

Relationships between classes Friend functions (friend keyword) In the previous section we have seen that there were three levels of internal protection for the different members of a class: public, protected and private. In the case of members protected and private, these could not be accessed from outside the same class at which they are declared. M.EC203* -- OOP (C++) -- Lecture 10 29

the friend keyword Nevertheless, this rule can be transgressed with the use of the friend keyword in a class, so we can allow an external function to gain access to the protected and private members of a class. In order to allow an external function to have access to the private and protected members of a class we have to declare the prototye of the external function that will gain access preceded by the keyword friend within the class declaration that shares its members. M.EC203* -- OOP (C++) -- Lecture 10 30

The Program 10-4 (1) M.EC203* -- OOP (C++) -- Lecture 10 31

The Program 10-4 (2) M.EC203* -- OOP (C++) -- Lecture 10 32

The Program 10-4 (3) M.EC203* -- OOP (C++) -- Lecture 10 33

Explain to 10-4 From within the duplicate function, that is a friend of CRectangle, we have been able to access the members width and height of different objects of type CRectangle. Notice that neither in the declaration of duplicate() nor in its later use in main() have we considered duplicate as a member of class CRectangle. It isn't. M.EC203* -- OOP (C++) -- Lecture 10 34

Explain to 10-4 The friend functions can serve, for example, to conduct operations between two different classes. Generally the use of friend functions is out of an object-oriented programming methodology, so whenever possible it is better to use members of the same class to make the process. Such as in the previous example, it would have been shorter to integrate duplicate() within the class CRectangle. M.EC203* -- OOP (C++) -- Lecture 10 35

Friend classes (friend) Just as we have the possibility to define a friend function, we can also define a class as friend of another one, allowing that the second one access to the protected and private members of the first one. M.EC203* -- OOP (C++) -- Lecture 10 36

The Program 10-5 (1) M.EC203* -- OOP (C++) -- Lecture 10 37

The Program 10-5 (2) M.EC203* -- OOP (C++) -- Lecture 10 38

Explain to 10-5 In this example we have declared CRectangle as a friend of CSquare so that CRectangle can access the protected and private members of CSquare, more concretely CSquare::side, that defines the square side width. M.EC203* -- OOP (C++) -- Lecture 10 39

Explain to 10-5 You may also see something new in the first instruction of the program, that is the empty prototype of class CSquare. This is necessary because within the declaration of CRectangle we refer to CSquare (as a parameter in convert()). The definition of CSquare is included later, so if we did not include a previous definition for CSquare this class would not be visible from within the definition of CRectangle. M.EC203* -- OOP (C++) -- Lecture 10 40

Explain to 10-5 Consider that friendships are not corresponded if we do not explicitly specify it. In our CSquare example CRectangle is considered as a class friend, but CRectangle does not do the proper thing with CSquare, so CRectangle can access to the protected and private members of CSquare but not the reverse way. Although nothing prevents us from declaring CSquare as a friend of CRectangle. M.EC203* -- OOP (C++) -- Lecture 10 41

Have you questions? M.EC203* -- OOP (C++) -- Lecture 10 42

Summary Overloading operators The keyword this Static members Relationships between classes M.EC203* -- OOP (C++) -- Lecture 10 43

End of Thank you for ATTENTION M.EC203* -- OOP (C++) -- Lecture 10 44