Classes & Objects Lecture-6. Classes and Objects A class is a 'blueprint' for all Objects of a certain type (defined by ADT) class defines the attributes.

Slides:



Advertisements
Similar presentations
Class and Objects.
Advertisements

Chapter 14: Overloading and Templates
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Classes Separating interface from implementation
Terms and Rules Professor Evan Korth New York University (All rights reserved)
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.
Lecture 9 Concepts of Programming Languages
C++ fundamentals.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Review of C++ Programming Part II Sheng-Fang Huang.
Abstract Data Types Using Classes Lecture-5. Abstract Data Types Using Classes Representing abstract data types using C++ We need the following C++ keywords.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Polymorphism Lecture-10. Print A Cheque A Report A Photograph PrintCheque() PrintReport() PrintPhoto() Printing.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
C++ Review (3) Structs, Classes, Data Abstraction.
Pointers OVERVIEW.
Operator Overloading Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Object-Oriented Programming in C++
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
 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.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Class Miscellanea Details About Classes. Review We’ve seen that a class has two sections: class Temperature { public: //... public members private: //...
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
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.
CSC241 Object-Oriented Programming (OOP) Lecture No. 5.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Object Lifetimes and Dynamic Objects Lecture-7. Object Lifetimes and Dynamic Objects External (Global) Objects Persistent (in existence) throughout the.
Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.
Yan Shi CS/SE 2630 Lecture Notes
Procedural and Object-Oriented Programming
User-Written Functions
Static data members Constructors and Destructors
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
A First C++ Class – a Circle
Introduction to Classes
Object Lifetime and Dynamic Objects
Constructor & Destructor
This pointer, Dynamic memory allocation, Constructors and Destructor
The dirty secrets of objects
Introduction to Classes
Operator Overloading.
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Presentation transcript:

Classes & Objects Lecture-6

Classes and Objects A class is a 'blueprint' for all Objects of a certain type (defined by ADT) class defines the attributes (data) and operations (methods) The class may be considered as an object factory allowing us to define objects of the same type. A single ADT allows us to make as many instances of a class as required

State Identity and Behavior A class is defined by the following 3 attributes A unique name, Attributes and Methods An Object is defined by Identity, State and Behavior The properties of the Class relate to the properties of the object Class attributes == Object state data Class Methods == Object behavior However Class name and Object identity are more complex

Object Name object are identified by unique names especially if only a few are created However if objects are constantly created and destroyed naming becomes more difficult In a program if many objects are created and destroyed at runtime the object is identified by it's memory location Objects which are predictable are identified by a unique name. Objects which are not are identified by there memory location and hence we use pointers. “when it rains there are many raindrops, it would be stupid to name each rain drop individually, so a rain drop may be identified by the space it occupies”

Difference between Classes and Objects Once defined, a class exists all the time the program is running, Objects may be created and destroyed For a single class there may be any number of objects instantiated at any one time A class has a unique name, attributes and methods. An Object has identity, state and behavior A class provides no state values for the object attributes. These can only be given values when applied to specific objects

Constructors When an object is created there are certain processes which must take place Instantiation always involves the allocation of memory for the objects state data. The methods do not require any memory as these are consistent for all objects of the class and are handled in the class itself. The special method which allocates the memory for an object is know as the 'constructor'

Constructors There are three basic types of constructor The default constructor User defined Constructor The Copy Constructor

The Default Constructor The default constructor takes no parameters (or return type) It performs no processing on the object data just memory allocation It will always be called by the compiler if no user defined constructor is provided The default constructor is not referred to by the programmer in the class definition

User Defined Constructor These constructors may be used to pass parameter values to the object These may be used to set default object values It is possible to have more than one constructor in a class passing different parameters This is known as overloading and gives more flexibility to the way the object can be instantiated

The Copy Constructor The copy constructor allows a new object to be created as a copy of an existing object. e.g. Therefore all of the objects state data is copied to the new object This object will be identical to the first except for it's identity This remains until methods are called on the new object to change the data object2 = object1;

Instantiating an Object To instantiate an object the abstract data type is called upon (class) This is like a normal data type (such as int, float, char, etc) However with the ADT we use the class name, for example with last weeks class Would create an object called point of class type Point2D However with classes we can instantiate many objects for example Point2D point1; Point2D point2; Point2D point3; Point2D point;

Sending Messages by using Methods The private data in a class can only be modified by using methods To call a method on an object we must refer to the object by name (and the specific instance) For Example: This calls the SetX method for the class to which point1 belongs to and sets the x attribute to 4.56 This is know as sending a message to the object and only methods contained in the public area of the class may be called. point1.setX(4.56);

Limitations of Default Constructor In the previous example the default constructor is called by the code Point2D point1; However this is not defined in the default class so the compiler takes care of it. However if we wish to give the object a default set of values we have to call methods for each object to set it. These limitations may be overcome by creating user defined constructors Point2D point1;

Coding a Constructor A C++ constructor has three important aspects It takes the same name as the class It may take arguments It cannot return a value For example the Point2D Class constructor would be class Point2D { public : Point2D(); }; Point2D::Point2D() { x = 0; y = 0; };

What to put in the Constructor Usually initialization code is put in the constructor As shown in the previous slide Now every time the object is created the current point is set to a useful default value (0,0) However sometimes this is not applicable to the program requirements

Parametrized Constructor As mentioned previously parameters can be passed to the constructor for example class Point2D { public : Point2D(); Point2D(float Xin, float Yin); }; Point2D::Point2D(float Xin, float Yin); { x = Xin; y = Yin; };

Default Parameter Values The above example may be modified to give a default value for example This means that if the Point2D constructor is called with no value it will default to [0.0,0.0] however this method may be overridden by using another value. For example Will construct a new class with a default value of [4.0,12.0] Point2D(float Xin=0.0,Yin=0.0); Point2D point(4.0,12.0);

User Defined Copy Constructor A programmer may also create a copy constructor by passing a reference to the object to be copied for example & denotes pass by reference and the const prefix indicates that the object being passed in must not be modified The code for the copy constructor may look like this Point2D(const Point2D &tocopy); Point2D::Point2D(const Point2D &tocopy) { x = tocopy.x; y = tocopy.y; }

Clean-up and Garbage Collection The default constructor is used to allocate memory for the creation of the Object The default destructor is used to de-allocate this memory With a static object this is done implicitly however for dynamic objects this must be done by the programmer This now allows us to determine exactly the life-cycle of the object by  being able to control both the creation and destruction of the object

Defining a destructor method The destructor method may be defined to add extra functionality to the destruction of an object It has certain characteristic which mark it out from the other methods as follows It takes the same name as the class, preceded by the 'tilde' character ~ It cannot take arguments It cannot return a value

Copy Constructor with dynamic memory If we have dynamic memory in our classes we must copy the contents of the memory into a new class when using the = This is done with the copy constructor as follows

Copy Constructor with dynamic memory #ifndef __TEST_CLASS_H__ #define __TEST_CLASS_H__ class TestClass { private : int *mem; int Size; public : TestClass(); TestClass(int Size); TestClass(const TestClass& in); ~TestClass(); void Print(void); int size(void){return Size;} int& operator [] (int index) { return mem[index]; } // non-const }; #endif;

Copy Constructor with dynamic memory #include "TestClass.h" #include #include // for memcpy using namespace std; TestClass::TestClass() { Size=0; mem=NULL; } TestClass::TestClass(int size) { mem = new int[size]; Size = size; } TestClass::TestClass(const TestClass& in) { Size=in.Size; mem = new int[Size]; for(int i=0; i<Size; ++i) { mem[i]=in.mem[i]; }

Copy Constructor with dynamic memory TestClass::~TestClass() { if(mem !=NULL) delete [] mem; } void TestClass::Print(void) { for(int i=0; i<Size; i++) cout<<"Mem["<<i<<"]="<<mem[i]<<endl; }

Copy Constructor with dynamic memory #include #include "TestClass.h" using namespace std; int main(void) { TestClass A(10); for(int i=0; i<A.size(); i++) A[i]=i; A.Print(); TestClass B=A; B.Print(); return 1; }

Operators Overloading Operator Overloading means making the compiler's built in operator symbols work with classes Operator symbols are things like + - = * etc We saw how these work with data types such as int float etc We can also do this with variable of different types by the use of coercion To do this with classes we may want to use a syntax as follows Object3 = Object1 + Object2

So how do we do this? To allow a similar operation as shown previously we have to code the mechanism ourselves In effect the + and = operator must be overloaded to be methods of the class By overloading operators in this way we can give the classes in a system a common interface, allowing us to perform similar operations on a range of different objects.

Overloading and the assignment Operator The = operator is already overloaded to work with objects as well as simple data types This has already been see with the copy constructor This means that object2 is instantiated with the same state as object1, We can also do this in normal code for example class_name object1 = object2; object1 = object2;

Overloading and the assignment operator This behavior is default for the = operator however we can program our own overloaded = operator within the class. This is required when pointers are used as the object will point only to the memory address and if the initial object is destroyed so will the memory address where the pointer points to, Therefore code will be required to create and copy the pointer data

Overloading operators for a class Assignment (=) is a fundamental operation of all classes and types, therefore it is provided by the compiler However no default behavior is provided for the other operators when used with classes Other operations must be specified by the programmer in the class Consider the class below Colour -R: float -G: float -B: float -A: float +Colour(r,g,b,c) +Colour + (Colour) ++=Colour The class could possibly require a + operator to add two colours together or a - operator to subtract them.

C++ Syntax The syntax required for operator overloading is :  The operator keyword  And one of the 40 operator which may be overloaded as shown below new delete + - * / % += -= *= /= %= = ^ & | ~ ^= &= |= > >>= <<= == != = ! && ||->* ->, comma operator () function call operator [] subscripting operator

The default assignment operator class Point2D { private : float x; float y; public : void SetX(float Xin); float GetX(void); float GetY(void); }; void Point2D::SetXY( float Xin,float Yin ) {x = Xin; y = Yin;} void Point2D::GetX(float Yin) { return x;} float Point2D::GetY(void) { return y; }

A Simple Program #include”Point2D.h” #include use namespace std; int main(void) { Point2D point, point2; point1.setXY(201,213) point2 = point1; cout << point1.x << “ “ << point1.y << endl; cout << point2.x << “ “ << point2.y << endl; }; Program Output

Syntax for Operator Overloading The syntax for operator overloading is as follows return_type operator symbol(parameter list... ) The return type for a relational operator is a Boolean value object1object2 if( > ) Object using Operator return true or false Parameter objects

Arithmetic Operator Overloading However arithmetic operators return an object of the appropriate class as shown below object = + Object using Operators object Parameter Operators Object to receive results a temporary object is returned from the operator method

The parameter List The parameter list usually consists of an object of the class This is passed by reference rather than by value to avoid making a copy of the object Therefore the const prefix is used to stop the reference object being modified in the class For example we may have the following: int operator < (const Object &object); Object operator -(const Object &object); less than (relational) minus (arithmetic)

Overloading relational operators These are the easiest to implement as they only return Boolean values Using the previous Point Class we can add an equality operator (==) bool operator == (const Point &rhs); And the method itself bool Point::operator==(const Point& rhs) { return (x==rhs.x && y==rhs.y) }

Using the relational operator In the code we can now write the following if(point1 == point2) cout << "points are the same" <<endl; else cout << "points are different"<<endl;

Overloading Arithmetic Operator As mentioned previously the arithmetic operator must return an object of the same class So for an + Point operator we would do the following Point operator + (const Point &rhs) and for the method Point Point::operator +(const Point &rhs) { return Point(x+rhs.x,y+rhs.y); }

Using arithmetic Overloading Now we can add two Point classes together using the following syntax Point point1,point2,point3; point3 = point1+point2; Now the object point3 contains the sum of the x points and the y points of objects point1 and point2.

Overloading the insertion and extraction operators (1) To overload the insertion and extraction operators we need to declare the following in the.h file friend std::ostream& operator<<(std::ostream& output, const Point3& s); friend std::istream& operator>>(std::istream& output, Point3 &s);

Overloading the insertion and extraction operators (2) And in the cpp file the actual methods ostream& operator<<(ostream& output, const Point & s) { return output<<"["<<s.x<<","<<s.y<<","<<s.z<<"]"; } istream& operator>>(istream& input, Point &s) { input >> s.x >>s.y >>s.z; return input; } Point3D point1; cin >> point1; cout << point1<<endl; we can now use these in code