Friends of Template Classes

Slides:



Advertisements
Similar presentations
The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Advertisements

1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
1 Inheritance and Polymorphism Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
Chapter 12: Adding Functionality to Your Classes.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
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.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Object Oriented Programming
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.
Introduction to Object-Oriented Programming Lesson 2.
Overview of C++ Polymorphism
OBJECT ORIENTED PROGRAMMING. Design principles for organizing code into user-defined types Principles include: Encapsulation Inheritance Polymorphism.
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
1 C++ Classes & Object Oriented Programming Overview & Terminology.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Object Lifetimes and Dynamic Objects Lecture-7. Object Lifetimes and Dynamic Objects External (Global) Objects Persistent (in existence) throughout the.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Chapter 2 Objects and Classes
Eine By: Avinash Reddy 09/29/2016.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Classes (Part 1) Lecture 3
Ch 10- Advanced Object-Oriented Programming Features
Final and Abstract Classes
Inheritance and Polymorphism
References as Function Arguments
Inheritance and Run time Polymorphism
Inheritance & Polymorphism
Review: Two Programming Paradigms
Object Lifetime and Dynamic Objects
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance Often, software encapsulates multiple concepts for which some attributes/behaviors overlap E.g. A computer (role-playing) game has: Monsters:
CS212: Object Oriented Analysis and Design
Chapter 9 Inheritance and Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Learning Objectives Inheritance Virtual Function.
CS212: Object Oriented Analysis and Design
Lecture 22 Inheritance Richard Gesick.
Week 6 Object-Oriented Programming (2): Polymorphism
Inheritance and Polymorphism
Polymorphism Polymorphism
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Fundaments of Game Design
Chapter 11: Inheritance and Composition
Overview of C++ Polymorphism
CIS 199 Final Review.
CS410 – Software Engineering Lecture #5: C++ Basics III
CPS120: Introduction to Computer Science
Final and Abstract Classes
Lecture 10 Concepts of Programming Languages
CS410 – Software Engineering Lecture #8: Advanced C++ III
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
C++ Object Oriented 1.
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Computer Science II for Majors
Presentation transcript:

Friends of Template Classes Template classes can contain friends. A friend function that does not use a template specification is universally a friend of all instantiations of the template class. A friend function that incorporates template arguments is specifically a friend of its instantiated class. November 3, 2016 CS410 – Software Engineering Lecture #16: Advanced C++ II

Friends of Template Classes Example: template <class T> class Matrix { public: friend void Foo(); // universal friend vect<T> Product(vect<T> v); // instantiated … }; November 3, 2016 CS410 – Software Engineering Lecture #16: Advanced C++ II

CS410 – Software Engineering Lecture #16: Advanced C++ II Template Arguments You can also use constant expressions, function names, and character strings as template arguments. Example: template <class T, int n> class AssignArray { public: T a[n]; }; AssignArray<double, 50> x, y; x = y; November 3, 2016 CS410 – Software Engineering Lecture #16: Advanced C++ II

Default Template Arguments In the standard library, the class complex is now a template class. Usually, we would instantiate it to double: complex<double> x, y, z[10]; A template provider could decide that this is such a common case that it should be provided as a default. To achieve this, we can use default template arguments. November 3, 2016 CS410 – Software Engineering Lecture #16: Advanced C++ II

Default Template Arguments Example: template<class T = double> class complex { … private: T real, imaginary; }; complex<> c; // same as complex <double> c; November 3, 2016 CS410 – Software Engineering Lecture #16: Advanced C++ II

CS410 – Software Engineering Lecture #16: Advanced C++ II Inheritance A class can be derived from an existing class by using the following form: class class-name:(public|protected|private) base-name { member declarations }; The keywords public, protected, and private are used to specify how the base-class members can be accessed by the derived class. Public derivation is far more important than private or protected derivation; it should be considered the normal form of inheritance. November 3, 2016 CS410 – Software Engineering Lecture #16: Advanced C++ II

CS410 – Software Engineering Lecture #16: Advanced C++ II Inheritance Example: class Student { public: enum year {fresh, soph, junior, senior, grad}; Student(string nm, int id, double g, year y); void Print() const; protected: string name; int student_id; double gpa; year yr; }; November 3, 2016 CS410 – Software Engineering Lecture #16: Advanced C++ II

CS410 – Software Engineering Lecture #16: Advanced C++ II Inheritance class GradStudent : public Student { public: enum support {ta, ra, fellowship, other}; GradStudent(string nm, int id, double g, year y, support s, string d, string th); void Print() const; protected: support supp; string dept; string thesis; }; November 3, 2016 CS410 – Software Engineering Lecture #16: Advanced C++ II

CS410 – Software Engineering Lecture #16: Advanced C++ II Inheritance In the preceding example, GradStudent is the derived class, and Student is the base class. Using the keyword public following the colon in the derived-class header has several effects: The protected and public members of Student are inherited as protected and public members, resp., of GradStudent. Private members are inaccessible. GradStudent is a subtype of Student. A GraduateStudent is a Student, but a Student does not have to be a GraduateStudent (is-a relationship, or interface inheritance). November 3, 2016 CS410 – Software Engineering Lecture #16: Advanced C++ II

CS410 – Software Engineering Lecture #16: Advanced C++ II Inheritance A derived class is a modification of the base class, inheriting the public and protected members of the base class. Notice: Only constructors, destructors, and the member function operator=() cannot be inherited. Frequently, a derived class adds new members to the existing class members. It is also possible to override existing class members. Notice: Overriding is different from overloading, in which the same function name can have different meanings for each unique signature. November 3, 2016 CS410 – Software Engineering Lecture #16: Advanced C++ II

CS410 – Software Engineering Lecture #16: Advanced C++ II Inheritance Benefits of using a derived class: Code is reused: GradStudent uses existing, tested code from Student. The hierarchy reflects a relationship found in the problem domain. In the real world, graduate students make up a subgroup of all students. Various polymorphic mechanisms will allow client code to treat GradStudent as a subtype of Student, which simplifies the code but keeps distinctions among subtypes. November 3, 2016 CS410 – Software Engineering Lecture #16: Advanced C++ II

Typing Conversions and Visibility A variable of a publicly derived class can in many ways be treated as if it were the base-class type. For example, a pointer whose type is pointer to base class can point to objects that have the derived-class type. To examine the properties of base classes and derived classes, let us first take a closer look at our examples Student and GradStudent. November 3, 2016 CS410 – Software Engineering Lecture #16: Advanced C++ II

Typing Conversions and Visibility Implementation of constructors: Student::Student(string nm, int id, double g, year y) : name(nm), student_id(id), gpa(g), yr(y) {} GradStudent::GradStudent(string nm, int id, double g, year y, support s, string d, string th) : Student(nm, id, g, y), supp(s), dept(d), thesis(th) Notice: The constructor of Student is invoked as part of the initializer list in the constructor of GradStudent. November 3, 2016 CS410 – Software Engineering Lecture #16: Advanced C++ II

Typing Conversions and Visibility Because GradStudent is a subtype of Student, a reference to the derived class GradStudent may be implicitly converted to a reference to the public base class Student. Example: GradStudent gst(“John Miller”, 31416, 3.99, grad, ra, “Computer Science”, “Eye Movements in the Dark”); Student &st = gst; November 3, 2016 CS410 – Software Engineering Lecture #16: Advanced C++ II

CS410 – Software Engineering Lecture #16: Advanced C++ II Inheritance class GradStudent : public Student { … }; Using the keyword public following the colon in the derived-class header has several effects: The protected and public members of Student are inherited as protected and public members, resp., of GradStudent. Private members are inaccessible. GradStudent is a subtype of Student. A GraduateStudent is a Student, but a Student does not have to be a GraduateStudent (is-a relationship, or interface inheritance). November 3, 2016 CS410 – Software Engineering Lecture #16: Advanced C++ II

CS410 – Software Engineering Lecture #16: Advanced C++ II Inheritance class GradStudent : private Student { … }; Using the keyword private following the colon in the derived-class header has several effects: The protected and public members of Student are inherited as private members of GradStudent. However, GradStudent can re-declare protected members of Student as protected… …and public members of Student as either public or protected. Private members of Student are inaccessible by GradStudent. GradStudent is not a subtype of Student. November 3, 2016 CS410 – Software Engineering Lecture #16: Advanced C++ II

CS410 – Software Engineering Lecture #16: Advanced C++ II Inheritance class GradStudent : protected Student { … }; Using the keyword protected following the colon in the derived-class header has several effects: The public and protected members of Student are inherited as protected members of GradStudent. However, GradStudent can re-declare public members of Student as public. Private members of Student are inaccessible by GradStudent. GradStudent is not a subtype of Student. November 3, 2016 CS410 – Software Engineering Lecture #16: Advanced C++ II

Typing Conversions and Visibility Example for pointer conversions: int main() { Student s(“Joe Smith”, 111, 2.57, student::fresh); Student *ps = &s; GradStudent gs(“John Miller”, 31416, 3.99, student::grad, ra, “Computer Science”, “Eye Movements in the Dark”); GradStudent *pgs; ps->Print(); ps = pgs = &gs; pgs->Print(); } // Student::Print() // GradStudent::Print() // Student::Print() November 3, 2016 CS410 – Software Engineering Lecture #16: Advanced C++ II

CS410 – Software Engineering Lecture #16: Advanced C++ II Virtual Functions Overloaded member functions are invoked by a type-matching algorithm. These types are known at compile time and allow the compiler to select the appropriate member directly. As you will see, it would be nice to dynamically select at runtime the appropriate member function from among base- and derived-class functions. Such a mechanism is provided by the keyword virtual; it may be used only to modify member function declarations. Virtual functions combined with public inheritance are a form of pure polymorphism. November 3, 2016 CS410 – Software Engineering Lecture #16: Advanced C++ II

CS410 – Software Engineering Lecture #16: Advanced C++ II Virtual Functions When a virtual function is invoked, its semantics are the same as those of other functions. In a derived class, a virtual function can be overridden by a another function with a matching signature. The selection of which function definition to invoke for a virtual function is dynamic. A pointer to base class can point at either a base-class object or a derived-class object. The member function selected will depend on the class of the object being pointed at, not on the pointer type. November 3, 2016 CS410 – Software Engineering Lecture #16: Advanced C++ II

CS410 – Software Engineering Lecture #16: Advanced C++ II Virtual Functions Note the difference in selection of the appropriate overridden virtual function from an overloaded member function: An overloaded member function is selected at compile time, based on its argument types, and it can have distinct return types. A virtual function is selected at runtime, based on the object’s type, which is passed to it as its this pointer argument. Once a function is declared virtual, this property is automatically carried along to all redefinitions in derived classes. November 3, 2016 CS410 – Software Engineering Lecture #16: Advanced C++ II

CS410 – Software Engineering Lecture #16: Advanced C++ II Virtual Functions Example: Virtual function selection class B { public: int i; virtual void Print() const { cout << i << “ inside B” << endl; } }; class D : public B void Print() const { cout << i << “ inside D “ << endl; } int main() { B b; B *pb = &b; D d; d.i = 1 + (b.i = 1); pb->Print(); pb = &d; } Output: 1 inside B 2 inside D November 3, 2016 CS410 – Software Engineering Lecture #16: Advanced C++ II

CS410 – Software Engineering Lecture #16: Advanced C++ II Virtual Functions In the Student/GradStudent example, the selection of Print() is based on the pointer type, known at compile time. In the current (B/D) example, selection is based on what is being pointed at. Here, the pointer’s base type is not determining the function selection. Instead, different class objects are processed by different functions, determined at runtime. November 3, 2016 CS410 – Software Engineering Lecture #16: Advanced C++ II

CS410 – Software Engineering Lecture #16: Advanced C++ II Virtual Functions Example: class Shape { public: virtual double Area() const { return 0; } protected: double x, y; }; class Rectangle : public Shape double Area() const { return (height*width); } private: double height, width; class Circle : public Shape { public: double Area() const { return (PI*radius*radius); } private: double radius; }; int main() … Shape *s[N]; for (int i = 0; i < N; i++) totArea += s[i]->Area(); } November 3, 2016 CS410 – Software Engineering Lecture #16: Advanced C++ II

CS410 – Software Engineering Lecture #16: Advanced C++ II Virtual Functions Notice: The declaration of an identifier in a scope hides all declarations of that identifier in outer scopes. A base class is an outer scope of any class derived from it. This rule is independent of whether the names are declared virtual or not. If the selected function is inaccessible, we get a compile-time error. November 3, 2016 CS410 – Software Engineering Lecture #16: Advanced C++ II

CS410 – Software Engineering Lecture #16: Advanced C++ II Virtual Functions We can define abstract base classes that include pure virtual functions (also called deferred methods). These are declared as follows: virtual function prototype = 0; An abstract base class specifies the basic common properties of its derived classes but cannot itself be used to declare objects. It is used to declare pointers that can access subtype pointers derived from the abstract class. November 3, 2016 CS410 – Software Engineering Lecture #16: Advanced C++ II

CS410 – Software Engineering Lecture #16: Advanced C++ II Design Patterns As software engineers, we commonly have to make decisions about how to create complex objects, how to encapsulate some actions, how to allow for the undoing of certain operations, etc. Instead of developing individual solutions every time, it is useful to apply standard design patterns. The patterns have been used and tested by many other software engineers. Moreover, they are known by many software engineers and well documented in literature so that using them will make it easier for other programmers to understand your code. November 3, 2016 CS410 – Software Engineering Lecture #16: Advanced C++ II

CS410 – Software Engineering Lecture #16: Advanced C++ II Design Patterns It typically takes programmers some time to get proficient with design patterns. Once they get used to these techniques, their productivity increases. There are entire catalogs of design patterns with formal specifications including pattern name, description, etc. We will look at several important and common design patterns in a less formal way. November 3, 2016 CS410 – Software Engineering Lecture #16: Advanced C++ II

The Singleton Design Pattern The singleton design pattern is a simple creational pattern, i.e., it applies to the creation of objects. This pattern can be used when at most one instance of a particular object is allowed to exist at any given time. For example, there should only be one mouse cursor object. The singleton design pattern prevents direct access to the object constructors but provides an instance() method for calling member operations. When called for the first time, the instance() method will create the only instance of the object. November 3, 2016 CS410 – Software Engineering Lecture #16: Advanced C++ II

The Singleton Design Pattern class Singleton { public: static Singleton* instance(); void op1(); protected: Singleton(); Singleton(const Singleton&); Singleton& operator=(const Singleton&); private: static Singleton* onlyInstance_; }; Singleton* Singleton::onlyInstance_ = 0; Singleton* Singleton::instance() { if (onlyInstance_ == 0) onlyInstance_ = new Singleton; return onlyInstance_; } Returns pointer to the only Singleton object. Uses lazy evaluation (instance is not created unless explicitly requested). Operations should be accessed through instantiating operation: Singleton::instance()->op1(); Constructors are protected, so the following is impossible: Singleton* s = new Singleton(); Pointer to the only instance or 0 if no instance exists. Could easily be modified to allow at most n instances. November 3, 2016 CS410 – Software Engineering Lecture #16: Advanced C++ II