Presentation is loading. Please wait.

Presentation is loading. Please wait.

COP 3330 Object-oriented Programming in C++

Similar presentations


Presentation on theme: "COP 3330 Object-oriented Programming in C++"— Presentation transcript:

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

2 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

3 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

4 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

5 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

6 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

7 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?

8 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:

9 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

10 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!”

11 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

12 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!

13 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)

14 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

15 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.

16 Questions


Download ppt "COP 3330 Object-oriented Programming in C++"

Similar presentations


Ads by Google