1 CSE 2341 Object Oriented Programming with C++ Note Set #12.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Advertisements

Chapter 14 Inheritance Pages ( ) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Derived Classes. C++ 2 Outline  Definition  Virtual functions  Virtual base classes  Abstract classes. Pure virtual functions.
C++ Classes & Data Abstraction
Inheritance Inheritance-2. Inheritance Rewriting point and circle classes class Point { protected: int x,y; public: Point(int,int); void display(void);
Inheritance and Polymorphism- I Math 130 Introduction to Computer Programming Lecture #27 Monday, November 5, 2007.
Inheritance in C++ Multiple Base Classes Inheritance By Nouf Aljaffan Reference: Learn C++ from the master, Schildt, second Ed.
Inheritance in C++ Multiple Base Classes Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif.
CPSC 231 C++ Review1 Learning Objectives §Review of the object oriented design goals. §Review of C++ classes l data members l member functions (methods)
Inheritance, Polymorphism, and Virtual Functions.
Chapter 15 What is Inheritance?
General Computer Science for Engineers CISC 106 Lecture 34 Dr. John Cavazos Computer and Information Sciences 05/13/2009.
Starting Out with C++, 3 rd Edition 1 Chapter 15 – Inheritance, Polymorphism, and Virtual Functions.
Inheritance, Polymorphism, and Virtual Functions
Inheritance Chapter 11.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
1 Object-Oriented Programming Using C++ CLASS 27.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
1 CSE 2341 Object Oriented Programming with C++ Note Set #6.
Inheritance CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
1 Object-Oriented Programming Using C++ CLASS 11 Honors.
Inheritance One of the most powerful features of C++
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab:
Starting Out with C++, 3 rd Edition 1 Chapter 13 – Introduction to Classes.
Inheriatance. 9-2 What is Inheritance? Generalization vs. Specialization Real-life objects are typically specialized versions of other more general objects.
11 Introduction to Object Oriented Programming (Continued) Cats.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
More on Inheritance Chapter 11 Continued. Reminders Overloading – different signatures Overriding – same signatures Preventing overriding – use final.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
11-2  What Is Inheritance?  Calling the Superclass Constructor  Overriding Superclass Methods  Protected Members  Chains of Inheritance  The Object.
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley What Is Inheritance? 15.1.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
1 CSE 2341 Object Oriented Programming with C++ Note Set #7.
1.What are the differences between composition(“has a” relationship) and inheritance(“is a” relationship)? 2.What is a base class? 3.How to declare a derived.
Classes Sujana Jyothi C++ Workshop Day 2. A class in C++ is an encapsulation of data members and functions that manipulate the data. A class is a mechanism.
Starting Out with C++, 3 rd Edition 1 Chapter 13 – Introduction to Classes Procedural and Object-Oriented Programming Procedural programming is a method.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 8 1.
1 Object-Oriented Programming Using C++ CLASS 2 Honors.
1 Class 19 Chapter 13 – Creating a class definition.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
1 C++ Classes and Data Structures Course link…..
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Chapter 11 Inheritance. Contents I.What Is Inheritance? II. Calling the Superclass Constructor III. Overriding Superclass Methods IV. Protected Members.
1 CSE 2341 Object Oriented Programming with C++ Note Set #13.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Access Functions and Friend Functions
Classes.
Inheritance, Polymorphism, and Virtual Functions
Inheritance & Polymorphism
Chapter 5 Classes.
group work #hifiTeam
Andy Wang Object Oriented Programming in C++ COP 3330
Inheritance, Polymorphism, and Virtual Functions
Programming -2 برمجة -2 المحاضرة-7 Lecture-7.
C++ Inheritance.
Multiple Base Classes Inheritance
Introduction to Classes and Objects
Chapter 11: Inheritance Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Inheritance in C++ Inheritance Protected Section
Presentation transcript:

1 CSE 2341 Object Oriented Programming with C++ Note Set #12

2 Overview Inheritance Protected Access Specifier

3 What is INHERITANCE?? Inheritance –Allows a new class to be based upon an existing class –new class inherits member variables and public functions of the class it is based upon –new members may be added to the new class –“is a” relationship

4 Inheritance BASE CLASS DERIVED CLASS

5 Inheritance in C++ class GradedActivity { public: //members private: //members }; class FinalExam : public GradedActivity { public: //members private: //members }; Indicates that FinalExam inherits from GradedActivity

6 GradedActivity.h #ifndef GRADEDACTIVITY_H #define GRADEDACTIVITY_H // Grade class declaration class GradedActivity { private: char Letter; float Score; void determineGrade(); public: void SetScore(float S) { Score = S; determineGrade(); } float GetScore(void) { return Score; } char GetLetter(void) { return Letter; } }; #endif GradedActivity.h

7 #include “GradedActivity.h" void GradedActivity::determineGrade(void) { if (Score > 89) Letter = 'A'; else if (Score > 79) Letter = 'B'; else if (Score > 69) Letter = 'C'; else if (Score > 59) Letter = 'D'; else Letter = 'F'; } GradedActivity.cpp

8 FinalExam.h #ifndef FINALEXAM_H #define FINALEXAM_H // Must include GradedActivity // class declaration. #include “GradedActivity.h" class FinalExam : public GradedActivity { private: int NumQuestions; float PointsEach; int NumMissed; public: FinalExam(int n) { NumQuestions = n; PointsEach = / n; } FinalExam.h baseclass access specification

9 FinalExam.h float getPointsEach() { return PointsEach;} float setNumMissed (int); int getNumMissed() { return numMissed; } }; #endif; FinalExam.h

10 FinalExam.h #include “FinalExam.h” void FinalExam::setNumMissed(int n) { float numericScore; NumMissed = n; numericScore = – (n*PointsEach); setScore(numericScore); } FinalExam.cpp base class member function

11 FinalExam.h #include #include “FinalExam.h“ using namespace std; int main(void) { int Questions, Missed; cout << "How many questions are on the test? "; cin >> Questions; cout << "How many questions did the student miss? "; cin >> Missed; FinalExam test(Questions); setprecision(2); cout << “Each question counts “ << test.getPointsEach() << “points” << endl; cout << "The exam score is " << test.getScore() << endl; cout << “The exam grade is” << test.getLetter(); return 0; } gradeDriver.cpp

12 Protected Members and Class Access Private members of a base class cannot be accessed by a derived class Protected members of a base class are similar to private data members but are accessible to derived classes Base class access specification determines how public, private and protected members are inherited by the derived class

13 Protected Members #ifndef GRADEDACTIVITY_H #define GRADEDACTIVITY_H // Grade class declaration class GradedActivity { protected: // gives FinalExam class the right //of direct access char Letter; float Score; void determineGrade(void); public: void SetScore(float S) { Score = S; determineGrade();} float GetScore(void) { return Score; } char GetLetter(void) { return Letter; } }; #endif GradedActivity.h

14 Base Class Access Specification class derived: private base –private members of base are inaccessible to derived –protected members of base become private members of derived –public members of base become private members of derived

15 Base Class Access Specification class derived: protected base –private members of base are inaccessible to derived –protected members of base become protected members of derived –public members of base become protected members of derived

16 Base Class Access Specification class derived: public base –private members of base are inaccessible to the derived class –protected members of base become protected members of derived –public members of base become public members of derived

17 Order of Constructors/Destructors Base class constructor is called before the derived class constructor Destructors are called in reverse order

18 Demo //Demo Program #include class BaseDemo { public: BaseDemo() { cout << "This is the BaseDemo constructor.\n"; } ~BaseDemo() { cout << "This is the BaseDemo destructor.\n"; } }; class DeriDemo : public BaseDemo { public: DeriDemo() { cout << "This is the DeriDemo constructor.\n"; } ~DeriDemo() { cout << "This is the DeriDemo destructor.\n"; } };

19 Demo int main() { cout << "We will now declare a DeriDemo object.\n"; DeriDemo Object; cout << "The program is now going to end.\n"; return 0; } We will now declare a DeriDemo object. This is the BaseDemo constructor. This is the DeriDemo constructor. The program is now going to end. This is the DeriDemo destructor. This is the BaseDemo destructor.

20 Passing Arguments to Base Class Constructors Assume class cube is derived from class rectangle Constructor in Rectangle: Rectangle::Rectangle(int w, int l) { width = w; length = l; area = length * width; }

21 Passing Arguments to Base Class Constructors Assume class cube is derived from class rectangle Constructor in Cube class Cube::Cube(int l, int w, int h) :Rectangle(w, l) { height = h; volume = area * height; }

22 General Form :: (parameter list) : (parameter list) The derived class constructor must accept enough arguments to be able to pass to the base class constructor

23 Fini ?