System Design and Programming II

Similar documents
Inheritance, Polymorphism, and Virtual Functions

Chapter 15: Inheritance, Polymorphism, and Virtual Functions

What Is Inheritance? Generalization and Specialization

Appendix E: Using UML in Class Design

Chapter 11: Inheritance

Chapter 10: Inheritance

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

1. the base class s (default) constructor is executed first, 2. followed by the derived class s constructor

CSCE Practice Midterm. Data Types

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

CS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM

Class Example. student.h file: Declaration of the student template. #ifndef STUDENT_H_INCLUDED #define STUDENT_H_INCLUDED

Polymorphism Part 1 1

CSCE Practice Midterm. Data Types

1 Short Answer (10 Points Each)

System Design and Programming II

Due Date: See Blackboard

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

Midterm Exam 5 April 20, 2015

Lab 12 Object Oriented Programming Dr. John Abraham

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

CSCE 110 PROGRAMMING FUNDAMENTALS

UEE1303(1070) S12: Object-Oriented Programming Constant Pointer and Class

LAB 7.1 Working with One-Dimensional Arrays

CMSC 202 Midterm Exam 1 Fall 2015

CSCI 104 Polymorphism. Mark Redekopp David Kempe

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

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Lecture 7. Log into Linux New documents posted to course webpage

CS 162, Lecture 25: Exam II Review. 30 May 2018

Inheritance, and Polymorphism.

Programming in C/C Lecture 3

CS 247: Software Engineering Principles. ADT Design

Program Organization and Comments

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Sample Code: OUTPUT Daily Highs & Lows

Polymorphism CSCI 201 Principles of Software Development

Note: The buy help from the TA for points will apply on this exam as well, so please read that carefully.

Introduction to Programming using C++

Question 1 Consider the following structure used to keep employee records:

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

CSCI 1370 APRIL 26, 2017

Object Oriented Design

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.7. User Defined Functions II

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Due Date: See Blackboard

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FACULTY INFORMATION TECHNOLOGY AND COMMUNICATION (FTMK) BITE 1513 GAME PROGRAMMING I.

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

Due Date: See Blackboard

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

PIC 10A. Final Review: Part I

CS250 Final Review Questions

Absolute C++ Walter Savitch

Midterm Review. PIC 10B Spring 2018

Getting started with C++ (Part 2)

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Introduction to Programming

PIC 10A. Lecture 17: Classes III, overloading

Lab 2: ADT Design & Implementation

2. COURSE DESIGNATION: 3. COURSE DESCRIPTIONS:

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

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

CS250 Final Review Questions

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Final Exam. Name: Student ID: Section: Signature:

Due Date: See Blackboard

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

CSCE 2004 Midterm Exam Spring 2017

ECE 462 Exam 1. 6:30-7:30PM, September 22, 2010

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

The American University in Cairo Department of Computer Science & Engineeringt CSCI &09 Dr. KHALIL Exam-I Fall 2009

Lab 15 Review of Arrays, Array of Objects and Vector Dr. John Abraham, Professor

Sonoma State University Computer Science Department CS 115 Fall 2010 Watts. Project 3 Part 1

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Intermediate Programming, Spring 2017*

CE221 Programming in C++ Part 1 Introduction

private: // can only be used by the member functions of MyType and friends of MyType };

Abstraction in Software Development

Object-Oriented Programming (OOP) Fundamental Principles of OOP

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

More Tutorial on C++:

A A B U n i v e r s i t y

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Review Questions for Final Exam

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

COMP322 - Introduction to C++ Lecture 01 - Introduction

CS212 - COMPUTATIONAL STRUCTURES AND ALGORITHMS

CS 1337 Computer Science II Page 1

Constants, References

University of Michigan EECS 183: Elem. Programming Concepts Fall 2011 Exam 1: Part 1: Form 1. Professors: ML Dorf, Elliot Soloway

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

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

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Constructor - example

Transcription:

System Design and Programming II CSCI 194 Section 01 CRN: 20220 Spring 2016 David L. Sylvester, Sr., Assistant Professor

Chapter 15 Inheritance, Polymorphism, And Virtual Functions

What is Inheritance? Inheritance allows a new class to be based on an existing class. The new class inherits all the member variables and functions (except the constructors and destructors) of the class it is based on. Inheritance and the is a Relationship When one object is a specialized version of another object, there is an is a relationship between them. Ex: A poodle is a dog. A car is a vehicle. A tree is a plant. A rectangle is a shape. A football player is an athlete. When a is a relationship exists between classes, it means that the specialized class has all of the characteristics of the general class, plus additional characteristics, that make it special.

Inheritance involves a base class and a derived class. The base class is the general class and the derived class is the specialized class. The derived class inherits the member variables and member functions of the base class without any of them being rewritten. Furthermore, new member variables and functions may be added to the derived class to make it more specialized than the base class. The following page show the GradedActivity class that has the general characteristics of a student s graded activity. Many different types of graded activities exist, such as quizzes, midterm exams, final exams, lab reports, essays, and so on. Because the numeric scores might be determined differently for each of the graded activities, we can create derived classes to handle each one. Therefore, a FinalExam class can be derived from the Graded Activity class. The FinalExam class has member variables for the number of questions, the number of points each question is worth, and the number of questions missed.

To declare the class FinalExam, you would do the following: class FinalExam : public GradedActivity Class being declared (the derived class) Base class If we want to express the relationship between the two classes, we can say that a FinalExam is a GradedActivity. The word public, which precedes the name of the base class is the bass class access specification. It affects how the members of the base class are inherited by the derived class. When you create an object of a derived class, you can think of it as being built on top of the base class. The members of the base class object become member of the derived class object. How the base class members appear in the derived class is determined by the base class access specification.

The GradedActivity class has both private members and public members. The FinalExam class is derived from the GradedActivity class, using public access specification. This means that the public members of the GradedActivity class will become public members of the FinalExam class. The private members of the GradedActivity class cannot be accessed directly by code in the FinalExam class. Although the private members of the GradedActivity class are inherited, it s as though they are invisible to the code in the FinalExam class. They can only be accessed by the member functions of the GradedActivity class.

#include <iostream> using namespace std; Sample Program class GradedActivity private: double score; // To hold the numeric score public: // Default constructor GradedActivity() score = 0.0; } // Constructor GradedActivity(double s) score = s; } // Mutator function void setscore(double s) score = s; } // Accessor functions double getscore() const return score; } char getlettergrade() const; }; Program Output with example input Shown in Bold Enter your numeric test score: 80 [Enter] The grade for that test is B Program Output with Different Examples Input shown in Bold Enter your numeric test score: 75 [Enter] The grade for that test is C char GradedActivity::getLetterGrade() const char lettergrade;// To hold the letter grade if (score > 89) lettergrade = 'A'; else if (score > 79) lettergrade = 'B'; else if (score > 69) lettergrade = 'C'; else if (score > 59) lettergrade = 'D'; else lettergrade = 'F'; return lettergrade; } int main() double testscore;// To hold a test score // Create a GradedActivity object for the test. GradedActivity test; // Get a numeric test score from the user. cout << "Enter your numeric test score: "; cin >> testscore; // Store the numeric score in the test object. test.setscore(testscore); // Display the letter grade for the test. cout << "The grade for that test is " << test.getlettergrade() << endl; return 0; }

Polymorphism (overloading) GRADEDACTIVITY (.h) GRADEDACTIVITY (. cpp) inherited #ifndef GRADEDACTIVITY_H #define GRADEDACTIVITY_H // GradedActivity class declaration class GradedActivity private: double score; // To hold the numeric score public: // Default constructor GradedActivity() score = 0.0; } // Constructor GradedActivity(double s) score = s; } // Mutator function void setscore(double s) score = s; } // Accessor functions double getscore() const return score; } char getlettergrade() const; }; #endif #include "C:\Users\David\Documents\Visual Studio 2010\Projects\chapter15b\chapter15b\GradedActivity.h" // // Member function GradedActivity::getLetterGrade // char GradedActivity::getLetterGrade() const char lettergrade;// To hold the letter grade if (score > 89) lettergrade = 'A'; else if (score > 79) lettergrade = 'B'; else if (score > 69) lettergrade = 'C'; else if (score > 59) lettergrade = 'D'; else lettergrade = 'F'; return lettergrade; }

MAIN PROGRAM #include <iostream> #include "GradedActivity.h" using namespace std; (.cpp) Program Output with example input Shown in Bold Enter your numeric test score: 80 [Enter] The grade for that test is B int main() double testscore;// To hold a test score // Create a GradedActivity object for the test. GradedActivity test; Program Output with Different Examples Input shown in Bold Enter your numeric test score: 75 [Enter] The grade for that test is C // Get a numeric test score from the user. cout << "Enter your numeric test score: "; cin >> testscore; // Store the numeric score in the test object. test.setscore(testscore); // Display the letter grade for the test. cout << "The grade for that test is " << test.getlettergrade() << endl; return 0; }

Ex: FinalExam class Private members: Public members: int numquestions double pointseach int nummissed FinalExam() FinalExam(int, int) set(int, int) getnumquestions() getpointseach() getnummissed() setscore() getscore() getlettergrade() declared in the FinalExam class declared in the FinalExam class declared in the FinalExam class defined in the FinalExam class defined in the FinalExam class defined in the FinalExam class defined in the FinalExam class defined in the FinalExam class defined in the FinalExam class inherited from GradedActivity inherited from GradedActivity inherited from GradedActivity

FINALEXAM (.h) FINALEXAM (.cpp) #ifndef FINALEXAM_H #define FINALEXAM_H #include "GradedActivity.h" class FinalExam : public GradedActivity private: int numquestions; // Number of questions double pointseach; // Points for each question int nummissed; // Number of questions missed public: // Default constructor FinalExam() numquestions = 0; pointseach = 0.0; nummissed = 0; } // Constructor FinalExam(int questions, int missed) set(questions, missed); } // Mutator function void set(int, int); // Defined in FinalExam.cpp // Accessor functions double getnumquestions() const return numquestions; } double getpointseach() const return pointseach; } int getnummissed() const return nummissed; } }; #endif Declares FinalExam class derived from GradedActivity #include "FinalExam.h" // // Set function // The parameters are the number of questions and the // number of questions missed. void FinalExam::set(int questions, int missed) double numericscore;// To hold the numeric score // Set the number of questions and number missed numquestions = questions; nummissed = missed; // Calculate the points for each question. pointseach = 100.0 / numquestions; // Calculate the numeric score for this exam. numericscore = 100.0 - (missed * pointseach); // Call the inherited setscore function to set // the numeric score. setscore(numericscore); }

MAIN PROGRAM (.cpp) Program Output with Example Input Shown in Bold #include <iostream> #include <iomanip> #include "FinalExam.h" using namespace std; int main() int questions;// Number of question on the exam int missed;// Number of questions missed by the student // Get the number of questions on the final exam. cout << "How many questions are on the final exam? "; cin >> questions; // Get the number of questions the student missed. cout << "How many questions did the student miss? "; cin >> missed; // Define a FinalExam object and initialize it with // the values entered. FinalExam test(questions, missed); // Display the test results. cout << setprecision(2); cout << "Product =: " << setprecision(3) << test.getproduct() << " " << endl; cout << "\neach question counts " << test.getpointseach() << " points.\n"; cout << "The exam score is " << test.getscore() << endl; cout << "The exam gade is " << test.getlettergrade() << endl; return 0; } How many questions are on the final exam? 20 [Enter] How many questions did the student miss? 3 [Enter] Each question counts 5 points. The exam score is 85 The exam grade is B

Overloading Constructors The polymorphism feature of an object-oriented language allows the programmer to provide multiple definitions of an operator or for a function. Another term used to refer to polymorphism is overloading. More than one function may have the same name, if the argument list is different. When the program is compiled the appropriate occurrence of the function will be linked base on the data type that is used in the call to the function. Therefore it is possible to have multiple constructor functions within a single class. Ex: // Default constructor GradedActivity() score = 0.0; } // Constructor GradedActivity(double s) score = s; } Notice that both constructor functions have the same name GradedActivity(), the difference is within the parentheses. When an object is declared, the contents in the parentheses determine which function is called.

There may also be multiple functions with arguments, but the arguments are of different types or have a different number of arguments for overloaded functions. Ex: GradedActivity(); GradedActivity(float); GradedActivity(int); GradedActivity(float, int); GradedActivity(int, float); All of the above are different and could be used within a single class or independently of classes. A function may be redefined with different argument lists using the same function name. It is possible to have different return types. However, the return types cannot be the only differing factor in the function declarations because the compiler would not be able to distinguish between the two functions at the point of the function call.