1 CS 201 Introduction to c++ (2) Debzani Deb. 2 Classes (1) A class definition – in a header file :.h file A class implementation – in a.cc,.cpp file.

Slides:



Advertisements
Similar presentations
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Advertisements

Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
C++ Programming Languages
Operator Overloading Fundamentals
Class and Objects.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Wednesday, 10/2/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/2/02  QUESTIONS (on HW02 – due at 5 pm)??  Today:  Review of parameters  Introduction.
Chapter 14: Overloading and Templates
Reviews for Exam 1 Chapter 1-4 CS 211 Data Structures MHC, 2007.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Lecture 5-6 OOP Overview. Outline A Review of C++ Classes (Lecture 5) OOP, ADTs and Classes Class Definition, Implementation and Use Constructors and.
Rossella Lau Lecture 5, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 5: Class construction  Encapsulation 
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
ASP.NET Programming with C# and SQL Server First Edition
Operator overloading Object Oriented Programming.
IT PUTS THE ++ IN C++ Object Oriented Programming.
Review of C++ Programming Part II Sheng-Fang Huang.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
1 Software Construction Lab 3 Classes and Objects in Java Basics of Classes in Java.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
1 Object-Oriented Programming Using C++ CLASS 1. 2 Review of Syllabus Catalog Description –An introduction to object oriented programming techniques using.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Week 14 - Wednesday.  What did we talk about last time?  More C++  new  delete  Differences for structs  OOP.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
Operator Overloading Operator Overloading allows a programmer to define new types from the built-in types. –Operator Overloading is useful for redefining.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
1 CSC241: Object Oriented Programming Lecture No 02.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Csi2172 class 5 Midterm: June 12. constructor Special method used to create objects of the class Never has a return type. Is called automatically upon.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Overloading operators C++ incorporates the option to use standard operators to perform operations with.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
1 CSC241: Object Oriented Programming Lecture No 03.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
1 Introduction to Object Oriented Programming Chapter 10.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
ECE 264 Object-Oriented Software Development
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Unit 2. Constructors It initializes an object when it is created. It has same as its class and syntactically similar to a method. Constructor have no.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 07: Object Oriented Programming:2014 Object-Oriented Programming in C++ Operator.
Constructors & Destructors Controlling initialization & destruction.
Operator Overloading Ritika Sharma.
Learning Objectives Pointers as dada members
By Muhammad Waris Zargar
group work #hifiTeam
Operator Overloading
CISC/CMPE320 - Prof. McLeod
C++ Constructor Insanity CSE 333 Summer 2018
Presentation transcript:

1 CS 201 Introduction to c++ (2) Debzani Deb

2 Classes (1) A class definition – in a header file :.h file A class implementation – in a.cc,.cpp file // header file point.h #include using namespace std; class point { public: point (void); ~point (void); int getX (void) const; int getY (void) const; int getR (void) const; private: int x_; int y_; }; Indicates that it does not change its caller

3 Classes (2) // implementation for point.cpp #include #include “point.h” using namespace std; point :: point (void) { x_ = 0; y_ = 0;} point :: ~point (void) { /* empty */} int point :: getX (void) const { return x_;} int point :: getY (void) const { return y_;} int point :: getR (void) const { return sqrt(x_*x_ + y_*y_); }

4 Constructors & Destructors Object is an instance of a class. A constructor initializes members of an object. point :: point (void) { x_ = 0; y_ = 0;}  A class can have multiple constructors  A default constructor is a constructor that can be called with no arguments. Destructors are used to release any resources allocated by an object.  No parameters and returns no value point :: ~point (void)

5 Instantiation { point a; /* create an object a of type point. Constructor is called */ } // The object is destroyed point * b = new point(); Creates an object which will not be destroyed unless delete is called. delete b; Like our old friend: malloc/free pair

6 Accessing Class Members point a; int x = a.getX(); //valid int xx = a.x_; // not valid point * b = new point (); int y = b -> getY(); // valid int yy = b ->y_ // not valid

7 Polymorphism Poly means “many” and morph means “forms or shapes” Polymorphism - “capable of assuming various forms” Here is an example of real-life polymorphism Suppose you want to mail something  Walk into a post office (PostOffice Object) Mail_Item(….)  Give them a letter to be mailed and the fee to mail it, and expect it to be mailed properly. Void Mail_Item (Letter letter, double posage_fee);  To mail a box, we expect the same thing. Void Mail_Item (Box box, double posage_fee);

8 Why is this polymorphism? No matter what type of item you wish to mail, you use the same post office. (imagine if there were different post offices for every type of item.) You do not have to worry about “how” the item is going to arrive at its destination. You simply leave the item at the post office and expect that the item will be sent.

9 Function Overloading Two or more functions can share the same name provided that:  Type of their arguments differs OR  Number of their arguments differs To overload a function, simply declare and define all required versions. class point { public: //distance to b double distance (const point & b); // distance to (0,0) double distance (void); }; point a; double d = a.distance (b); double d0 = a.distance ();

10 “Donut” Model of Standard Type int

11 “Donut” Model of an Abstract Data Type

12 Wouldn’t be more easier to have something like this?

13 Operator Overloading One of C++’s most powerful feature. Operator overloading is just a type of function overloading An operator is always overloaded relative to a user-defined type, such as a class. To overload an operator, you create an operator function. An operator function is often a member of the class for which it is defined. Syntax: returnType className :: operator# (arg) { // operations to be performed }

14 Binary Operator When a member operator function overloads a binary operator, the function will have only one parameter. Consider : X + Y X is the object that generates the call to the operator function. Y is only a parameter of the operator function called by X.

15 Example : coord class class coord { int x, y; // coordinate values public: // constructor function is overloaded coord () { x = ; y = 0; } coord (int i, int j) { x = i; y = j;} // our new operator function coord operator+ (coord ob2) const; }; coord coord:: operator+ (coord ob2) const { coord temp; temp.x = x + ob2.x; temp.y = y + ob2.y; return temp; } int main() { coord o1(10, 10), o2(5, 3), o3, o4; o3 = o1 + o2; // string of addition is also allowed o4 = o1 + o2 + o3; return 0; }

16 Example coord class class coord { int x, y; // coordinate values public: coord () { x = ; y = 0; } coord (int i, int j) { x = i; y = j;} // operator function coord operator+ (coord ob2) const; coord operator- (coord ob2) const; void operator= (coord ob2) const; }; The operator –() can be implemented similarly to operator +().

17 Example: overloading operator =() We focus on the assignment operator function, operator =(). void coord :: operator = (coord ob2) { x = ob2.x; y = ob2.y; } int main () { coord o1(4,6), o2; o2 = o1; return 0; }

18 Why is operator overloading polymorphism? Polymorphism is using a common interface to accomplish multiple “expected tasks”. Because C++ does not automatically define “+” for user-defined classes, we can use operator overloading to add functionality to “+”. With an overloaded “+’ operator, we can now use the “+” interface for adding multiple types of objects as well. We can also keep its original capabilities.

19 Rules of operator overloading The operator must obey the built-in definition on its precedence, associativity, and number of operands. When overloaded as a member function, the operator has its associated object as the left-most operand. As member functions, unary operators have no arguments, and binary operators have only one. Only three operators : ::,.,.* can not be overloaded.

20 Reasons for using/not using C++ Advantages  C++ is a superset of C.  Efficient implementation  Low-level and high-level features  Portable  No need for fancy OOP resources Disadvantages  A hybrid  Little confusing syntax and semantics  Programmers must decide between efficiency and elegance.  No automatic garbage collection.

21 Important !!! Final: pm, Tuesday, 8 th May. Take Home exercise will be uploaded at 5.00 pm today. You will have 48 hours to submit it. Sort out all lab related problem before this Friday. Make appointment with Fuad if you need to see him. Course survey: Please go to the lab now and fill out the survey. It is very important for me and the department to have your feed back about this course.

22 Thanks Thank you very much for your attention. Thank you for all your efforts in this course. It was a pleasure to have you all as my students.