1 2/21/05CS250 Introduction to Computer Science II Destructors, Get and Set, and Default Memberwise Assignment.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Using Classes to Store Data Computer Science 2 Gerb.
Abstract Data Type Fraction Example
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
C++ Classes & Data Abstraction
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. S. Kozaitis Summer Summary of Topics Related to Classes Class definition Defining member.
1 CMSC 202 More C++ Classes. 2 Announcements Project 1 due midnight Sunday 2/25/01 Quiz #2 this week Exam 1 Wednesday/Thursday Project 2 out Monday March.
Class and Objects.
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
More on Objects CS 102 More, more, more on Objects.
CS 106 Introduction to Computer Science I 03 / 23 / 2007 Instructor: Michael Eckmann.
 Wednesday, 9/25/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/25/02  QUESTIONS??  Today: More on functions  Reading: Chapter 3 through 3.7 
Classi - Esempi1 // SalesPerson class definition // Member functions defined in salesp.cpp #ifndef SALESP_H #define SALESP_H class SalesPerson { public:
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
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+
C++ Lecture 4 Tuesday, 15 July Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l.
1 3/2/05CS250 Introduction to Computer Science II Composition and friend Functions.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
First steps Jordi Cortadella Department of Computer Science.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Part 9:
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
11 Introduction to Object Oriented Programming (Continued) Cats.
Object Oriented Programming (OOP) Lecture No. 11.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
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.
CLASSES : A DEEPER LOOK Chapter 9 Part I 1. 2 OBJECTIVES In this chapter you will learn: How to use a preprocessor wrapper to prevent multiple definition.
Chapter 9 Classes: A Deeper Look, Part I Part II.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
1 const and this Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures Series.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of 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?
1 Lecture 17 Operator Overloading. 2 Introduction A number of predefined operators can be applied to the built- in standard types. These operators can.
The This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros
Introduction to Programming Lecture 40. Class Class is a user defined data type.
1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Chapter Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};
February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 20 November 10, 2009.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
1 Introduction to Object Oriented Programming Chapter 10.
1 Introduction to Computer Science for Majors II CPSC 233, Winter 2013 CPSC 233, winter 2013 Tutorial 7, Feb 6/7, 2013.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 7: Classes Part II Outline 7.1 Introduction 7.2 const (Constant) Objects and const Member Functions.
Chapter 9 Classes: A Deeper Look, Part 1 Seventh Edition C++ How to Program © by Pearson Education, Inc. All Rights Reserved.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. Këpuska Summer 2004 from Dr. S. Kozaitis Spring 2003 slides 1 Summary of Chapter 6: Classes.
Object Access m1.write(44); m2.write(m2.read() +1); std::cout
CompSci 230 S Programming Techniques
Lecture 5: Classes (continued) Feb 1, 2005
Review What is an object? What is a class?
Object-Oriented Programming
Fraction Abstract Data Type
CS148 Introduction to Programming II
#include <iostream.h>
A Deeper Look at Classes
CS 144 Advanced C++ Programming February 21 Class Meeting
Separating Interface from Implementation
CS150 Introduction to Computer Science 1
Chapter 11 Classes.
Constructors & Destructors
Classes Member Qualifiers
Presentation transcript:

1 2/21/05CS250 Introduction to Computer Science II Destructors, Get and Set, and Default Memberwise Assignment

2 2/21/05CS250 Introduction to Computer Science II Destructors  The opposite of constructors  Called whenever an object is destroyed o It is out of scope. For example, if it was a local variable in a function and the function has completed  A destructor has no arguments and or return value  Only one destructor allowed!  No need for us to explicitly declare a destructor

3 2/21/05CS250 Introduction to Computer Science II Example class Test { private: int id; public: Test( int ); ~Test(); }; Test::Test( int i ) { id = i; cout << "constructor for " << id << " is called\n"; } Test::~Test() { cout << "destructor for " << id << " is called\n"; }

4 2/21/05CS250 Introduction to Computer Science II What is the Output? void funct(); int main() { Test t1( 1 ); funct(); Test t3( 3 ); return 0; } void funct() { Test t2( 2 ); }

5 2/21/05CS250 Introduction to Computer Science II Set and Get Functions  The principle of least privilege says that we should only provide outside members with access to data that is absolutely necessary  Data members should therefore be set to private  To modify and get access to that data, specific member functions need to be provided  These are the Set and Get functions

6 2/21/05CS250 Introduction to Computer Science II Set and Get Functions  The functions don’t need to be called set or get, but it has become commonplace to do this  In the time class we could have the following set functions: o void setTime( int, int, int ); o void setHour( int ); o void setMinute( int ); o void setSecond( int );

7 2/21/05CS250 Introduction to Computer Science II Get Functions  For the Time class we would have the following get functions: int getHour(); int getMinute(); int getSecond(); Time t4( 9, 25, 30 ); Time t5( 45, 90, 72 );

8 2/21/05CS250 Introduction to Computer Science II References to Private Data  Although we may have declared the data inside of a class as private, there is a way to manipulate it directly (not use a member function)  It is important that we are aware of this so that we can avoid it in the future

9 2/21/05CS250 Introduction to Computer Science II Example class Test { private: int id; public: int &setId( int ); int getId(); }; int& Test::setId( int newId ) { id = ( newId >= 0 && newId <=10 )? newId : 0; return id; } int Test::getId() { return id; }

10 2/21/05CS250 Introduction to Computer Science II What is the Output? int main() { Test t1; int &testRef = t1.setId( 5 ); cout << "Id is: " << t1.getId() << endl; testRef = 34; cout << "Id is: " << t1.getId() << endl; t1.setId( 4 ) = 52; cout << "Id is: " << t1.getId() << endl; return 0; }

11 2/21/05CS250 Introduction to Computer Science II Default Memberwise Assignment  It is possible to assign an object to another object of the same type  This will assign every data member in the first object to the value of the equivalent data member in the second object

12 2/21/05CS250 Introduction to Computer Science II Example Time t1( 9, 25, 32 ); Time t2; t2 = t1; t2.printStandard();  Let’s illustrate this further with another example

13 2/21/05CS250 Introduction to Computer Science II Exercise 6.7  This exercise asks us to create a class called Rational to perform arithmetic with fractions  Fraction data should be stored as numerator and denominator and should be stored in reduced form  The class should include member functions for addition, subtraction, multiplication, division, and printing

14 2/21/05CS250 Introduction to Computer Science II Class Interface class Rational { public: Rational( int = 0, int = 1 ); Rational addition( const Rational & ); Rational subtraction( const Rational & ); Rational multiplication( const Rational & ); Rational division( const Rational & ); void printRational (); private: int numerator; int denominator; void reduction(); };

15 2/21/05CS250 Introduction to Computer Science II Driver or Main Rational c( 1, 3 ); Rational d( 7, 8 ); Rational x; c.printRational(); cout << " + "; d.printRational(); x = c.addition( d ); cout << " = "; x.printRational(); cout << '\n';

16 2/21/05CS250 Introduction to Computer Science II Summary  There is a test on Wednesday, Feb 23  We have completed everything in chapters 1 and 2  That is the material you will be tested on  Office hours today pm. I will also be available tomorrow pm