More About Classes Chapter 14. 22 Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:

Slides:



Advertisements
Similar presentations
Lesson 14 Classes, Continued CS1 Lesson More Classes1.
Advertisements

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.
Operator Overloading Fundamentals
Class and Objects.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
 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.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 11 More.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 11: More About.
More About Classes. C++: Classes & Objects -2 2 Instance and Static Members Each instance of a class has its own copies of the class’s instance (member)
Chapter 13: Overloading.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 15: Operator Overloading
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Operator Overloading in C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Chapter 18 - Operator Overloading Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
OPERATOR OVERLOADING. Closely related to function overloading is - operator overloading. In C++ you can overload most operators so that they perform special.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Using work from: Starting Out with C++ Early Objects Eighth Edition.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
CSCI 383 Object-Oriented Programming & Design Lecture 13 Martin van Bommel.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Case Study - Fractions Timothy Budd Oregon State University.
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+
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
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.
Chapter 11 More about classes and OOP. Relationships Between Classes Possible relationships – Access ("uses-a") – Ownership/Composition ("has-a") – Inheritance.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Operator Overloading.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
Chapter 8 Operator Overloading, Friends, and References.
C++ Class Members Class Definition – class Name – { – public: » constructor(s) » destructor » function members » data members – protected: » function members.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
1 Chapter 11 Structured Data. 2 Topics 10.1 Abstract Data Types 10.2 Combining Data into Structures 10.3 Accessing Structure Members 10.4 Initializing.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 5 1.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 14: More About Classes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Operator Overloading.
Yan Shi CS/SE 2630 Lecture Notes
Overloading C++ supports the concept of overloading Two main types
Reference Parameters There are two ways to pass arguments to functions: pass- by-value and pass-by-reference. pass-by-value A copy of the argument’s value.
Definition and Application of Binary Trees
Chapter 14: More About Classes.
Overloading Operator MySting Example
14.4 Copy Constructors.
Chapter 11: More About Classes and Object-Oriented Programming
Memberwise Assignment / Initialization
Chapter 14: More About Classes.
Inheritance Using work from:
Chapter 9 Classes: A Deeper Look, Part 1
Operator Overloading; String and Array Objects
Operator Overloading; String and Array Objects
Constant Member Functions
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Operator Overloading; String and Array Objects
Presentation transcript:

More About Classes Chapter 14

22 Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable: one variable shared among all objects of a class static member function: can be used to access static member variable; can be called before any objects are defined

33 static member variable 1) Declare with keyword static : class IntVal { public: intVal(int val = 0) { value = val; valCount++; } int getVal(); void setVal(int); private: int value; static int valCount; }

44 static member variable 2) Define outside of class: int IntVal::valCount = 0; 3)Can be accessed or modified by any object of the class: IntVal val1, val2; valCount val1 val2 value 2

55 static member function Declared with static before return type: static int getVal() { return valCount; } Can only access static member data Can be called independent of objects: cout << "There are " << IntVal::getVal() << " objects\n";

66 Friends Of Classes Friend: a function or class that is not a member of a class, but has access to private members of the class A friend function can be a stand-alone function or a member function of another class It is declared a friend of a class with friend keyword in the function prototype

77 friend Function Declarations Stand-alone function: friend void setAVal(intVal&, int); // declares setAVal function to be // a friend of this class Member function of another class: friend void SomeClass::setNum(int num) // setNum function from SomeClass // class is a friend of this class

88 friend Function Implementation Body: void setAVal(intVal &it, int val) { it.value = val; } Use: IntVal v1(13), v2(5); setAVal(v2, 11);

99 friend Class Declarations Class as a friend of a class: class FriendClass {... }; class NewClass { public: friend class FriendClass; // declares // entire class FriendClass as a friend // of this class … };

10 Memberwise Assignment Can use = to assign one object to another, or to initialize an object with an object’s data Copies member to member. e.g., v2 = v1; means: copy all member values from v1 and assign to the corresponding member variables of v2 Use at initialization: IntVal v3 = v2;

11 Copy Constructors Special constructor used when a newly created object is initialized to the data of another object of same class Default copy constructor copies field-to- field Default copy constructor works fine in many cases

12 Copy Constructors Problem: what if object contains a pointer? class SomeClass { public: SomeClass(int val = 0) {value=new int; *value = val;} int getVal(); void setVal(int); private: int *value; }

13 Copy Constructors What we get using memberwise copy with objects containing dynamic memory: SomeClass object1(5); SomeClass object2 = object1; object2.setVal(13); cout << object1.getVal(); // also 13 object1 object2 value 13

14 Programmer-defined Copy Constructor Allows us to solve problem with objects containing pointers: SomeClass::SomeClass(SomeClass &obj) { value = new int; *value = obj.value; } Copy constructor takes a reference parameter to an object of the class

15 Programmer-defined Copy Constructor Each object now points to separate dynamic memory: SomeClass object1(5); SomeClass object2 = object1; object2.setVal(13); cout << object1.getVal(); // still 5 object1 object2 value 135

16 Programmer-defined Copy Constructor Since copy constructor has reference to the object it is copying from, SomeClass::SomeClass(SomeClass &obj) it can modify that object. To prevent this from happening, make the object parameter const: SomeClass::SomeClass (const SomeClass &obj)

17 Operator Overloading Operators such as =, +, and others can be redefined when used with objects of a class The name of the function for the overloaded operator is operator followed by the operator symbol, e.g., operator+ to overload the + operator, and operator= to overload the = operator Prototype for the overloaded operator goes in the declaration of the class that is overloading it Overloaded operator function definition goes with other member functions

18 Operator Overloading Prototype: void operator=(const SomeClass &rval) Operator is called via object on left side return type function name parameter for object on right side of operator

19 Invoking An Overloaded Operator Operator can be invoked as a member function: object1.operator=(object2); It can also be used in more conventional manner: object1 = object2;

20 Returning A Value Overloaded operator can return a value class Point2d { public: float operator-(const point2d &right)// computes { return sqrt(pow((x-right.x),2) // and displays + pow((y-right.y),2)); } // the // distance between two points... private: int x, y; }; Point2d point1(2,2), point2(4,4); cout << point2 – point1 << endl; // displays

21 Returning A Value Return type the same as the left operand supports notation like: object1 = object2 = object3; Function declared as follows: SomeClass operator=(const someClass &rval) In function, include as last statement: return *this;

22 The this Pointer this : predefined pointer available to a class’s member functions Always points to the instance (object) of the class whose function is being called Is passed as a hidden argument to all non-static member functions Can be used to access members that may be hidden by parameters with same name

23 this Pointer Example class SomeClass { private: int num; public: void setNum(int num) { this->num = num; }... };

24 Notes On Overloaded Operators Can change meaning of an operator Cannot change the number of operands of the operator Only certain operators can be overloaded. Cannot overload the following operators: ?:..* :: sizeof

25 Overloading Types Of Operators ++, -- operators overloaded differently for prefix vs. postfix notation Overloaded relational operators should return a bool value Overloaded stream operators >>, << must return reference to istream, ostream objects and take istream, ostream objects as parameters

26 Overloaded [] Operator Can create classes that behave like arrays, provide bounds-checking on subscripts Must consider constructor, destructor Overloaded [] returns a reference to object, not an object itself

27 Object Conversion Type of an object can be converted to another type Automatically done for built-in data types Must write an operator function to perform conversion To convert an IntVal object to an int : IntVal::operator int() {return value;} Allows statements like: int num; IntVal val(2); num = val; // num now has 2

28 Object Composition Object composition: a class is a member of a class Supports the modeling of ‘has a’ relationship between classes – enclosing class ‘has a’ enclosed class Same notation as for structures within structures

29 Object Composition class StudentInfo { private: string firstName, LastName; string address, city, state, zip;... }; class Student { private: StudentInfo personalData;... };