P Improvements - Constructor p Parameter passing Constructors Problem Solving With C++

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Engineering Problem Solving With C++ An Object Based Approach Additional Topics Chapter 10 Programming with Classes.
Operator overloading redefine the operations of operators
The Point Class public class Point { public double x; public double y; public Point(double x0, double y0) { x = x0; y = y0; } public double distance(Point.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors.
Constructors 11/10/10. Today  Use constructors to initialize objects.  Use const to protect data members.
תכנות מונחה עצמים Object Oriented Programming (OOP) אתגר מחזור ב ' Classes – - המשך.
. Plab – Tirgul 6 Classes. Point.h class Point { public: Point(int x, int y); ~Point(); int getX() const; int getY() const; private: int m_x, m_y; };
Const Parameters & In Line Functions 04/15/11. Next Time  Quiz, Monday, 04/18/11  Over 5.2 and 5.3 void functions pass-by-reference  Read 7.1 about.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
Lecture 5-6 OOP Overview. Outline A Review of C++ Classes (Lecture 5) OOP, ADTs and Classes Class Definition, Implementation and Use Constructors and.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Engineering Problem Solving With C++ An Object Based Approach Chapter 8 Introduction to Classes.
תכנות מונחה עצמים Object Oriented Programming (OOP) אתגר מחזור ב' Classes.
Alok Mehta - Programming in Lisp - Data Abstraction and Mapping Programming in Lisp Common Lisp Object System (CLOS)
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Methods and Constructors reading: self-checks: #1-12.
1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
Class Byteline Ustyugov Dmitry MDSP November, 2009.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Constructors and Encapsulation reading: self-checks: #10-17.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
 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.
Chapter 15 C++ Function By C. Shing ITEC Dept Radford University.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 3 ADT and C++ Classes (II) Instructor: Zhigang Zhu Department of Computer Science.
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
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?
Xiaoyan Li CSC211 Data Structures Lecture 2 ADT and C++ Classes (I) Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 2 ADT and C++ Classes (I) Instructor: Zhigang Zhu Department of Computer Science City.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
11/07/11Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 8 An Introduction.
6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 8 An Introduction.
INTRODUCTION TO CLASSES & OBJECTS CREATING CLASSES USING C#
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Pointer to an Object Can define a pointer to 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?
CSC212 Data Structure - Section BC
Memberwise Assignment / Initialization
Object initialization: constructors
Topic 28 classes and objects, part 2
Building Java Programs
Topic 29 classes and objects, part 3
Building Java Programs
Building Java Programs
References, const and classes
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Dr. R Z Khan Handout-3 Classes
Building Java Programs
Building Java Programs
Building Java Programs
Transformations Review
Building Java Programs
Chapter 11 Inheritance and Encapsulation and Polymorphism
Chapter 11 Classes.
Topic 29 classes and objects, part 3
Building Java Programs
Topic 29 classes and objects, part 3
Presentation transcript:

p Improvements - Constructor p Parameter passing Constructors Problem Solving With C++

Example: Point Class p This is an ADT to store and manipulate the location of a single point on a two- dimensional plane. XY A (-1, 0.8)

Point Class (con’d) p Two modification member functions: p Shifting function p Example: B obtained by shifting point A by 1.3 units along the X axis and by -1.4 units along the Y axis X Y A (-1, 0.8)

Point Class (con’d) p Two modification member functions: p Shifting function p Example: B obtained by shifting point A by 1.3 units along the X axis and by -1.4 units along the Y axis X Y A (-1, 0.8) B (0.3, -0.8)

Point Class (con’d) p Two modification member functions: (con’d) p Rotating function p Example: C obtained by rotating point A by 90 degree clockwise. X Y A (-1, 0.8)

Point Class (con’d) p Two modification member functions: (con’d) p Rotating function p Example: C obtained by rotating point A by 90 degree clockwise. X Y A (-1, 0.8) C (0.8, 1.0)

Point Class (con’d) p Two constant member functions : p return X coordinate of the point. double get_x() const; p return Y coordinate of the point double get_y() const;

Point Class (con’d) Class Point { public: public: void shift(double x_ammount, double y_ammount); void rotate90(); double get_x() const {return x;} double get_y() const {return y;} private: private: double x; // x coordinate of this point double y; // y coordinate of this point double y; // y coordinate of this point}; Inline functions

Improvements: Constructor p A member function that is automatically called to initialize an object when the object is declared

Improvements: Constructor p A member function that is automatically called to initialize an object when the object is declared p The name of the constructor must be the same as the name of the class

Improvements: Constructor p A member function that is automatically called to initialize an object when the object is declared p The name of the constructor must be the same as the name of the class p It does not have any return value

Constructor (con’d) p Two kinds of constructors p Default constructor: constructor with no parameters p Constructor with parameters p Example: Point Class

Constructor (con’d) p Example: Point Class Class Point { …... //Constructor Point( ); //or Point(double initial_x, double initial_y); …… };

Constructor (con’d) p Example: Point Class p Application Point pointA; Point pointA; Point pointB(5, 10); Point pointB(5, 10);

Constructor (con’d) p Example: Point Class p Application Point pointA; Point pointA; Point pointB(5, 10); Point pointB(5, 10); If the constructor has parameters, then the values must be given after the object name.

Constructor (con’d) p A good technique: using constructor with default parameters p Example: Point Class Point::Point(double initial_x = 0, Point::Point(double initial_x = 0, double initial_y = 0); double initial_y = 0);

Constructor (con’d) p When you declare an object, what happen to these declarations? int main( ) { Point pointA(5, 10); Point pointB(5); Point pointC; …... return 0; };

Constructor (con’d) p When you declare an object, what happen to these declarations? void main() { Point pointA(5, 10); Point pointB(5); Point pointC; …... }; Initial_x = 5, initial_y = 10 Initial_x = 0, initial_y = 0 Initial_x = 0, initial_y = 0 Initial_x = 5 initial_y = 0 Initial_x = 5 initial_y = 0

Constructor (con’d) p What if you write a class without constructors?

Constructor (con’d) p What if you write a class without constructors? Automatic default constructor will be called