COP 3330 Object-oriented Programming in C++

Slides:



Advertisements
Similar presentations
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.
Advertisements

Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
Road Map Introduction to object oriented programming. Classes
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Review of C++ Programming Part II Sheng-Fang Huang.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
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.
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.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Using Member Functions and Data Members.
Constructors Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction.
Classes - Part II (revisited) n Constant objects and member functions n Definition Form of Member Functions n friend functions and friend classes n Constructors.
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.
Object Oriented Programming COP3330 / CGS5409.  Classes & Objects  DDU Design  Constructors  Member Functions & Data  Friends and member functions.
Learners Support Publications Constructors and Destructors.
Constructors and Destructors
Learning Objectives Pointers as dada members
Classes (Part 1) Lecture 3
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?
Static data members Constructors and Destructors
Abstract Data Types Programmer-created data types that specify
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.
Review What is an object? What is a class?
Andy Wang Object Oriented Programming in C++ COP 3330
Programming with ANSI C ++
Chapter 1 C++ Basics Review
Andy Wang Object Oriented Programming in C++ COP 3330
Java Primer 1: Types, Classes and Operators
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Constructors and Other Tools Dr.
10.2 Classes Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Andy Wang Object Oriented Programming in C++ COP 3330
Review: Two Programming Paradigms
Constructor & Destructor
Class: Special Topics Copy Constructors Static members Friends this
group work #hifiTeam
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Object Oriented Programming COP3330 / CGS5409
More on Classes Programming in C++ Fall 2008
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 14 Inheritance Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Constructor & Destructor
Inheritance Basics Programming with Inheritance
Operator Overloading
CS212: Object Oriented Analysis and Design
Andy Wang Object Oriented Programming in C++ COP 3330
CONSTRUCTORS AND DESRUCTORS
Constructors and Other Tools
Constructors and destructors
Constructors and Destructors
Operator overloading Dr. Bhargavi Goswami
Computer Programming with JAVA
Friends, Overloaded Operators, and Arrays in Classes
Operator Overloading, Friends, and References
CISC/CMPE320 - Prof. McLeod
9-10 Classes: A Deeper Look.
COP 3330 Object-oriented Programming in C++
Java Programming Language
Andy Wang Object Oriented Programming in C++ COP 3330
COP 3330 Object-oriented Programming in C++
Dr. R Z Khan Handout-3 Classes
Class rational part2.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Constructors & Destructors
9-10 Classes: A Deeper Look.
(4 – 2) Introduction to Classes in C++
SPL – PS3 C++ Classes.
Presentation transcript:

COP 3330 Object-oriented Programming in C++ Classes and Objects: friend, conversion constructor, destructor, and const Spring 2019

Let’s Consider a New Class: Fraction A fraction represents a part of a whole or, more generally, any number of equal parts Consists of an integer numerator displayed above a line (or before a slash), and a non-zero integer denominator, displayed below (or after) that line frac.h contains the class declaration.   the Declare stage frac.cpp contains the class definition.  the Define stage main.cpp contains a simple program that tests the class.  the Use stage

A Motivating Example Suppose we want to write a function equals that compares two fraction objects and use it as follows A possible definition of equals might be

A Motivating Example A student, however, implemented equals as follows: What’s wrong? equals is not member function of Fraction It can’t directly access the private members and functions! For functions (e.g. comparing two objects) that need to access private members frequently, we would like C++ to allow such access C++ solution: the “friend” keyword

Friend Friend allows a class to grant FULL access to an outside entity “Full access" means access to all the class' members, including the private section An outside entity can be a function, or even another class To grant friend status, declaration of the "friend" is made inside the class declaration block, with the keyword friend in front of it A friend function to a class will have full access to the private members of the class

Friend vs. Member Functions When a function works on two objects, we can pass both as parameters and make it a friend Another option is to use a member function one of the two objects must be the calling object Example: The Equals() function could have been set up as a member function, which would mean this kind of call: f1 is the calling object (the object calling a member function) and f2 is passed into f1's Equals function as an argument

Friend vs. Member Functions Whether to make a function a friend or a member function of a class? Usually a stylistic decision Different programmers may have different preferences The member and friend versions are not always equivalent In the friend version, the function cannot change original fractions What about the member version?

Conversion Constructors A special constructor with one parameter, s.t., A variable of that parameter’s type is converted to an object An example The conversion constructor could be used to perform automatic type conversions:

Conversion Constructors A constructor with multiple parameters may be a conversion constructor if all but one parameter is optional: Fraction(int n, int d = 1); Automatic type conversion for constructors can be suppressed by using the keyword explicit in front of the declaration: explicit Fraction(int n); The above constructor will not auto-convert integers to Fraction

Destructors The destructor looks like the default constructor, but with a ~ in front Cannot have parameters, and only one destructor for a class Example: the destructor for Fraction is ~Fraction() This function is called automatically (not explicitly) right before an object is deallocated by the system usually when it goes out of scope Typical job is to do any clean-up tasks (usually involving memory allocation) needed before an object is deallocated Destructor: “Clean up, clean up, everybody!”

Using Const in Classes Const Why Const? A compile time constraint that an object cannot be modified const int i = 9; i = 6; // Oops! const int* p = &i; // data is const, while point is not *p = 7; // Oops! p++; // Complier says it is okay Why Const? Guard against inadvertent write to the object Self documenting Enable compiler to do more optimization, making code tighter Const variables can be put in ROM

Using Const in Classes Objects can also be declared as const The constructor will always run to initialize the object, and the object's state (i.e. internal data) cannot be changed const Fraction ZERO; // this fraction is fixed at 0/1 const Fraction FIXED(3,4); // this fraction is fixed at ¾ A const object may only call const member functions FIXED.Show(); // Good ZERO.SetValue(3, 7); // Oops!

Const Member Data When a variable is declared with const in a normal block of code, it must be initialized on the same line const int SIZE = 10; However, it is illegal to initialize const member variables on the declaration line, or split them up into constructor initializations Solution: consider the initialization list Constructor (parameters) : mv1(v1), …, mvk (vk)

Const Reference Parameters Pass by Reference for Object Parameters, If Possible If pass by value, a copy of the object is made (R-value) Whether const or not doesn’t matter much If pass by reference, no copy is made (L-value) If pass by const reference, no copy is made, but the object cannot be changed Example: Equals and Add functions in the class Fraction

Const Member Functions Const is at the end of a member function Fraction Add(const Fraction& rhs) const; The member function will NOT change member variables! Const must go on the declaration and on the definition Summary Constructors: initialize the object (set the member data), so non-const Mutators: change member data, non-const Accessors: retrieve data from inside the class, const Print, show, display, etc.

Questions