Week 3: Classes, constructors, destructors, new operator Operator and function overloading.

Slides:



Advertisements
Similar presentations
C++ Classes & Data Abstraction
Advertisements

***** SWTJC STEM ***** Chapter 4-1 cg 42 Object Oriented Program Terms Up until now we have focused on application programs written in procedural oriented.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
Object-Oriented PHP (1)
Road Map Introduction to object oriented programming. Classes
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Lecture 9 Concepts of Programming Languages
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
CS352-Week 2. Topics Heap allocation References Pointers.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
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.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
CSC 143F 1 CSC 143 Constructors Revisited. CSC 143F 2 Constructors In C++, the constructor is a special function automatically called when a class instance.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
C++ Class. © 2005 Pearson Addison-Wesley. All rights reserved 3-2 Abstract Data Types Figure 3.1 Isolated tasks: the implementation of task T does not.
Chapter -6 Polymorphism
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
1 Introduction to Object Oriented Programming Chapter 10.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
1 Classes struct Public and Private Parts of a struct Class Scope of a Class Overloading Member Functions Class in a Class Static Members of Classes this.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Object Lifetimes and Dynamic Objects Lecture-7. Object Lifetimes and Dynamic Objects External (Global) Objects Persistent (in existence) throughout the.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Creating Your Own Classes
Classes C++ representation of an object
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Abstract Data Types Programmer-created data types that specify
Object Oriented Programming
Introduction to Classes
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Chapter 5 Classes.
Creating Your OwnClasses
The dirty secrets of objects
Lecture 4-7 Classes and Objects
Lecture 9 Concepts of Programming Languages
Defining Classes and Methods
CS212: Object Oriented Analysis and Design
Object-Oriented Programming
Classes C++ representation of an object
Object-Oriented PHP (1)
Classes and Objects CGS3416 Spring 2019.
(4 – 2) Introduction to Classes in C++
Lecture 9 Concepts of Programming Languages
Presentation transcript:

Week 3: Classes, constructors, destructors, new operator Operator and function overloading

Previous Knowledge What is a class and why they are useful Difference between class methods and instance methods Public vs private and accessors and mutators (“getters” and “setters”) Function override (i.e. __str__ in python) Function overload

Objectives Understand the syntax of: – Creating a class (template for type) – Providing default constructors and destructors – Adding other constructors – Accessing state variables using public accessors and mutators – Follow flow of execution for instance method invocation Instantiating an instance of a class Overload << operator to control printing

Classes Expanded concept of data structure Creates new data type composed of other data types Combines data and operations on data Cohesive structure that better models natural relationships Requires a different kind of source file organization – Object oriented analysis and design

Initial class definition considerations What data/fields defines a specific instance? What operations are naturally used in conjunction with class? Look over set of operators and consider if each has a natural context – Can they be added? – What does it mean to be equal? – Is there a natural ordering (used for comparison)? – If I print this object, what would be output?

Example Point (2 dimensions) – Has x and y dimension <= data that defines a specific point – Can define the inverse point <= operation on data – We can add one point to another <= Addition has specific meaning in this context – Printing of a point should include value of x and y

Point class class Point { double x, y; public : void plus(Point c); //function prototype void print() { cout << "(" << x << "," << y << ")"; } void init(double u, double v) { x = u; y = v; } }; void Point::plus(Point c) { //definition not inline //offset the existing point by point c x += c.x; y += c.y; }

Point class class Point { double x, y; public : void plus(Point c); //function prototype void print() { cout << "(" << x << "," << y << ")"; } void init(double u, double v) { x = u; y = v; } }; void Point::plus(Point c) { //definition not inline //offset the existing point by point c x += c.x; y += c.y; } class is keyword Point is the name of the class class definition is delimited by braces

Point class class Point { double x, y; public : void plus(Point c); //function prototype void print() { cout << "(" << x << "," << y << ")"; } void init(double u, double v) { x = u; y = v; } }; void Point::plus(Point c) { //definition not inline //offset the existing point by point c x += c.x; y += c.y; } items defined after public keyword can be used by any instantiating code i.e. public for all to see

Point class class Point { double x, y; public : void plus(Point c); //function prototype void print() { cout << "(" << x << "," << y << ")"; } void init(double u, double v) { x = u; y = v; } }; void Point::plus(Point c) { //definition not inline //offset the existing point by point c x += c.x; y += c.y; } classes have variables called attributes think of class attributes as defining a structure x y

Point class class Point { double x, y; public : void plus(Point c); //function prototype void print() { cout << "(" << x << "," << y << ")"; } void init(double u, double v) { x = u; y = v; } }; void Point::plus(Point c) { //definition not inline //offset the existing point by point c x += c.x; y += c.y; } items defined before public or after private keyword are not visible to the public i.e. for private use only

Point class class Point { private: double x, y; public : void plus(Point c); //function prototype void print() { cout << "(" << x << "," << y << ")"; } void init(double u, double v) { x = u; y = v; } }; void Point::plus(Point c) { //definition not inline //offset the existing point by point c x += c.x; y += c.y; } items defined before public or after private keyword are not visible to the public i.e. for private use only

Point class class Point { double x, y; public : void plus(Point c); //function prototype void print() { cout << "(" << x << "," << y << ")"; } void init(double u, double v) { x = u; y = v; } }; void Point::plus(Point c) { //definition not inline //offset the existing point by point c x += c.x; y += c.y; } Must initialize attributes to meaningful values ** What happens if init is never called? (the only function that sets x and y) ** Will it create a compile error?

Point class class Point { double x, y; public : void plus(Point c); //function prototype void print() { cout << "(" << x << "," << y << ")"; } void init(double u, double v) { x = u; y = v; } }; void Point::plus(Point c) { //definition not inline //offset the existing point by point c x += c.x; y += c.y; } Longer functions are defined outside of the class definition Classname:: denotes that the function is part of the class Must still have function prototype in class definition

Point class class Point { double x, y; public : void plus(Point c); //function prototype void print() { cout << "(" << x << "," << y << ")"; } void init(double u, double v) { x = u; y = v; } }; void Point::plus(Point c) { //definition not inline //offset the existing point by point c x += c.x; y += c.y; } Since Point is a data type, it can be used as a parameter plus is a mutator (changes x and y)

Main function int main() { Point w1, w2; w1.init(0, 0.5); w2.init(-0.5, 1.5); cout << "\npoint w1 = "; w1.print(); cout << "\npoint w2 = "; w2.print(); w1.plus(w2); cout << "\npoint w1 after plus = "; w1.print(); cout<<endl; } point w1 = (0,0.5) point w2 = (-0.5,1.5) point w1 after plus = (-0.5,2)

Main function int main() { //Declares/allocates w1 and w2 Point w1, w2; w1.init(0, 0.5); //sets the x and y value for w1 w2.init(-0.5, 1.5); //sets the x and y value for w2 cout << "\npoint w1 = "; w1.print(); //executes print with w1 data cout << "\npoint w2 = "; w2.print(); //executes print with w2 data w1.plus(w2); //calls plus cout << "\npoint w1 after plus = "; w1.print(); cout<<endl; } junk x y junk x y w1 w2

Main function int main() { //Declares/allocates w1 and w2 Point w1, w2; w1.init(0, 0.5); //sets the x and y value for w1 w2.init(-0.5, 1.5); //sets the x and y value for w2 cout << "\npoint w1 = "; w1.print(); //executes print with w1 data cout << "\npoint w2 = "; w2.print(); //executes print with w2 data w1.plus(w2); //calls plus cout << "\npoint w1 after plus = "; w1.print(); cout<<endl; } x y x y x y w1 w2

Main function int main() { Point w1, w2; w1.init(0, 0.5); //sets the x and y value for w1 w2.init(-0.5, 1.5); //sets the x and y value for w2 cout << "\npoint w1 = "; w1.print(); //executes print with w1 data cout << "\npoint w2 = "; w2.print(); //executes print with w2 data w1.plus(w2); //calls plus cout << "\npoint w1 after plus = "; w1.print(); cout<<endl; } void Point::plus(Point c) { //c is equal to w2 x += c.x; // x= y += c.y;// y= } x y x y x y w1 w2 w1 is the calling object so unqualified variables refer to w1

Main function int main() { Point w1, w2; w1.init(0, 0.5); //sets the x and y value for w1 w2.init(-0.5, 1.5); //sets the x and y value for w2 cout << "\npoint w1 = "; w1.print(); //executes print with w1 data cout << "\npoint w2 = "; w2.print(); //executes print with w2 data w1.plus(w2); //calls plus cout << "\npoint w1 after plus = "; w1.print(); cout<<endl; } void Point::plus(Point c) { //c is equal to w2 x += c.x; // x= y += c.y;// y= } x y x y x y w1 w2

Update using C++ constructs Creation of objects using constructors rather than init function Override adding one object to another using function overloading rather than creating print function This will affect both the class declaration and the invocation (how we use the methods in the main function

Constructors Constructors are run when an object is created It is a special function that different from other methods Allows declaration, allocation and initialization of user defined types like native types //Create point with initialized values Point w1(0,0.5),w2(-0.5,1.5); //Create points with uninitialized attributes //and initialize with method Point w1, w2; w1.init(0, 0.5); w2.init(-0.5, 1.5);

Updates to class definition //Create point with initialized values Point w1(0,0.5),w2(-0.5,1.5); //Create point with default values Point emptyPoint; void init(double u, double v) { x = u; y = v; } //signature has no return type and name of class (exact match) Point(double a, double b) { //in scope are parameter and attributes //can initialize class attributes x=a; y=b; } //Creates point with 0 and 0 as defaults Point() { x=0; y=0; }

Update addition to use + operator Update one point by adding another point to it (a bit awkward…will fix this Thursday) Can use the addition(+) operator int main() { Point emptyPoint,w1(0,0.5), w2(-0.5,1.5); cout << "\nTesting creation of point with no parameters = "; emptyPoint.print(); cout << "\npoint w1 = "; w1.print(); cout << "\npoint w2 = "; w2.print(); w1+w2; cout << "\npoint w1 after plus = "; w1.print(); cout<<endl; }

Overloading addition operator //point2.cc class Point { double x, y; public : Point(){x=0;y=0;} Point(double a,double b) {x=a;y=b;} void operator+(Point c); //function prototype void print (){ cout << "(" << x << "," << y << ")";} }; void Point::operator+(Point c) { //definition not inline //offset the existing point by point c x += c.x; y += c.y; }

Updated main program //point2.cc int main() { Point emptyPoint,w1(0,0.5), w2(-0.5,1.5); cout << "\nTesting creation of point with no parameters = "; emptyPoint.print(); cout << "\npoint w1 = "; w1.print(); cout << "\npoint w2 = "; w2.print(); w1+w2; cout << "\npoint w1 after plus = "; w1.print(); cout<<endl; } Testing creation of point with no parameters = (0,0) point w1 = (0,0.5) point w2 = (-0.5,1.5) point w1 after plus = (-0.5,2)

Recap Class – user defined data type – combines data (attributes) and functions (methods) together Constructors allow us to control the initialization of attributes upon creation Redefine operator functions to use natural semantics (operator overloading)

Thursday Destructors Overloading the print function (operator <<) Fixing addition to return a new object rather than mutate the existing object w3=w1+w2; Friend functions Accessors and mutators for encapsulation