SFU CMPT Topic: Has-a Relationship

Similar documents
SFU CMPT Topic: Class Templates

SFU CMPT Topic: Classes

CMPT 135: Midterm Answer Key

SFU CMPT Topic: Function Objects

Program construction in C++ for Scientific Computing

Implementing Abstract Data Types (ADT) using Classes

Object-Oriented Design (OOD) and C++

Abstract Data Types (ADT) and C++ Classes

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

IS 0020 Program Design and Software Tools

Review: C++ Basic Concepts. Dr. Yingwu Zhu

IS0020 Program Design and Software Tools Midterm, Fall, 2004

Use the dot operator to access a member of a specific object.

Object-Oriented Programming in C++

pointers & references

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

C++ Important Questions with Answers

More Advanced Class Concepts

Object Oriented Software Design II

Classes. Christian Schumacher, Info1 D-MAVT 2013

OBJECT ORIENTED PROGRAMMING USING C++

Object Oriented Software Design II

Object-Oriented Principles and Practice / C++

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

ECE 462 Fall 2011, Second Exam

Intermediate Programming, Spring 2017*

Intermediate Programming & Design (C++) Classes in C++

Copy Constructor, Assignment, Equality

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

CS11 Intro C++ Spring 2018 Lecture 5

CS105 C++ Lecture 7. More on Classes, Inheritance

Vectors of Pointers to Objects. Vectors of Objects. Vectors of unique ptrs C++11. Arrays of Objects

14.1. Chapter 14: static member variable. Instance and Static Members 8/23/2014. Instance and Static Members

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

The Class Construct Part 2

SFU CMPT Topic: Control Statements

SFU CMPT Topic: Compound Types

struct Buffer { Buffer(int s) { buf = new char[s]; } ~Buffer() { delete [] buf; } char *buf; };

Object Oriented Programming: Inheritance Polymorphism

Module Operator Overloading and Type Conversion. Table of Contents

Financial computing with C++

Array Elements as Function Parameters

A <Basic> C++ Course

Programming Languages: OO Paradigm, Objects

A <Basic> C++ Course

Laboratory 7. Programming Workshop 2 (CSCI 1061U) Faisal Qureshi.

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

Operator overloading. Instructor: Bakhyt Bakiyev

Comp151. Generic Programming: Container Classes

Review. What is const member data? By what mechanism is const enforced? How do we initialize it? How do we initialize it?

Programming 2. Object Oriented Programming. Daniel POP

Object oriented programming

CSC1322 Object-Oriented Programming Concepts

Lecture 10: Introduction to Inheritance

Week 5-1: ADT Design

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

CPSC 427: Object-Oriented Programming

Overloaded Operators, Functions, and Students

Inheritance and Polymorphism

Instantiation of Template class

COMP322 - Introduction to C++

Object-Oriented Programming, Iouliia Skliarova

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

1.124 Quiz 1 Thursday October 8, 1998 Time: 1 hour 15 minutes Answer all questions. All questions carry equal marks.

And Even More and More C++ Fundamentals of Computer Science

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

ECE Fall 20l2, Second Exam

Absolute C++ Walter Savitch

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

CSC 210, Exam Two Section February 1999

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

C++ (classes) Hwansoo Han

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Introduction to C++ with content from

UEE1303(1070) S12: Object-Oriented Programming Advanced Topics of Class

CS250 Final Review Questions

CPSC 427: Object-Oriented Programming

C++ Inheritance and Encapsulation

Increases Program Structure which results in greater reliability. Polymorphism

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

CSE 333. Lecture 11 - constructor insanity. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

04-24/26 Discussion Notes

CS 247: Software Engineering Principles. ADT Design

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Programming C++ Lecture 3. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor

Programming, numerics and optimization

Lecture 02, Fall 2018 Friday September 7

Introduction & Review

Classes in C++98 and C++11

Dynamic memory in class Ch 9, 11.4, 13.1 & Appendix F

CSE 303: Concepts and Tools for Software Development

Inheritance and aggregation

B16 Object Oriented Programming

Classes and Objects in C++

Classes: Member functions // classes example #include <iostream> using namespace std; Objects : Reminder. Member functions: Methods.

CISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture:

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name:

Composition I. composition is a way to combine or compose multiple classes together to create new class

Transcription:

SFU CMPT-212 2008-1 1 Topic: Has-a Relationship SFU CMPT-212 2008-1 Topic: Has-a Relationship Ján Maňuch E-mail: jmanuch@sfu.ca Friday 22 nd February, 2008

SFU CMPT-212 2008-1 2 Topic: Has-a Relationship Has-a relationship public inheritence models is-a relationship Example: Student is a Person Q. how can we model has-a relationship? Example: Student has grades (intarray) 2 different ways: containment the owner class contains member objects of another class Example: 1 class Student { 2 char name[20]; 3 intarray grades; 4... 5 };

SFU CMPT-212 2008-1 3 Topic: Has-a Relationship private inheritence Example: 1 class Student : private intarray { 2... 3 }; In both cases the owner class (Student) inherits the implementation, but not the interface of the contained/base class: class methods can use public interface of the contained/base class; public interface of the contained/base class will not become part of public interface of the owner class.

SFU CMPT-212 2008-1 4 Topic: Has-a Relationship Recall declaration of class intarray: 1 class intarray { 2 int *data; 3 long size; 4 public: 5 intarray(long s); 6 intarray(const intarray &); // copy constructor 7 virtual intarray(); 8 intarray & operator=(const intarray & a); 9 // assignment operator 10 int get(long index) const; // get a value 11 void set(long index, int value); // set a value 12 friend ostream & operator<<(ostream &os, 13 const intarray &a); 14 };

SFU CMPT-212 2008-1 5 Topic: Has-a Relationship Example: student1.cpp 1 class Student { 2 char name[20]; 3 intarray grades; 4 public: Containment 5 Student(const char *n, int ngrades); 6 }; Q. how to initialize grades to the array of size ngrades? before executing the body of constructor, C++ creates all data members: for member objects, it calls the default constructor but intarray has no default constructor! A. similarly as data members of the base class, member objects can be initialized with non-default constructors through member initializer list

SFU CMPT-212 2008-1 6 Topic: Has-a Relationship Example: 1 Student::Student(const char *n, int ngrades) 2 : grades(ngrades) 3 { 4 strcpy(name,n); 5 } Notes: instead of class name we use directly the name of the member object any data members can be initialized in the same way: 1 class Point { 2 int x,y; 3 public: 4 Point(int xx, int yy) : x(xx), y(yy) {} 5 };

SFU CMPT-212 2008-1 7 Topic: Has-a Relationship Q. how to use interface of class intarray? as grades is a private data member of class Student, it cannot be accessed (as well as its interface) from outside of the class but it can be accessed in the methods of the class, hence class Student can make it available to the world : 1 class Student {... 2 int getgrade(long index) const 3 { return grades.get(index); } 4 void setgrade(long index, int value) 5 { grades.set(index,value); } 6 friend ostream & operator<<(ostream &os, 7 const Student &s); 8 }; 9 ostream & operator<<(ostream &os,const Student &s) { 10 os << "Student name: " << s.name << endl; 11 os << "Grades: " << s.grades <<endl; 12 return os; }

SFU CMPT-212 2008-1 8 Topic: Has-a Relationship Private inheritence Example: student2.cpp 1 class Student : private intarray 2 char name[20]; 3 public: 4 Student(const char *n, int ngrades); 5 }; Notes: public and protected members of the base class (intarray) become private members of the derived class (Student) models the has-a relationship very similar to containment method, but now the subobject grades has no name!

SFU CMPT-212 2008-1 9 Topic: Has-a Relationship Q. how to initialize intarray to the size ngrades? as with public inheritance, inherited subobjects can be initialized using non-default constructors through member initializer list Example: 1 Student::Student(const char *n, int ngrades) 2 : intarray(ngrades) 3 { 4 strcpy(name,n); 5 } now, we use the name the class of the contained subobject

SFU CMPT-212 2008-1 10 Topic: Has-a Relationship Q. how to use interface of class intarray? as public/protected members of intarray are private members of class Student, they cannot be accessed from outside of the class Student we have to make them available to the world : 1 class Student {... 2 int getgrade(long index) const 3 { return get(index); } 4 void setgrade(long index, int value) 5 { set(index,value); } 6 };

SFU CMPT-212 2008-1 11 Topic: Has-a Relationship Notes: sometimes it s desirable that the function of the base class which we are making available publicly has the same name as in the base class: 1 class Student {... 2 int get(long index) const 3 { return intarray::get(index); } 4 }; we have to use the scope resolution operator ::, otherwise we end up with infinite recursive call there is another way how to make function of the base class world-wide available: changing access using using-declaration: 1 class Student {... 2 using intarray::get; 3 using intarray::set; 4 }; note: no parameters!

SFU CMPT-212 2008-1 12 Topic: Has-a Relationship Q. what about friend functions? how to invoke a friend function from the base class? we cannot use the membership operator ( subobject has no name) we cannot use the scope resolution operator because a friend function does not belong to the class! A. type cast the Student argument to the base class: 1 ostream & operator<<(ostream &os,const Student &s) { 2 os << "Student name: " << s.name << endl; 3 os << "Grades: " << (const intarray &) s <<endl; 4 return os; 5 } Remark: no upcasting a pointer (reference) to privately derived class is not automatically converted to a pointer (reference) to the base class

SFU CMPT-212 2008-1 13 Topic: Has-a Relationship Containment vs. private inheritance Advantages: containment: named subobjects are easier to follow can include more subobjects of the same class easier to resolve identical names private inheritance: can access protected members of the included subobject can redefine virtual functions in general: use containment unless you need any of the two features provided by private inheritence

SFU CMPT-212 2008-1 14 Topic: Has-a Relationship Protected inheritance a variation of private inheritance, models the has-a relationship public and protected members of the base class become protected members of the derived class (this will affect only classes derived from the derived class)

SFU CMPT-212 2008-1 15 Topic: Has-a Relationship Sample questions What are the differences between public and private inheritance. What do they have in common? What is member initializer list? What can it contain? In which situations it is necessary to use the member initializer list? What s the problem with the following code? 1 class A { 2... 3 }; 4 class B : private A { 5... 6 }; 7... 8 B object; 9 A *p = &object; What are the two methods for modeling has-a relationship? What are advantages of each method?