計算機概論實習 2007-04-13. 2 Class Sample: RECT Class weight length class RECT{ private: int weight, length; public: RECT(); int surface(); int perimeter();

Slides:



Advertisements
Similar presentations
Outline 1 Introduction 2 Base Classes and Derived Classes 3 protected Members 4 Relationship between Base Classes and Derived Classes 5 Case Study: Three-Level.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Reusable Classes.  Motivation: Write less code!
OOP: Inheritance By: Lamiaa Said.
CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
C++ Inheritance Systems Programming.
Inheritance CT Introduction 2  Inheritance  Software reusability  Create new class from existing class  Absorb existing class’s data and behaviors.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
計算機概論實習 What is Class In C++, class is the keyword which means encapsulation. Property and method are used to define what is class. Materialize.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
計算機概論實習 Last Practice (P4) members StudentTeacher private: char name[80]; protected: int number; public: void setName(char *); void getName(char*);
Inheritance Is a form of software reusability in which programmers create classes that absorb an existing class’s data and behaviors and enhance them with.
Object Oriented Programming: Inheritance Chapter 9.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
1 CSC241: Object Oriented Programming Lecture No 13.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
Operator Overloading Operator Overloading allows a programmer to define new types from the built-in types. –Operator Overloading is useful for redefining.
Peyman Dodangeh Sharif University of Technology Fall 2014.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
Programming Fundamentals1 Chapter 8 OBJECT MANIPULATION - INHERITANCE.
Topics Inheritance introduction
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Template Lecture 11 Course Name: High Level Programming Language Year : 2010.
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
EC-111 Algorithms & Computing Lecture #6 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Overview of C++ Polymorphism
Constructors, Copy Constructors, constructor overloading, function overloading Lecture 04.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Chapter 15 - C++ As A "Better C"
Advanced Programming in Java
Two or more functions can have the same name but different parameters
Object-Oriented Programming: Inheritance
Function Overloading.
Inheritance & Polymorphism
Objectives Define polymorphism Overload functions
Week 4 Object-Oriented Programming (1): Inheritance
Inheritance Often, software encapsulates multiple concepts for which some attributes/behaviors overlap E.g. A computer (role-playing) game has: Monsters:
Object Oriented Analysis and Design
Zhen Jiang West Chester University
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
Advanced Programming Behnam Hatami Fall 2017.
Object-Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
CS1201: Programming Language 2
By Muhammad Waris Zargar
Inheritance CT1513.
Object-Oriented Programming (OOP) Lecture No. 22
Recitation Course 0610 Speaker: Liu Yu-Jiun.
Overview of C++ Polymorphism
Eighth step for Learning C++ Programming
C++ Programming CLASS This pointer Static Class Friend Class
Announcements Assignment 4 Due Today Lab 8 Due Wednesday
Computer Science II for Majors
Presentation transcript:

計算機概論實習

2 Class Sample: RECT Class weight length class RECT{ private: int weight, length; public: RECT(); int surface(); int perimeter(); void setWeight(int); int getWeight(); }; int surface(){ return weight  length; } int perimeter(){ return 2  (weight+length); } RECT(){ weight = 10; length = 10; }

3 Function Overloading weight length class RECT{ private: int weight, length; public: RECT(); RECT(int, int); int surface(); int perimeter(); void setWeight(int); int getWeight(); }; Constructor Overloading RECT(int w, int l){ weight = 10; length = 10; } RECT(){ RECT(10, 10); }

4 Function Overloading Function overloading Functions with same name and different parameters Should perform similar tasks I.e., function to square ints and function to square floats int square(int x, int y){return x * y;} int square(int x) {return x * x;} float square(float x) { return x * x; } Overloaded functions distinguished by signature Based on name and parameter types (order matters) Name mangling Encodes function identifier with parameters Type-safe linkage Ensures proper overloaded function called

5 Class Sample: CUBOID Class weight length class CUBOID{ private: int weight, length, height; public: int surface(); int perimeter(); int volume(); void setWeight(int); int getWeight(); }; int surface(){ return 2  (weight  length + weight  height + height  length); } int perimeter(){ return 4  (weight+length+height); } int volume(){ return weight  length  height; } height

6 Overview of Two Classes class RECT{ private: int weight, length; public: RECT(); int surface(); int perimeter(); void setWeight(int); int getWeight(); void setLength(int); int getLength(); }; class CUBOID{ private: int weight, length, height; public: CUBOID(); int surface(); int perimeter(); int volume(); void setWeight(int); int getWeight(); void setLength(int); int getLength(); void setHeight(int); int getHeight(); };

7 Function Override class CUBOID : public RECT{ private: int height; public: CUBOID(); int volume(); void setHeight(int); int getHeight(); }; class RECT{ private: int weight, length; public: RECT() int surface(); int perimeter(); void setWeight(int); int getWeight(); void setLength(int); int getLength(); }; override int surface(); int perimeter(); void setWeight(int); int getWeight(); void setLength(int); int getLength(); Inheritance

8 Function Override When derived class inherits from base class Rewrite the implementation of functions Return type, function name, and parameters are the same. Called Override

9 Introduction to Inheritance Inheritance Software reusability Create new class from existing class Absorb existing class’s data and behaviors Enhance with new capabilities Derived class inherits from base class Derived class More specialized group of objects Behaviors inherited from base class Can customize Additional behaviors

10 Introduction to Inheritance When class A inherits from class B, we show that Inheritance symbol class A : public B Derived classBase class

11 Member Access Specifiers Public Can be accessible publicly Protected Can be accessible by Base class Derived class Private Only can be accessible by base class

12 class ExA{ private: int a; protected: int b; public: void set(int); int get(); } void main() { ExA ex; ex.a; // Error: can’t access private ex.b; // Error: can’t access protected } NOT inheritance  Can not access non-public members

13 class ExA{ private: int a; protected: int b; public: void set(int); int get(); } class ExB : public ExA { public: void fa1(){cout <<get();} void fb(){cout<<b;} //Error: can’t access private void fa2(){cout <<a;} } ExB inherits from ExA  Can access public and protected members

14 Public, Protected, and Private Member private: name; protected: number; public: getName(); Student Member private: name; protected: number; public: getName(); by public function ex: getName(); directly accessible by Student.

15 Summary Protected Data Member Intermediate level of protection between public and private protected members can be accessed by Derived class member functions directly Derived class friends functions directly Advantages Derived classes can modify values directly Slight increase in performance Avoid set/get function call overhead Disadvantages No validity checking Derived class can assign illegal value

16 Practice 4 (P4) members StudentTeacher private: char name[80]; protected: int number; public: void setName(string); void getName(); void setNumber(int); int getNumber(); void input(); void print(); private: int chinese; public: void setChinese(int); int getChinese(); void input(); void print(); private: int bounds; public: void setBounds(int); int getBounds(); void input(); void print();

17 Practice 4 (P4) PLEASE write a program that a user can input information of Student, Teacher, or Member. If the user inputs information of Student/Teacher/Member, please output all the information about the Student/Teacher/Member.