Wednesday, January 31, 2018 Announcements… For Today… For Next Time…

Slides:



Advertisements
Similar presentations
ITEC200 – Week03 Inheritance and Class Hierarchies.
Advertisements

George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Road Map Introduction to object oriented programming. Classes
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Inheritance and Class Hierarchies Chapter 3 Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Review of C++ Programming Part II Sheng-Fang Huang.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
CHAPTER 3 Inheritance and Class Hierarchies. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how C++
Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
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.
Chapter 9 Questions 1. What are the difference between constructors and member functions? 2. Design and implement a simple class as you want, with constructors.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
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.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Overview of C++ Polymorphism
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,
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Classes C++ representation of an object
Polymorphism, Virtual Methods and Abstract Classes
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
7. Inheritance and Polymorphism
Andy Wang Object Oriented Programming in C++ COP 3330
Polymorphism, Abstract Classes & Interfaces
Review: Two Programming Paradigms
More about OOP and ADTs Classes
Friday, January 26, 2018 Announcements… For Today… For Next Time…
Inheritance and Class Hierarchies
Lecture 23 Polymorphism Richard Gesick.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 14 Inheritance Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Separate Compilation and Namespaces
Introduction to Classes
3.3 Abstract Classes, Assignment, and Casting in a Hierarchy
Chapter 3 Introduction to Classes, Objects Methods and Strings
Java Programming Language
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Operator Overloading; String and Array Objects
More about OOP and ADTs Classes
Polymorphism, Abstract Classes & Interfaces
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
Tonga Institute of Higher Education
CISC/CMPE320 - Prof. McLeod
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
Overview of C++ Polymorphism
Java Programming Language
Submitted By : Veenu Saini Lecturer (IT)
2.1 Program Defects and "Bugs''
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Classes C++ representation of an object
Chapter 9 Introduction To Classes
Class rational part2.
14 – Sequential Containers
Presentation transcript:

Wednesday, January 31, 2018 Announcements… For Today… For Next Time… Inheritance Announcements… Lab 02: SNAP due Friday Questions? Zipped file contains ONLY .cpp, .h, .jpg, and .pdf files Code Reviews due before Friday Evening. "Poor" ratings from "Overall Rating" Private vs Protected discussion. Please include netID when reporting a problem! For Today… 3.3-6, pgs. 203-212 For Next Time… 2.1-2, pgs. 129-148

Attendance Quiz #9 Inheritance

Tip #10: Standard Integer typedefs Tip of the Day The actual size of integer types varies by implementation. The standard only requires size relations between the data types and minimum sizes for each data type, such that a long long is not smaller than long, which is not smaller than int, which is not smaller than short. As char's size is always the minimum supported data type, no other data types (except bit-fields) can be smaller. Starting with C99, the following pre-processor macros are defined in the header file <stdint.h>: int8_t int16_t int32_t int64_t signed integer type with width of exactly 8, 16, 32 and 64 bits respectively with no padding bits and using 2's complement for negative values (provided only if the implementation directly supports the type) uint8_t uint16_t uint32_t uint64_t unsigned integer type with width of exactly 8, 16, 32 and 64 bits respectively (provided only if the implementation directly supports the type)

3.3 Abstract Classes, Assignment, and Casting in a Hierarchy Referencing Actual Objects Summary of Features of Actual Classes and Abstract Classes Assignments in a Class Hierarchy Casting in a Class Hierarchy Case Study: Displaying Addresses for Different Countries 3.3, pgs. 203-206

Abstract Classes Inheritance An abstract class differs from an actual class (sometimes called a concrete class) in two respects: An abstract class cannot be instantiated. An abstract class declares at least one abstract member function, which must be defined in its derived classes. An abstract function is a virtual function that is declared but for which no body (definition) is provided. We use an abstract class in a class hierarchy when A base class can define attributes and functions that are common to derived classes. Actual derived classes may have unique as well as shared implementations. Pure abstract functions must be defined by derived concrete classes.

Abstract Class Food.h Inheritance #ifndef FOOD_H_ #define FOOD_H_ class Food { private: double calories; public: virtual double percent_protein() const = 0; virtual double percent_fat() const = 0; virtual double percent_carbohydrates() const = 0; double get_calories() const { return calories; } void set_calories(double cal) { calories = cal; } }; #endif These three virtual functions impose the requirement that all derived classes implement these three functions. We would expect a different function definition for each kind of food.

Abstract Functions Inheritance

Referencing Concrete Objects Inheritance Because class Food is abstract, we can’t create type Food objects, hence the following statement is invalid: Food my_snack(); // compile time error! We can use a type Food pointer variable to point to an actual object that belongs to a class derived from Food. If a Vegetable object is derived from Food, then Food is contained in Vegetable. Vegetable Food Thus a pointer to Food can be used to reference a Vegetable. The pointer variable my_snack (type pointer-to-Food) is legal: Food* my_snack = new Vegetable("carrot sticks");

Assignments in a Class Hierarchy Inheritance C++ is what is known as a strongly typed language. Operands have a type, and operations can be performed only on operands of the same or compatible types. This includes the assignment operation: l-value = r-value For the built-in types the r-value must be of the same type as the l- value, or there must be a conversion defined to convert the r-value into a value that is the same type as the l-value. For class types, the assignment operator may be overridden and overloaded. Pointer types are an exception: A pointer variable (l-value) that is of type pointer-to-base class may point to a derived object (r-value). However, the opposite is not legal: Computer* a_computer = new Lap_Top( ... ); // Legal Lap_Top* a_laptop = new Computer( ... ); // Illegal

Abstract vs Concrete Inheritance

Casting in a Class Hierarchy Inheritance Dynamic casting enables us to process objects referenced by my_computer through a pointer variable of its actual type, instead of through a type pointer-to-Computer : dynamic_cast<LapTop*>(my_computer) casts the type of the object pointed to by my_computer to type LapTop . The cast is called a downcast because we are casting from a higher type in the inheritance hierarchy (Computer) to a lower type (LapTop) . The operation succeeds only if the object referenced by my_computer is type LapTop. If the cast does not succeed, the result is the null pointer. Downcasting operations do not change the object pointed to by my_computer, but instead, creates a type LapTop pointing to it. This is called an anonymous or unnamed pointer. Microsoft compilers require you to set a compiler switch to use dynamic_cast.

Casting in a Class Hierarchy (cont.) Inheritance

Refactoring the Employee and Student Classes 3.4 Multiple Inheritance Refactoring the Employee and Student Classes 3.4, pgs. 210-212

Multiple Inheritance Inheritance Multiple inheritance refers the ability of a class to extend more than one class. In multiple inheritance, all the data fields for the derived class are inherited from its base classes. For example, a university has many students who are full- time students and many employees who are full-time employees, but also some student employees.

Definition of Student_Worker Inheritance There is no argument given for data fields hours or gpa, so they are initialized to default values #ifndef STUDENT_WORKER_H_ #define STUDENT_WORKER_H_ #include <string> #include "Employee.h" #include "Student.h" class Student_Worker : public Employee, public Student { public: Student_Worker(const std::string& the_name, Address* the_address, double the_rate, const std::string& the_major) : Employee(the_name, the_address, the_rate), Student(the_name, the_address, the_major) {} std::string to_string() const; }; #endif The heading shows that class Student_Worker extends class Employee and class Student The constructor contains two initialization expressions separated by a comma The first initializes the Employee part The second initializes the Student part

Refactoring Classes Inheritance A better solution than multiple inheritance is to recognize that both Employees and Students have common data fields. Common data fields and their associated member functions could be collected into a separate class, which would become a common base class Person. This process is known as refactoring and is often used in object-oriented design. However, it leads to similar problems, because there are two Person components in the Student_Worker class: one inherited from Employee and the other inherited from Student A workable solution to this problem is beyond the scope of the chapter you can find this solution in Appendix A.4.

SNAP UML Inheritance Student SNAP Csg Course Cr Csg Cdh SNAP Csg Cdh -int studID +int getStudID() +toString() const SNAP -string name -string address -string phone +string getName() +string getAddr() +string getPhone() Csg -string course -int studID -string grade +string getCourse() +int getStudID() +string getGrade() +toString() const Course Cr -string course -string room +string getCourse() +string getRoom() +toString() const Csg -string course -int studID -string grade +string getCourse() +int getStudID() +string getGrade() +toString() const Cdh -string course -string day -string hour +string getCourse() +string getDay() +string getHour() +toString() const SNAP -int studID -string name -string address -string phone +int getStudID() +string getName() +string getAddr() +string getPhone() +toString() const Csg -string course -int studID -string grade +string getCourse() +int getStudID() +string getGrade() +toString() const Cdh -string course -string day -string hour +string getCourse() +string getDay() +string getHour() +toString() const Cr -string course -string room +string getCourse() +string getRoom() +toString() const

3.5, pgs. 213-217 3.5 Namespaces and Visibility Namespaces Declaring a Namespace The Global Namespace The using Declaration and using Directive Using Visibility to Support Encapsulation The friend Declaration 3.5, pgs. 213-217

Namespaces Inheritance The entire C++ standard library is contained in the namespace std. Namespaces are used to group collections of declarations into a functional unit. Namespaces prevent clashes between duplicate names. An adventure game program may define the class map to represent the area where the action takes place. The standard library also defines a class called map, which associates values with keys (discussed in Chapter 9). To avoid clashes between the two map classes, we prefix the namespace name before the class name. #include <map> // Get defn of standard map class map { private: std::map the_map; // Declare a component. ... };

The Global Namespace Namespaces are nested: Inheritance Namespaces are nested: The global namespace is at the top level; its name is effectively the null string. By default symbols exist in a global namespace unless they are defined inside a block that starts with keyword namespace or members of a class or local variables of a function. In our map example, the distinction is between std::map and ::map. Since the reference to ::map is made within the default namespace, the leading prefix :: (the scope resolution operator) is not required. If we defined the namespace game but did not define the map class inside of it, code within the scope of namespace game that referenced class map, defined in the global namespace, would still require the :: qualifier, e.g., ::map.

using Declaration / Directive Inheritance The using declaration takes a name from the namespace in which it was declared and places it into the namespace where the using declaration appears: using std::cout; The qualified name (cout) is now a member of the namespace in which the using declaration appears. The using directive takes all of the names from a given namespace and places them into the namespace where the using directive appears: using namespace std; All of the names defines in std now are defined as such within the current namespace.

Declaring a Namespace (cont.) Inheritance

The friend Declaration Inheritance The friend declaration allows a function external to a class to access the private members of that class. The friend declaration gives the functions and classes it specifies access to the private and protected members of the class in which the friend declaration appears. This effectively makes the named function or class a member of the declaring class. The class itself must declare who its friends are. Friendship is not inherited and is not transitive - if a base class is a friend of a particular class, its derived classes are not automatically friends too.

The friend Declaration Inheritance class A { private: int x; friend foo; }; class B void foo() A a; a.x = 10; // Valid } class C void bar() a.x = 10; // Invalid! class A { private: int x; friend class B; }; class B int y; void foo() A a; a.x = 10; // Valid (B friend of A) } friend class C; class C void bar() B b; b.y = 10; // Valid (C friend of B) a.x = 10; // Invalid, C ~friend of A)

The Global Namespace (cont.) Inheritance The fragment below declares a class map in the global namespace and one in the game namespace class map { ... }; // Defined in the global namespace namespace game { class map { ... }; // Defined in the game namespace } The statement map map1; in the global namespace declares an object map1 of type ::map (that is, the map defined in the global namespace) game::map map2 in the global namespace declares an object map2 of type game::map (that is the map defined in namespace game)

The Global Namespace (cont.) Inheritance The statements namespace game { map map4; // map4 is the map defined in the game namespace. ::map map5; // map5 is the map defined in the global namespace. } are in the game namespace. The object map4 is of the map class that is declared in the game namespace The object map5 is of the type map defined in the global namespace