More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
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.
Introduction to Programming Lecture 39. Copy Constructor.
A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
Class and Objects.
CS-240 Operator Overloading Dick Steflik. Operator Overloading What is it? –assigning a new meaning to a specific operator when used in the context of.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
Chapter 14: Overloading and Templates
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
Rossella Lau Lecture 5, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 5: Class construction  Encapsulation 
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.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
Operator Overloading in C++
Review of C++ Programming Part II Sheng-Fang Huang.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Copy Control Joe Meehean. More Class Responsibilities When making a new type (i.e., class) we must specify what happens when it is: Copied Assigned Destroyed.
The Rest of the Story.  Constructors  Compiler-generated  The Initializer List  Copy Constructors  Single-arg (conversion ctors)  The Assignment.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor.
Operator Overloads Part2. Issue Provide member +(int) operator Rational + int OK int + Rational Error.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
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+
Chapter 14 More About Classes. Chapter 13 slide 2 Topics 13.1 Instance and Static Members 13.2 Friends of Classes 13.3 Memberwise Assignment 13.4 Copy.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 10 Introduction to Classes
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
 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.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
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.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
By Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 13 More on Classes Chapter 8 Week 13 More on Classes Chapter 8.
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.
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?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
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.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 14: More About Classes.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
ECE 264 Object-Oriented Software Development
Yan Shi CS/SE 2630 Lecture Notes
Learning Objectives Pointers as dada members
Chapter 14: More About Classes.
Class: Special Topics Copy Constructors Static members Friends this
Memberwise Assignment / Initialization
group work #hifiTeam
Chapter 14: More About Classes.
Andy Wang Object Oriented Programming in C++ COP 3330
9-10 Classes: A Deeper Look.
Operator Overloading I
Operator Overloading; String and Array Objects
CS410 – Software Engineering Lecture #5: C++ Basics III
Andy Wang Object Oriented Programming in C++ COP 3330
Operator Overloading II
Rule of Three Part 1 & 2.
9-10 Classes: A Deeper Look.
SPL – PS3 C++ Classes.
Presentation transcript:

More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B

Overview Reminder: Midterm Wednesday Project 1 Part 1 Solution Project 1 Part 2 Overview Classes Recap More on C++ Classes

Project 1 Part 1 Solution Example solution presented in class – Will not be available online

Project 1 Part 2 Compare function Using test_it.sh to test sorter – Add test files – Run via: PATH=$PATH:. test_it.sh tests/

Classes Provide encapsulation – Combining a number of items, such as variables and functions, into a single package, such as an object of some class (or instance of the class) Today: Discuss encapsulating functionality provided by the programming language

Overloading Instance Methods Defining methods of a class with the same name, but different parameters void Date::update_date(int d, int m, int y) {…} void Date::update_date(Date &other) {…} Today: Look at overloading operators (+, -, >, =, ==, etc.)

More C++ Classes friend functions const keyword overloading operators arrays in classes destructor copy constructor overloading assignment operator

friend functions A friend function is a function that has access to the private members of a class The function must be declared inside the class class Date { friend bool equals(const Date &, const Date&); … };

const keyword bool Date::equal(const Date& other) const{…} const for parameters – Means the method cannot modify the parameter const at the end of the function declaration – Means that the method cannot modify its own state

Instance method or not? (In book) member v. non-member function Book’s suggestion: – Use member functions if operating on a single instance – Use non-member friend functions otherwise It’s up to you to choose: friend bool equals(const Date &, const Date&); bool equals(const Date &) const;

Overloading Operators Special syntax using operator keyword Add an int on the rhs (Date() + 5) Date operator +(int other) const; // member friend Date operator +(const Date &lhs, int rhs); Add an int on the lhs (5 + Date()) friend Date operator +(int lhs, const Date &rhs);

Overloading << for output In the following example, the left hand sides are always of type ostream&. – cout << date_a << “ “ << date_b – Parsed as ((cout << date_a) << “ “) << date_b Function prototype for Date objects friend ostream& operator <<(ostream &stream, const Date &date);

Overloading >> for input Similar behavior to output friend istream& operator >>(istream &stream, Date &date);

Arrays in classes You can have arrays of classes – Date some_dates[10]; You can have arrays in your classes – Static arrays Memory is cleaned up automatically just as all other instance variables when the object goes out of scope – Dynamic arrays (and other objects created with new) Need a special way to clean these up when the object goes out of scope

Destructor An instance’s destructor is called – implicitly when the instance goes out of scope – explicitly when delete is called on the instance Needs to be declared whenever dynamic memory is used in the class Can also be used for class “clean up” ~ClassName();

Copy constructor Default copy constructor will only perform a shallow copy – Pointers will point to the same dynamic data Copy constructor should implement a deep copy Used when returning an instance of the object, or passing the object by value Tuple(const Tuple &other);

Overloading Assignment Operator Used when assigning an object to another object: – Tuple a_tuple, b_tuple(5); – a_tuple = b_tuple; void operator =(const Tuple &other);

The Big Three Copy constructor Assignment operator Destructor If you have to implement any one of them, then you should implement all of them

For Tomorrow Review all material up to today Come to class with questions (Recommended) implement at least 1 of the sort functions