3.3 Abstract Classes, Assignment, and Casting in a Hierarchy

Slides:



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

Polymorphism, Virtual Methods and Abstract 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.
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
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.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 12: Adding Functionality to Your Classes.
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
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
CHAPTER 3 Inheritance and Class Hierarchies. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how C++
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.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
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.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
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.
Classes, Interfaces and Packages
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
C++ Namespaces, Exceptions CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
A First Book of C++ Chapter 12 Extending Your Classes.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Design issues for Object-Oriented Languages
Chapter 2 Objects and Classes
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Programming with ANSI C ++
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
7. Inheritance and Polymorphism
Polymorphism, Abstract Classes & Interfaces
Object-Oriented Design (OOD) and C++
CS3340 – OOP and C++ L. Grewe.
Polymorphism &Virtual Functions
Review: Two Programming Paradigms
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Polymorphism, Virtual Methods and Abstract Classes
Friday, January 26, 2018 Announcements… For Today… For Next Time…
Inheritance and Class Hierarchies
Wednesday, January 31, 2018 Announcements… For Today… For Next Time…
1.4 Abstract Data Types, Interfaces, and Pre- and Postconditions
Lab 02 - SNAP.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Introduction to Classes
Polymorphism, Abstract Classes & Interfaces
Chapter 11: Inheritance and Composition
1.4 Abstract Data Types, Interfaces, and Pre- and Postconditions
Java Programming Language
2.1 Program Defects and "Bugs''
2.6, pgs Efficiency of Algorithms Big-O Notation
Presentation transcript:

3.3 Abstract Classes, Assignment, and Casting in a Hierarchy 3.4 Multiple Inheritance 09 – Inheritance

Attendance Quiz #9 Inheritance

Retrieves full path of command line arguments Tip #9: Directory Path Inheritance #include <iostream> #include <fstream> #include <stdlib.h> using namespace std; int main(int argc, char* argv[]) { char full[_MAX_PATH]; _fullpath(full, ".\\", _MAX_PATH); cout << "Place input/output files in the following directory:"; cout<< endl << full << endl; } ifstream in(argv[1]); // try to open argv[1] if (in) cout << "Successfully found \"" << argv[1] << " \""; in.close(); else cout << "Unable to find file \"" << argv[1] << " \""; return 0; Retrieves full path of command line arguments Place input/output files here: C:\Users\proper\Dropbox\BYU\CS 235\Visual Studio Examples\PrintFullPath\ Successfully found "fullPath.cpp "

Lab 02 - SNAP

S.N.A.P Lab Lab 02 - SNAP Class design is type design - defining effective classes can be challenging. For the SNAP lab you are to design classes for a school database with has-a and is-a relationships that have natural syntax, intuitive semantics, and efficient memory allocation. The class objects are populated from parsed input strings. Use object inheritance, polymorphism, and function and operator overloading where needed. All member data and internal functions are private. All classes have a "toString" method with friends for the insertion ("<<") operator. Use UML diagrams to describe your implementation.

Student # Name Address Phone # Course Student # Grade Course Day Hour Input Error Course Room

vector<Snap> snaps; vector<Csg> csgs; vector<Cdh> cdhs; vector<Cr> crs; string toString() const { stringstream out; out << "snap(" << this->studentId; out << "," << this->studentName; out << "," << this->studentAddress; out << "," << this->studentPhone << ")"; return out.str(); } friend std::ostream& operator<< (ostream& os, const Snap& snap) os << snap.toString(); return os;

UML Summary Lab 02 - SNAP

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 abstract virtual functions impose the requirement that all derived classes implement these functions. (We would expect a different function definition for each kind of food.) A pure virtual function is specified in the class definition by using "= 0;:" in place of the function body.

Referencing Concrete Objects Inheritance Because class Food is abstract, we can’t create type Food objects, hence the following statement is invalid: Food mySnack(); // 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 mySnack (type pointer-to-Food) is legal: Food* mySnack = 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* computer = new LapTop( ... ); // Legal LapTop* laptop = new Computer( ... ); // Illegal

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 StudentWorker : public Employee, public Student { public: Student_Worker(const std::string& name, Address* address, double rate, const std::string& major) : Employee(name, address, rate), Student(name, address, major) {} std::string to_string() const; }; #endif The heading shows that class StudentWorker 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 StudentWorker 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.

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)

Follow up… Inheritance class MyClass { private: string name; public: MyClass(const string name) { this->name = name; } ~MyClass() = default; friend std::ostream& operator<< (ostream& lhs, const MyClass& rhs) lhs << "MyClass(" << name << ")"; return lhs; } }; class MyClass { private: string name; public: MyClass(const string name) { this->name = name; } ~MyClass() = default; string toString() const ostringstream out; out << "MyClass(" << name << ")"; return out.str(); } }; std::ostream& operator<< (ostream& lhs, const MyClass& rhs) lhs << rhs.toString(); return lhs; A friend function of a class has the same access privileges to private and protected data as the class member functions. A friend declaration is not needed if using the public toString member function.