Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 CSE 2341 Object Oriented Programming with C++ Note Set #12."— Presentation transcript:

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

2 2 Overview Inheritance Protected Access Specifier

3 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 4 Inheritance BASE CLASS DERIVED CLASS

5 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 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 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 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 = 100.0 / n; } FinalExam.h baseclass access specification

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

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

11 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 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 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 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 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 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 17 Order of Constructors/Destructors Base class constructor is called before the derived class constructor Destructors are called in reverse order

18 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 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 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 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 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 23 Fini ?


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

Similar presentations


Ads by Google