Introduction to Programming

Size: px
Start display at page:

Download "Introduction to Programming"

Transcription

1 Introduction to Programming SS 2010 Adrian Kacso, Univ. Siegen Tel.: 0271/ , Office: H-B 8406 Stand: June 21, 2010 Betriebssysteme / verteilte Systeme Introduction to Programming (1) i Introduction to Programming SS Inheritance and Polymorphism Betriebssysteme / verteilte Systeme Introduction to Programming (2) 255

2 9 Inheritance and Polymorphism Contents: Inheritance Polymorphism Abstract classes Yet another example Betriebssysteme / verteilte Systeme Introduction to Programming (2) Inheritance Reminder: classes A class defines a new type: a collection of variables (data members, attributes) a set of related operations (methods, member functions) they define the interface to objects of the class i.e., the data members can (usually) be accessed only via these methods An object is an instance (a variable) of a class Betriebssysteme / verteilte Systeme Introduction to Programming (2) 257

3 9.1 Inheritance Often Classes are quite similar Assume we need classes for cats and dogs: Cat age Dog age Attributes weight weight Methods eat() Why are these classes so similar? breed eat() Speak() Speak() purr() wagtail() Because both cats and dogs are special kinds of mammals! the common attributes / methods are those of mammals Betriebssysteme / verteilte Systeme Introduction to Programming (2) Inheritance Refinement / specialization Cat purr() Mammal age weight eat() Speak() Dog breed wagtail() Class Mammal defines the common attributes and methods Cat and Dog are derived from Mammal They inherit all attributes and methods of Mammal they also can define additional ones Betriebssysteme / verteilte Systeme Introduction to Programming (2) 259

4 9.1 Inheritance Extending the hierarchy of animal classes Animal age weight eat() Mammal Speak() nursebaby() Fish spawn() Cat purr() Dog breed wagtail() Betriebssysteme / verteilte Systeme Introduction to Programming (2) Inheritance Relations between classes Mammal inherits from Animal, and Dog inherits from Mammal A Dog is a Mammal which is an Animal Animal is a (direct) base class (or superclass) of Mammal, Animal and Mammal are base classes (or superclasses) of Dog Mammal is a derived class (or subclass) of Animal, Dog and Mammal are derived classes (or subclasses) of Animal Betriebssysteme / verteilte Systeme Introduction to Programming (2) 261

5 9.1 Inheritance Uses of inheritance Conceptual Modelling: define a hierarchy of classes that represent the concepts of the data specialization / generalization, e.g., cats and dogs as special kinds of mammals this mostly defines hierarchies of interfaces Code re-use: take a given class, and extend it by a little code to get what you need Betriebssysteme / verteilte Systeme Introduction to Programming (2) Inheritance A Mammal class class Mammal { public: Mammal(); ~Mammal(); int getage() const; void setage(int); int getweight() const; void setweight(int); void Speak() const; protected: // allow access by derived classes int itsage; int itsweight; ; Betriebssysteme / verteilte Systeme Introduction to Programming (2) 263

6 9.1 Inheritance Private vs. protected vs. public Class members can be private, protected, or public: private members can be accessed only within methods of the class itself protected members can be accessed from within methods of the class itself, and of its derived classes public members can also be accessed from other classes ( code outside the class itself ) Betriebssysteme / verteilte Systeme Introduction to Programming (2) Inheritance A Cat class class Cat : public Mammal { public: Cat(); ~Cat(); void purr() const; ; public class inheritance means that the public members of the base class are also public for the derived class (there is also private and protected inheritance, but we will not use it in this course) Betriebssysteme / verteilte Systeme Introduction to Programming (2) 265

7 9.1 Inheritance A Dog class enum Breed { YORKIE, DANDIE, SHETLAND, DOBERMAN, LABRADOR ; class Dog : public Mammal { public: Dog(); Dog(Breed breed); ~Dog(); Breed getbreed() const; void wagtail() const; protected: Breed itsbreed; ; Betriebssysteme / verteilte Systeme Introduction to Programming (2) Inheritance The interface of Dog (accumulated) The following methods can be called for a Dog object: Inherited from Mammal Defined by Dog int getage() const; Dog(); void setage(int); Dog(Breed breed); int getweight() const; ~Dog(); void setweight(int); Breed getbreed() const; void Speak() const; void wagtail() const; The constructor and destructor is not inherited!! Betriebssysteme / verteilte Systeme Introduction to Programming (2) 267

8 9.1 Inheritance The data members of Dog (accumulated) A Dog object has the following data members: Inherited from Mammal Defined by Dog int itsage; Breed itsbreed; int itsweight; Betriebssysteme / verteilte Systeme Introduction to Programming (2) Inheritance Constructors and destructors What happens when a Dog is created? first, the Mammal constructor is called it initializes the data members inherited from Mammal then, the Dog constructor is called it initializes the data members added by Dog And when a Dog is deleted? first, the Dog destructor is called, then, the Mammal destructor Betriebssysteme / verteilte Systeme Introduction to Programming (2) 269

9 9.1 Inheritance Passing arguments to base constructors Assume we have the following constructors for Mammal: Mammal(); Mammal(int age); And we want the following ones for Dog: Dog(); Dog(int age, Breed breed); Dog(int age, int weight, Breed breed); How to do this? Betriebssysteme / verteilte Systeme Introduction to Programming (2) Inheritance Passing arguments to base constructors The Mammal constructors could be implemented like this: Mammal::Mammal() : itsage(1), itsweight(5) { Mammal::Mammal(int age) : itsage(age), itsweight(5) { Betriebssysteme / verteilte Systeme Introduction to Programming (2) 271

10 9.1 Inheritance Passing arguments to base constructors Dog::Dog() : Mammal(), // could be omitted itsbreed(yorkie) { Dog::Dog(int age, BREED breed) : Mammal(age), // base class initialization itsbreed(breed) { Dog::Dog(int age, int weight, BREED breed) : Mammal(age), itsbreed(breed) { itsweight = weight; // cannot initialize itsweight Betriebssysteme / verteilte Systeme Introduction to Programming (2) Inheritance Overriding methods Implementation of the Speak method in Mammal: void Mammal::Speak() { cout << "Mammal sound" << endl; Mammal m; m.speak(); Mammal sound Dog d; d.speak(); Mammal sound Dog inherits the implementation of Speak but shouldn t a dog say Woof? Dog should implement the Speak method differently Betriebssysteme / verteilte Systeme Introduction to Programming (2) 273

11 9.1 Inheritance Overriding methods This can be done by overriding the method: class Dog : public Mammal { public: void Speak() const; ; // redefine the method // in class Dog void Dog::Speak() const // and provide a new { // implementation cout << "Woof!" << endl; Dog d; d.speak(); Woof! Betriebssysteme / verteilte Systeme Introduction to Programming (2) Inheritance Overriding vs. overloading Overloading define multiple methods with the same name, but different signature Overriding define a method in a derived class with the same name and signature as in the base class Caution: overriding means hiding! if you override one of many, overloaded methods (with the same name), you hide all of them! Betriebssysteme / verteilte Systeme Introduction to Programming (2) 275

12 9.1 Inheritance Hiding, calling base methods void Mammal::Move(int distance) { void Mammal::Move(int distance, int angle) { void Dog::Move(int distance) { Now, with Dog d; you can not call d.move(2,4) anymore But, you can call d.mammal::move(2,4) And even d.mammal::move(2), which calls Mammal s implementation of Move(int distance) Betriebssysteme / verteilte Systeme Introduction to Programming (2) Polymorphism Often we want an object to do something, without knowing the exact class of the object E.g., if the Cat class implements void Cat::Speak() const { cout << "Meow!" << endl; can we do something like Mammal *mypets[3]; int i; mypets[0] = new Dog(DOBERMAN); mypets[1] = new Cat(); mypets[2] = new Dog(LABRADOR); cin >> i; mypets[i]->speak(); // ==> Woof! or Meow! Betriebssysteme / verteilte Systeme Introduction to Programming (2) 277

13 9.2 Polymorphism Pointers, references and inheritance If Sub is a subclass of Super, then the following assignments are allowed: Sub sub; Super *p = Super &r = sub; I.e., a super class pointer (reference) can also point (refer) to an object of a subclass but not the other way round! Quiz: what happens in this case? Sub sub; Super super; super = sub; Betriebssysteme / verteilte Systeme Introduction to Programming (2) Polymorphism Virtual methods In C++, if you want the polymorphism to work, the base class must declare the methods in question as being virtual! class Mammal { public: virtual void Speak() const; ; Otherwise, in our example, you would just get Mammal sound i.e., the method of class Mammal is invoked Betriebssysteme / verteilte Systeme Introduction to Programming (2) 279

14 9.2 Polymorphism How does it work? Dog object A Dog object in memory: itsage itsweight itsbreed Mammal part An object of a derived class consists of data members of the base class additional data members of the derived class Betriebssysteme / verteilte Systeme Introduction to Programming (2) Polymorphism How does it work? Dog *d Dog object (seen by d) A Dog object in memory: itsage itsweight itsbreed Mammal part A derived class pointer sees everything Betriebssysteme / verteilte Systeme Introduction to Programming (2) 281

15 9.2 Polymorphism How does it work? Mammal *m Dog *d Dog object (seen by d) A Dog object in memory: itsage itsweight itsbreed Mammal part (seen by m) A base class pointer sees just the base class part at the beginning Betriebssysteme / verteilte Systeme Introduction to Programming (2) Polymorphism How does it work? Mammal *m Dog *d A Dog object in memory: vptr itsage itsweight itsbreed v table Speak() Move() Dog::Speak() { cout << "Woof!" Mammal::Move() { The object contains a pointer to a virtual method table this table has an entry for each virtual method, which points to the proper implementation Thus, each object knows its virtual methods Betriebssysteme / verteilte Systeme Introduction to Programming (2) 283

16 9.2 Polymorphism How does it work? (now with a Cat object) Mammal *m Cat *c A Cat object in memory: vptr itsage itsweight v table Speak() Move() Cat::Speak() { cout << "Meow! Mammal::Move() { In a Cat object s virtual method table, the entry for Speak() points to Cat::Speak() Thus, m->speak() prints: Moew! Betriebssysteme / verteilte Systeme Introduction to Programming (2) Polymorphism How does it work? (now with a Mammal object) Mammal *m A Mammal object in memory: v table vptr itsage itsweight Speak() Move() Mammal::Speak() { cout << "Mamm Mammal::Move() { In a Mammal object s virtual method table, the entry for Speak() points to Mammal::Speak() Thus, m->speak() prints: Mammal sound Betriebssysteme / verteilte Systeme Introduction to Programming (2) 285

17 9.2 Polymorphism Virtual destructors When an object is destroyed, we always want to call the destructor of the object s actual class e.g., call ~Dog(), even with a pointer to Mammal In principle, destructors should always be declared as virtual but virtual makes objects larger and method calls slower Rule of thumb: if a class contains a virtual method, the destructor also should be virtual (assumption: in this case, you refer to objects via a base class pointer or reference) Betriebssysteme / verteilte Systeme Introduction to Programming (2) Abstract Classes Reminder: the drawing example Remember the classes of our drawing program example: Point Line Rectangle draw() move() rotate() getx() gety() draw() move() rotate() draw() move() rotate() Ideally, we would like to use the methods draw(), move(), and rotate() in a polymorphic way, e.g.: drawingelements[i]->draw(); Betriebssysteme / verteilte Systeme Introduction to Programming (2) 287

18 9.3 Abstract Classes A class hierarchy for the drawing example DrawingElement draw() move() rotate() Point getx() gety() Line Rectangle But, implementing draw(), move(), and rotate() in the base class doesn t really make sense! Betriebssysteme / verteilte Systeme Introduction to Programming (2) Abstract Classes The animal hierarchy revisited Mammal Speak() nursebaby() Animal age weight eat() Fish spawn() Animal and Mammal provide useful interfaces But implementing the methods only makes sense for the bottommost classes think e.g. of this artificial Mammal sound in Mammal::Speak() Cat purr() Dog breed wagtail() Animal or Mammal objects also don t make too much sense Betriebssysteme / verteilte Systeme Introduction to Programming (2) 289

19 9.3 Abstract Classes Pure virtual methods Instead of providing an interface and its implementation, a base class also can provide only the interface: class Mammal { public: Mammal(); virtual ~Mammal(); virtual void Speak() const = 0; // pure virtual ; That is, we leave the implementation to the derived classes: void Dog::Speak() const { cout << "Woof!\n"; Betriebssysteme / verteilte Systeme Introduction to Programming (2) Abstract Classes Abstract classes In C++, a class is abstract, if it has at least one pure virtual method abstract C++ classes may also implement some methods An abstract class can not be instantiated you cannot create an object of an abstract class But (of course) polymorphism still works if AC is an abstract class, you can still use pointers (or references) to AC to point to objects of (non-abstract) subclasses of AC (In Java, interfaces serve a similar purpose) Betriebssysteme / verteilte Systeme Introduction to Programming (2) 291

20 9.3 Abstract Classes Hierarchies of abstract / concrete classes We can define Animal and Mammal as abstract classes they now simply provide common interfaces for all their subclasses If we derive Dog and Cat from Mammal, we make these classes concrete, so we can have Dog objects that implement the Mammal interface Betriebssysteme / verteilte Systeme Introduction to Programming (2) Abstract Classes The abstract / concrete hierarchy of animals Mammal Speak() nursebaby() Animal age weight eat() Fish spawn() Abstract Classes (define interfaces) Cat purr() Dog breed wagtail() Concrete Classes (provide implementations) Betriebssysteme / verteilte Systeme Introduction to Programming (2) 293

21 9.4 Yet Another Example A hierarchy of graphics elements From a previous exam: Define an abstract class Element for graphics elements. The abstract class shall have a method circumference that returns the circumference of the object as a double value. Betriebssysteme / verteilte Systeme Introduction to Programming (2) Yet Another Example Class Element class Element { public: virtual ~Element() { virtual double circumference() const = 0; ; Notes: No need to declare a constructor the compiler will create an empty one for us We are using a short form with ~Element(): you can also define a method s body inside the class declaration not recommended for real programming! Betriebssysteme / verteilte Systeme Introduction to Programming (2) 295

22 9.4 Yet Another Example Derive real classes Implement three classes of graphics elements which are derived from Element: 1. Circle, defined via its radius r. 2. Rectangle, defined via the lengths of its sides a and b. 3. Square, derived from Rectangle, using a = b. For each class, you need to implement a constructor, the method Circumference, and the data members. (Remark: In general, deriving a square class from a rectangle class is a questionable approach: although each square is a rectangle, not every operation on a rectangle is allowed on a square, e.g., set the length of one side ) Betriebssysteme / verteilte Systeme Introduction to Programming (2) Yet Another Example Class Circle class Circle : public Element { public: Circle(double radius) : itsradius(radius) { double circumference() const { return 2 * PI * itsradius; private: double itsradius; ; Betriebssysteme / verteilte Systeme Introduction to Programming (2) 297

23 9.4 Yet Another Example Class Rectangle class Rectangle : public Element { public: Rectangle(double a, double b) : itsa(a), itsb(b) { double circumference() const { return 2 * (itsa + itsb); private: double itsa, itsb; ; Betriebssysteme / verteilte Systeme Introduction to Programming (2) Yet Another Example Class Square class Square : public Rectangle { public: Square(double a) : Rectangle(a,a) { ; Betriebssysteme / verteilte Systeme Introduction to Programming (2) 299

24 9.4 Yet Another Example Use the Element interface Implement a function: double sum(element const *array[], unsigned int len) that computes the sum of the circumferences of the Element objects in the array. len gives the length of the array. Betriebssysteme / verteilte Systeme Introduction to Programming (2) Yet Another Example Function sum() double sum(element const *array[], unsigned int len) { double retval = 0.0; //unsigned int i; for ( unsigned int i=0; i < len; i++) { retval += array[i]->circumference(); return retval; Betriebssysteme / verteilte Systeme Introduction to Programming (2) 301

25 9.5 Summary Inheritance Cat purr() Mammal age weight eat() Speak() Dog breed wagtail() Class Mammal defines the common attributes and methods Cat and Dog are derived from Mammal They inherit all attributes and methods of Mammal they also can define additional ones Betriebssysteme / verteilte Systeme Introduction to Programming (2) Summary A Cat class derived from Mammal class Mammal { public: Mammal(); ~Mammal(); int getage() const; protected: int itsage; int itsweight; ; // allow access by derived classes class Cat : public Mammal { // public members remain public public: Cat(); ~Cat(); void purr() const; ; Betriebssysteme / verteilte Systeme Introduction to Programming (2) 303

26 9.5 Summary Polymorphism Often we want an object to do something, without knowing the exact class of the object E.g., we can write Mammal *mypets[3]; int i; mypets[0] = new Dog(DOBERMAN); mypets[1] = new Cat(); mypets[2] = new Dog(LAB); cin >> i; mypets[i]->speak(); // ==> Woof! or Meow! Caveat: This only works if Mammal declares the Speak-Method as virtual! Betriebssysteme / verteilte Systeme Introduction to Programming (2) Summary How do virtual methods work? Mammal *m Dog *d A Dog object in memory: vptr itsage itsweight itsbreed v table Speak() Move() Dog::Speak() { cout << "Woof!" Mammal::Move() { The object contains a pointer to a virtual method table This table has an entry for each virtual method, which points to the proper implementation Thus, each object knows its virtual methods Betriebssysteme / verteilte Systeme Introduction to Programming (2) 305

27 9.5 Summary Pure virtual methods Instead of providing an interface and its implementation, a base class also can provide only the interface: class Mammal { public: Mammal(); virtual ~Mammal(); virtual void Speak() const = 0; // pure virtual ; That is, we leave the implementation to the derived classes: void Dog::Speak() const { cout << "Woof!\n"; Betriebssysteme / verteilte Systeme Introduction to Programming (2) Summary Reminder: abstract classes In C++, a class is abstract, if it has at least one pure virtual method abstract C++ classes may also implement some methods An abstract class can not be instantiated you cannot create an object of an abstract class But (of course) polymorphism still works if AC is an abstract class, you can still use pointers (or references) to AC to point to objects of (non-abstract) subclasses of AC (In Java, interfaces serve a similar purpose) Betriebssysteme / verteilte Systeme Introduction to Programming (2) 307

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4 CS32 - Week 4 Umut Oztok Jul 15, 2016 Inheritance Process of deriving a new class using another class as a base. Base/Parent/Super Class Derived/Child/Sub Class Inheritance class Animal{ Animal(); ~Animal();

More information

Introduction to Programming

Introduction to Programming Introduction to Programming Summer Term 2014 Dr. Adrian Kacso, Univ. Siegen adriana.dkacsoa@duni-siegena.de Tel.: 0271/740-966, Office: H-B 8406 State: June 4, 2014 Betriebssysteme / verteilte Systeme

More information

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1 CS250 Intro to CS II Spring 2017 CS250 - Intro to CS II 1 Topics Virtual Functions Pure Virtual Functions Abstract Classes Concrete Classes Binding Time, Static Binding, Dynamic Binding Overriding vs Redefining

More information

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

private: // can only be used by the member functions of MyType and friends of MyType }; CSCI 3110 Inheritance (1) 1. Inheritance a relationship among classes whereby a class derives properties from a previously defined class. Inheritance provides a means of deriving a new class from existing

More information

Inheritance, and Polymorphism.

Inheritance, and Polymorphism. Inheritance and Polymorphism by Yukong Zhang Object-oriented programming languages are the most widely used modern programming languages. They model programming based on objects which are very close to

More information

Polymorphism. Arizona State University 1

Polymorphism. Arizona State University 1 Polymorphism CSE100 Principles of Programming with C++, Fall 2018 (based off Chapter 15 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University

More information

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

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1 Polymorphism Part 1 What is Polymorphism? Polymorphism refers to a programming language s ability to process objects differently depending on their data type or class. Number person real complex kid adult

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Object Oriented Programming Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. Al-Azhar University Website: eaymanelshenawy.wordpress.com Email : eaymanelshenawy@azhar.edu.eg

More information

Chapter 14 Abstract Classes and Interfaces

Chapter 14 Abstract Classes and Interfaces Chapter 14 Abstract Classes and Interfaces 1 What is abstract class? Abstract class is just like other class, but it marks with abstract keyword. In abstract class, methods that we want to be overridden

More information

Polymorphism Part 1 1

Polymorphism Part 1 1 Polymorphism Part 1 1 What is Polymorphism? Polymorphism refers to a programming language s ability to process objects differently depending on their data type or class. Number person real complex kid

More information

Java Object Oriented Design. CSC207 Fall 2014

Java Object Oriented Design. CSC207 Fall 2014 Java Object Oriented Design CSC207 Fall 2014 Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code

More information

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1 Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1 Inheritance Consider a new type Square. Following how we declarations for the Rectangle and Circle classes we could declare it as follows:

More information

Introduction to Scientific Programming with C++

Introduction to Scientific Programming with C++ Introduction to Scientific Programming with C++ Session 5: Advanced object oriented programming Martin Uhrin edited by Andrew Maxwell and Ahmed Al Refaie UCL November 6, 2016 1 / 24 Table of Contents 1

More information

Inheritance (Outsource: )

Inheritance (Outsource: ) (Outsource: 9-12 9-14) is a way to form new classes using classes that have already been defined. The new classes, known as derived classes, inherit attributes and behavior of the pre-existing classes,

More information

ECE 3574: Dynamic Polymorphism using Inheritance

ECE 3574: Dynamic Polymorphism using Inheritance 1 ECE 3574: Dynamic Polymorphism using Inheritance Changwoo Min 2 Administrivia Survey on class will be out tonight or tomorrow night Please, let me share your idea to improve the class! 3 Meeting 10:

More information

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes. a and Interfaces Class Shape Hierarchy Consider the following class hierarchy Shape Circle Square Problem AND Requirements Suppose that in order to exploit polymorphism, we specify that 2-D objects must

More information

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

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name: Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name: Section 1 Multiple Choice Questions (40 pts total, 2 pts each): Q1: Employee is a base class and HourlyWorker is a derived class, with

More information

VIRTUAL FUNCTIONS Chapter 10

VIRTUAL FUNCTIONS Chapter 10 1 VIRTUAL FUNCTIONS Chapter 10 OBJECTIVES Polymorphism in C++ Pointers to derived classes Important point on inheritance Introduction to virtual functions Virtual destructors More about virtual functions

More information

QUIZ. How could we disable the automatic creation of copyconstructors

QUIZ. How could we disable the automatic creation of copyconstructors QUIZ How could we disable the automatic creation of copyconstructors pre-c++11? What syntax feature did C++11 introduce to make the disabling clearer and more permanent? Give a code example. QUIZ How

More information

Polymorphism. Zimmer CSCI 330

Polymorphism. Zimmer CSCI 330 Polymorphism Polymorphism - is the property of OOP that allows the run-time binding of a function's name to the code that implements the function. (Run-time binding to the starting address of the code.)

More information

CS250 Final Review Questions

CS250 Final Review Questions CS250 Final Review Questions The following is a list of review questions that you can use to study for the final. I would first make sure that you review all previous exams and make sure you fully understand

More information

Critique this Code. Hint: It will crash badly! (Next slide please ) 2011 Fawzi Emad, Computer Science Department, UMCP

Critique this Code. Hint: It will crash badly! (Next slide please ) 2011 Fawzi Emad, Computer Science Department, UMCP Critique this Code string const & findsmallest(vector const &v) string smallest = v[0]; for (unsigned int i = 0; i < v.size(); i++) if (v[i] < smallest) smallest = v[i]; return smallest; Hint:

More information

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017 Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017 Three Properties of Object-Oriented Languages: Encapsulation Inheritance Dynamic method binding (polymorphism)

More information

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs. Local Variable Initialization Unlike instance vars, local vars must be initialized before they can be used. Eg. void mymethod() { int foo = 42; int bar; bar = bar + 1; //compile error bar = 99; bar = bar

More information

QUIZ. How could we disable the automatic creation of copyconstructors

QUIZ. How could we disable the automatic creation of copyconstructors QUIZ How could we disable the automatic creation of copyconstructors pre-c++11? What syntax feature did C++11 introduce to make the disabling clearer and more permanent? Give a code example. Ch. 14: Inheritance

More information

Software Practice 1 - Inheritance and Interface Inheritance Overriding Polymorphism Abstraction Encapsulation Interfaces

Software Practice 1 - Inheritance and Interface Inheritance Overriding Polymorphism Abstraction Encapsulation Interfaces Software Practice 1 - Inheritance and Interface Inheritance Overriding Polymorphism Abstraction Encapsulation Interfaces Prof. Hwansoo Han T.A. Minseop Jeong T.A. Wonseok Choi 1 In Previous Lecture We

More information

Object Oriented Software Design II

Object Oriented Software Design II Object Oriented Software Design II Inheritance Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 29, 2012 G. Lipari (Scuola Superiore Sant Anna) C++ Intro February

More information

Object Oriented Software Design II

Object Oriented Software Design II Object Oriented Software Design II Inheritance Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 29, 2012 G. Lipari (Scuola Superiore Sant Anna) C++ Intro February

More information

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017 OOP components For : COP 3330. Object oriented Programming (Using C++) http://www.compgeom.com/~piyush/teach/3330 Data Abstraction Information Hiding, ADTs Encapsulation Type Extensibility Operator Overloading

More information

COMP200 INTERFACES. OOP using Java, from slides by Shayan Javed

COMP200 INTERFACES. OOP using Java, from slides by Shayan Javed 1 1 COMP200 INTERFACES OOP using Java, from slides by Shayan Javed Interfaces 2 ANIMAL picture food sleep() roam() makenoise() eat() 3 ANIMAL picture food sleep() roam() makenoise() eat() 4 roam() FELINE

More information

Introduction to Object-Oriented Programming

Introduction to Object-Oriented Programming Introduction to Object-Oriented Programming Review 2: Object-Oriented Programming Christopher Simpkins chris.simpkins@gatech.edu CS 1331 (Georgia Tech) Review 2: Object-Oriented Programming 1 / 14 Topics

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 22 Polymorphism using Interfaces Overview Problem: Can we delay decisions regarding which method to use until run time? Polymorphism Different methods

More information

COMP 110/L Lecture 19. Kyle Dewey

COMP 110/L Lecture 19. Kyle Dewey COMP 110/L Lecture 19 Kyle Dewey Outline Inheritance extends super Method overriding Automatically-generated constructors Inheritance Recap -We talked about object-oriented programming being about objects

More information

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario The Story So Far... Classes as collections of fields and methods. Methods can access fields, and

More information

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

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value Paytm Programming Sample paper: 1) A copy constructor is called a. when an object is returned by value b. when an object is passed by value as an argument c. when compiler generates a temporary object

More information

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine Homework 6 Yuji Shimojo CMSC 330 Instructor: Prof. Reginald Y. Haseltine July 21, 2013 Question 1 What is the output of the following C++ program? #include #include using namespace

More information

Object Oriented Programming. Java-Lecture 11 Polymorphism

Object Oriented Programming. Java-Lecture 11 Polymorphism Object Oriented Programming Java-Lecture 11 Polymorphism Abstract Classes and Methods There will be a situation where you want to develop a design of a class which is common to many classes. Abstract class

More information

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator C++ Addendum: Inheritance of Special Member Functions Constructors Destructor Construction and Destruction Order Assignment Operator What s s Not Inherited? The following methods are not inherited: Constructors

More information

15: Polymorphism & Virtual Functions

15: Polymorphism & Virtual Functions 15: Polymorphism & Virtual Functions 김동원 2003.02.19 Overview virtual function & constructors Destructors and virtual destructors Operator overloading Downcasting Thinking in C++ Page 1 virtual functions

More information

G52CPP C++ Programming Lecture 13

G52CPP C++ Programming Lecture 13 G52CPP C++ Programming Lecture 13 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last lecture Function pointers Arrays of function pointers Virtual and non-virtual functions vtable and

More information

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia Object Oriented Programming in Java Jaanus Pöial, PhD Tallinn, Estonia Motivation for Object Oriented Programming Decrease complexity (use layers of abstraction, interfaces, modularity,...) Reuse existing

More information

Introduction to Programming

Introduction to Programming Introduction to Programming Summer Term 2015 Dr. Adrian Kacso, Univ. Siegen adriana.dkacsoa@duni-siegena.de Tel.: 0271/740-3966, Office: H-B 8406 State: May 6, 2015 Betriebssysteme / verteilte Systeme

More information

Derived and abstract data types. TDT4205 Lecture 15

Derived and abstract data types. TDT4205 Lecture 15 1 Derived and abstract data types TDT4205 Lecture 15 2 Where we were We ve looked at static semantics for primitive types and how it relates to type checking We ve hinted at derived types using a multidimensional

More information

Need to store a list of shapes, each of which could be a circle, rectangle, or triangle

Need to store a list of shapes, each of which could be a circle, rectangle, or triangle CS112-2012S-23 Abstract Classes and Interfaces 1 23-0: Drawing Example Creating a drawing program Allow user to draw triangles, circles, rectanlges, move them around, etc. Need to store a list of shapes,

More information

OVERRIDING. 7/11/2015 Budditha Hettige 82

OVERRIDING. 7/11/2015 Budditha Hettige 82 OVERRIDING 7/11/2015 (budditha@yahoo.com) 82 What is Overriding Is a language feature Allows a subclass or child class to provide a specific implementation of a method that is already provided by one of

More information

Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction

Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction Class Interaction There are 3 types of class interaction. One

More information

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity. OOPS Viva Questions 1. What is OOPS? OOPS is abbreviated as Object Oriented Programming system in which programs are considered as a collection of objects. Each object is nothing but an instance of a class.

More information

CS 251 Intermediate Programming Inheritance

CS 251 Intermediate Programming Inheritance CS 251 Intermediate Programming Inheritance Brooke Chenoweth University of New Mexico Spring 2018 Inheritance We don t inherit the earth from our parents, We only borrow it from our children. What is inheritance?

More information

C++ (classes) Hwansoo Han

C++ (classes) Hwansoo Han C++ (classes) Hwansoo Han Inheritance Relation among classes shape, rectangle, triangle, circle, shape rectangle triangle circle 2 Base Class: shape Members of a class Methods : rotate(), move(), Shape(),

More information

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes CS/ENGRD 2110 SPRING 2019 Lecture 7: Interfaces and Abstract Classes http://courses.cs.cornell.edu/cs2110 1 Announcements 2 A2 is due Thursday night (14 February) Go back to Lecture 6 & discuss method

More information

And Even More and More C++ Fundamentals of Computer Science

And Even More and More C++ Fundamentals of Computer Science And Even More and More C++ Fundamentals of Computer Science Outline C++ Classes Special Members Friendship Classes are an expanded version of data structures (structs) Like structs, the hold data members

More information

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2) Software Paradigms (Lesson 3) Object-Oriented Paradigm (2) Table of Contents 1 Reusing Classes... 2 1.1 Composition... 2 1.2 Inheritance... 4 1.2.1 Extending Classes... 5 1.2.2 Method Overriding... 7 1.2.3

More information

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

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi Modern C++ for Computer Vision and Image Processing Igor Bogoslavskyi Outline Move semantics Classes Operator overloading Making your class copyable Making your class movable Rule of all or nothing Inheritance

More information

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding Conformance Conformance and Class Invariants Same or Better Principle Access Conformance Contract Conformance Signature Conformance Co-, Contra- and No-Variance Overloading and Overriding Inheritance as

More information

CS250 Final Review Questions

CS250 Final Review Questions CS250 Final Review Questions The following is a list of review questions that you can use to study for the final. I would first make sure you review all previous exams and make sure you fully understand

More information

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community CSCI-12 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community http://csc.cs.rit.edu 1. Provide a detailed explanation of what the following code does: 1 public boolean checkstring

More information

Objectives. INHERITANCE - Part 1. Using inheritance to promote software reusability. OOP Major Capabilities. When using Inheritance?

Objectives. INHERITANCE - Part 1. Using inheritance to promote software reusability. OOP Major Capabilities. When using Inheritance? INHERITANCE - Part 1 OOP Major Capabilities Introduction Basic Concepts and Syntax Protected Members Constructors and Destructors Under Inheritance Multiple Inheritance Common Programming Errors encapsulation

More information

INHERITANCE - Part 1. CSC 330 OO Software Design 1

INHERITANCE - Part 1. CSC 330 OO Software Design 1 INHERITANCE - Part 1 Introduction Basic Concepts and Syntax Protected Members Constructors and Destructors Under Inheritance Multiple Inheritance Common Programming Errors CSC 330 OO Software Design 1

More information

Inheritance, Polymorphism, and Interfaces

Inheritance, Polymorphism, and Interfaces Inheritance, Polymorphism, and Interfaces Chapter 8 Inheritance Basics (ch.8 idea) Inheritance allows programmer to define a general superclass with certain properties (methods, fields/member variables)

More information

COMP 110/L Lecture 20. Kyle Dewey

COMP 110/L Lecture 20. Kyle Dewey COMP 110/L Lecture 20 Kyle Dewey Outline super in methods abstract Classes and Methods Polymorphism super in Methods Recap You ve seen super in constructors... Recap You ve seen super in constructors...

More information

CS-202 Introduction to Object Oriented Programming

CS-202 Introduction to Object Oriented Programming CS-202 Introduction to Object Oriented Programming California State University, Los Angeles Computer Science Department Lecture III Inheritance and Polymorphism Introduction to Inheritance Introduction

More information

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

CS 162, Lecture 25: Exam II Review. 30 May 2018 CS 162, Lecture 25: Exam II Review 30 May 2018 True or False Pointers to a base class may be assigned the address of a derived class object. In C++ polymorphism is very difficult to achieve unless you

More information

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism. Outline Inheritance Class Extension Overriding Methods Inheritance and Constructors Polymorphism Abstract Classes Interfaces 1 OOP Principles Encapsulation Methods and data are combined in classes Not

More information

Why use inheritance? The most important slide of the lecture. Programming in C++ Reasons for Inheritance (revision) Inheritance in C++

Why use inheritance? The most important slide of the lecture. Programming in C++ Reasons for Inheritance (revision) Inheritance in C++ Session 6 - Inheritance in C++ The most important slide of the lecture Dr Christos Kloukinas City, UoL http://staff.city.ac.uk/c.kloukinas/cpp (slides originally produced by Dr Ross Paterson) Why use inheritance?

More information

Function Overloading

Function Overloading Function Overloading C++ supports writing more than one function with the same name but different argument lists How does the compiler know which one the programmer is calling? They have different signatures

More information

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor CSE 143 Object & Class Relationships Inheritance Reading: Ch. 9, 14 Relationships Between Real Things Man walks dog Dog strains at leash Dog wears collar Man wears hat Girl feeds dog Girl watches dog Dog

More information

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com More C++ : Vectors, Classes, Inheritance, Templates with content from cplusplus.com, codeguru.com 2 Vectors vectors in C++ basically arrays with enhancements indexed similarly contiguous memory some changes

More information

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed? QUIZ Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed? Or Foo(x), depending on how we want the initialization

More information

Inheritance & Polymorphism

Inheritance & Polymorphism Inheritance & Polymorphism Procedural vs. object oriented Designing for Inheritance Test your Design Inheritance syntax **Practical ** Polymorphism Overloading methods Our First Example There will be shapes

More information

Object-Oriented Programming, Iouliia Skliarova

Object-Oriented Programming, Iouliia Skliarova Object-Oriented Programming, Iouliia Skliarova You reuse code by creating new classes, but instead of creating them from scratch, you use existing classes that someone else has built and debugged. In composition

More information

Midterm Review. PIC 10B Spring 2018

Midterm Review. PIC 10B Spring 2018 Midterm Review PIC 10B Spring 2018 Q1 What is size t and when should it be used? A1 size t is an unsigned integer type used for indexing containers and holding the size of a container. It is guarenteed

More information

Chapter 5 Object-Oriented Programming

Chapter 5 Object-Oriented Programming Chapter 5 Object-Oriented Programming Develop code that implements tight encapsulation, loose coupling, and high cohesion Develop code that demonstrates the use of polymorphism Develop code that declares

More information

INHERITANCE - Part 1. CSC 330 OO Software Design 1

INHERITANCE - Part 1. CSC 330 OO Software Design 1 INHERITANCE - Part 1 Introduction Basic Concepts and Syntax Protected Members Constructors and Destructors Under Inheritance Multiple Inheritance Common Programming Errors CSC 330 OO Software Design 1

More information

Chapter 15: Inheritance, Polymorphism, and Virtual Functions

Chapter 15: Inheritance, Polymorphism, and Virtual Functions Chapter 15: Inheritance, Polymorphism, and Virtual Functions 15.1 What Is Inheritance? What Is Inheritance? Provides a way to create a new class from an existing class The new class is a specialized version

More information

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: has a CSE143 Sp Student. CSE 143 Java Object & Class Relationships Inheritance Reading: Ch. 9, 14 Relationships Between Real Things Man walks dog Dog strains at leash Dog wears collar Man wears hat Girl feeds dog Girl watches

More information

Interface Class. Lecture 22. Based on Slides of Dr. Norazah Yusof

Interface Class. Lecture 22. Based on Slides of Dr. Norazah Yusof Interface Class Lecture 22 Based on Slides of Dr. Norazah Yusof 1 Interface & Implements An interface is a classlike construct that contains only constants variables and abstract methods definition. An

More information

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources. Java Inheritance Written by John Bell for CS 342, Spring 2018 Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources. Review Which of the following is true? A. Java classes may either

More information

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: has a CSC Employee. Supervisor CSC 143 Object & Class Relationships Inheritance Reading: Ch. 10, 11 Relationships Between Real Things Man walks dog Dog strains at leash Dog wears collar Man wears hat Girl feeds dog Girl watches dog

More information

Introduction to Classes

Introduction to Classes Introduction to Classes Procedural and Object-Oriented Programming Procedural and Object-Oriented Programming Procedural programming focuses on the process/actions that occur in a program Object-Oriented

More information

POLYMORPHISM Polymorphism: the type Polymorphism taking many shapes type of object

POLYMORPHISM Polymorphism: the type Polymorphism taking many shapes type of object 1 License: http://creativecommons.org/licenses/by-nc-nd/3.0/ POLYMORPHISM There are three major concepts in object-oriented programming: 1. Encapsulation (Classes), Data abstraction, information hiding

More information

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE Abstract Base Classes POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors class B { // base class virtual void m( ) =0; // pure virtual function class D1 : public

More information

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors CSC 330 OO Software Design 1 Abstract Base Classes class B { // base class virtual void m( ) =0; // pure virtual

More information

What does it mean by information hiding? What are the advantages of it? {5 Marks}

What does it mean by information hiding? What are the advantages of it? {5 Marks} SECTION ONE (COMPULSORY) Question #1 [30 Marks] a) Describe the main characteristics of object-oriented programming. {5 Marks Encapsulation the ability to define a new type and a set of operations on that

More information

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture - 43 Dynamic Binding (Polymorphism): Part III Welcome to Module

More information

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak OBJECT ORIENTED PROGRAMMING Ms. Ajeta Nandal C.R.Polytechnic,Rohtak OBJECT ORIENTED PARADIGM Object 2 Object 1 Data Data Function Function Object 3 Data Function 2 WHAT IS A MODEL? A model is an abstraction

More information

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed CREATED BY: Muhammad Bilal Arslan Ahmad Shaad JAVA Chapter No 5 Instructor: Muhammad Naveed Muhammad Bilal Arslan Ahmad Shaad Chapter No 5 Object Oriented Programming Q: Explain subclass and inheritance?

More information

Lecture 21: The Many Hats of Scala: OOP 10:00 AM, Mar 14, 2018

Lecture 21: The Many Hats of Scala: OOP 10:00 AM, Mar 14, 2018 CS18 Integrated Introduction to Computer Science Fisler, Nelson Lecture 21: The Many Hats of Scala: OOP 10:00 AM, Mar 14, 2018 Contents 1 Mutation in the Doghouse 1 1.1 Aside: Access Modifiers..................................

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 05: Inheritance and Interfaces MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Inheritance and Interfaces 2 Introduction Inheritance and Class Hierarchy Polymorphism Abstract Classes

More information

Inheritance (Part 2) Notes Chapter 6

Inheritance (Part 2) Notes Chapter 6 Inheritance (Part 2) Notes Chapter 6 1 Object Dog extends Object Dog PureBreed extends Dog PureBreed Mix BloodHound Komondor... Komondor extends PureBreed 2 Implementing Inheritance suppose you want to

More information

More C++ : Vectors, Classes, Inheritance, Templates

More C++ : Vectors, Classes, Inheritance, Templates Vectors More C++ : Vectors,, Inheritance, Templates vectors in C++ basically arrays with enhancements indexed similarly contiguous memory some changes defined differently can be resized without explicit

More information

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides 1B1b Inheritance Agenda Introduction to inheritance. How Java supports inheritance. Inheritance is a key feature of object-oriented oriented programming. 1 2 Inheritance Models the kind-of or specialisation-of

More information

Midterm Exam 5 April 20, 2015

Midterm Exam 5 April 20, 2015 Midterm Exam 5 April 20, 2015 Name: Section 1: Multiple Choice Questions (24 pts total, 3 pts each) Q1: Which of the following is not a kind of inheritance in C++? a. public. b. private. c. static. d.

More information

Inheritance, polymorphism, interfaces

Inheritance, polymorphism, interfaces Inheritance, polymorphism, interfaces "is-a" relationship Similar things (sharing same set of attributes / operations): a group / concept Similar groups (sharing a subset of attributes / operations): a

More information

Data Abstraction. Hwansoo Han

Data Abstraction. Hwansoo Han Data Abstraction Hwansoo Han Data Abstraction Data abstraction s roots can be found in Simula67 An abstract data type (ADT) is defined In terms of the operations that it supports (i.e., that can be performed

More information

CS105 C++ Lecture 7. More on Classes, Inheritance

CS105 C++ Lecture 7. More on Classes, Inheritance CS105 C++ Lecture 7 More on Classes, Inheritance " Operator Overloading Global vs Member Functions Difference: member functions already have this as an argument implicitly, global has to take another parameter.

More information

Inheritance and Polymorphism. CS180 Fall 2007

Inheritance and Polymorphism. CS180 Fall 2007 Inheritance and Polymorphism CS180 Fall 2007 Definitions Inheritance object oriented way to form new classes from pre-existing ones Superclass The parent class If class is final, cannot inherit from this

More information

C++ Classes & Object Oriented Programming

C++ Classes & Object Oriented Programming C++ Classes & Object Oriented Programming What is it? Object Oriented Programming 1 Object Oriented Programming One of the first applications of modern computing was modeling and simulation. Scientists

More information

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

1. the base class s (default) constructor is executed first, 2. followed by the derived class s constructor Inheritance & Polymorphism Week 7 Gaddis: Chapter 15 CS 5301 Spring 2017 Inheritance A way to create a new class from an existing class The new class is a specialized version of the existing class Base

More information

CSE 143. "Class Relationships" Class Relationships and Inheritance. A Different Relationship. Has-a vs. Is-a. Hierarchies of Organization

CSE 143. Class Relationships Class Relationships and Inheritance. A Different Relationship. Has-a vs. Is-a. Hierarchies of Organization Class Relationships and Inheritance [Chapter 8, pp.343-354] "Class Relationships" is the title of Chapter 8 One class may include another as a member variable Date" used inside "Performance" Called a "has-a"

More information

Overriding המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון

Overriding המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון Overriding עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון 2 Roadmap A method in a child class overrides a method in the parent class if it has the same name and type signature: Parent void method(int,float)

More information