1 Inheritance: From membership point of view Vehicle SUV Honda Pilot Object Number Integer Double Base concept Derived concept Java Base class Derived.

Slides:



Advertisements
Similar presentations
Engineering Problem Solving With C++ An Object Based Approach Additional Topics Chapter 10 Programming with Classes.
Advertisements

Operator overloading redefine the operations of operators
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
1 CSC241: Object Oriented Programming Lecture No 21.
Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
Introduction to Programming Lecture 39. Copy Constructor.
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
Derived Classes. C++ 2 Outline  Definition  Virtual functions  Virtual base classes  Abstract classes. Pure virtual functions.
C++ Classes & Data Abstraction
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors.
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.
Derived Classes & Inheritance. Inheritance The process of having a new class acquire some or many of its properties from an existing class The new class.
Before Introducing Inheritance … Here is another way to re-use what have been developed: This is known as the object composition! A real-world example.
Wrap Up and Misc Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.
DERIVED CLASSES AND INHERITANCE Moshe Fresko Bar-Ilan University Object Oriented Programing
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 3.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
9/12/2015IT 2751 IDE ( Integrated Development Environment ) 1.A customized plain text editor 2.Compiler 3.Loader 4.Debugging tool JDK (Java Development.
Class Byteline Ustyugov Dmitry MDSP November, 2009.
Slide 1 Unit Testing. Slide 2 Unit Testing Options l Use N-Unit In a microsoft environment.NET… you can use their supplied N-Unit testing to test your.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Operator Overloading Like most languages, C++ supports a set of operators for its built-in types. Example: int x=2+3; // x=5 However, most concepts for.
J. P. Cohoon and J. W. Davidson © 1997 McGraw-Hill, Inc. Templates Generic functions and classes.
Object-Oriented Programming in C++ More examples of Association.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
Inheritance Initialization & Destruction of Derived Objects Protected Members Non-public Inheritance Virtual Function Implementation Virtual Destructors.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
1 Classes: A class is a concept of something Vehicle 4 wheels, seats, engine, windows, color…… accelerate, start, stop, turn, … attributes.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Learners Support Publications Constructors and Destructors.
CS180 Recitation Chapter 7: Defining Your Own Classes II.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Unified Modeling Language
Unit 2. Constructors It initializes an object when it is created. It has same as its class and syntactically similar to a method. Constructor have no.
SEG4110 – Advanced Software Design and Reengineering TOPIC I Introduction to C++ For those who know Java And OO Principles in General.
1 Overload assignment = as a member function class point { public:..... point & operator = (const point & a);..... private: double my_x; double my_y;.....
On dynamic memory and classes ● We previously had only discussed dynamic memory in regards to structs and dynamic arrays. ● However, they can be used (to.
Constructors and Destructors
Pointers and Dynamic Arrays
Submission Example May 14, 2018
OBJECT ORIENTED PROGRAMMING
Robot Class name Attributes Constructor Services, methods
Class A { public : Int x; A()
Memberwise Assignment / Initialization
group work #hifiTeam
CS5253 Workshop I Lecturer: Dr. Lusheng Wang
Contents Introduction to Constructor Characteristics of Constructor
Initialization List.
The Big Three If you need one of them, you need all of them
Constructors and destructors
Constructors and Destructors
Indirection.
Summary: Abstract Data Type
Object-Oriented Programming (OOP) Lecture No. 22
Overview of C++ Polymorphism
Function Overloading.
IDE (Integrated Development Environment)
Review of Function Overloading
Class: Special Topics Overloading (methods) Copy Constructors
Constructors & Destructors
Presentation transcript:

1 Inheritance: From membership point of view Vehicle SUV Honda Pilot Object Number Integer Double Base concept Derived concept Java Base class Derived class A SUV is a vehicle, but a vehicle may not be a SUV

2 Inheritance: From functionality point of view SUV Vehicle Derived class Base class All vehicle can do, a SUV can too

3 Base class & Derived class // A is a base class class A { public: A(int a); int get_a(); private: int my_a; }; // B is a derived class class B: public A { public: B(int b); int get_b(); private: int my_b; }; int main() { A a(2); B b(3); cout << a.get_a(); cout << b.get_a() << b.get_b(); } B inherits A’s functions and variables.

4 Base class and Derived class class A { public: A(int a); int get_a(); private: int my_a; }; class B: public A { public: B(int b); int get_b(); private: int my_b; }; B::B(int b) :my_b(b), A(2*b) { } Need to call the constructor of A to initialize A’s private variable; the constructors will not be inherited. You can decide how to call A’s constructor.

5 Parameters and Derived class class A { get_a() }; class B: public A { get_b() }; void fun_for_A(A a) {....a.get_a()....; } void fun_for_B(B b) {....b.get_a()....;....b.get_b()....; } int main() { A a(2); B b(3); fun_for_A(b); // This is OK! fun_for_B(a); // This is not allowed } because fun_for_B may use a member function of class B that is newly defined when derive B but not available in class A.

6 3 Generations // Base class class A { public: A(int a); int get_a(); private: int my_a; }; // Derived class class B: public A { public: B(int a); int get_b(); private: int my_b; }; int main() { A a(1); B b(2); C c(3); cout << a.get_a(); cout << b.get_a() << b.get_b(); cout << c.get_a() << c.get_b() << c.get_c(); } // Derived class class C: public B { public: C(int a); int get_c(); private: int my_c; };

7 Richer and Richer public: A(int a); int get_a(); private: int my_a = 1; int main() { A a(1); B b(2); C c(3);..... } public: B(int a); int get_a(); int get_b(); private: int my_a = 3; int my_b = 2; public: C(int a); int get_a(); int get_b(); int get_c(); private: int my_a = 4; int my_b = 5; int my_c = 3; a: b: c: Suppose constructors are defined as: A::A(int a) :my_a(a) {} B::B(int b) :my_b(b), A(b+1) {} C:C(int c) :my_c(c), B(c+2) {} //C:C(int c) //:my_c(c), A(c+1), B(c+2) {} The constructors of the base classes will not be inherited. Inherited from A Inherited from B 6

8 Inheritance and Big-Three // Base class class A { public: A(int a); A(const & A); //Copy constructor; A & operator = (const A & a); // overload = ~A(); // Destructor;..... private: int size_A; int *array_A; }; // Derived class class B : public A { public: B(int b); B(const & B); //Copy constructor; B & operator = (const B & b); // ~B(); // Destructor;..... private: int size_B; int *array_B; }; In private section!! Use A’s constructor

9 Inheritance and copy Constructor // Derived class class B : public A { public: B(int b); B(const & B); //Copy constructor; B & operator = (const B & b); // ~B(); // Destructor;..... private: int size_B; int *array_B; }; // Derived class B::B(const B & b) :A(b) // call the copy constructor of A; { size_B = b.size_B; array_B = new int[size_B]; for (int i=0; i<size_B; i++) array_B[i] = b. array_B[i]; }

10 Inheritance and assignment = // Derived class class B : public A { public: B(int b); B(const & B); //Copy constructor; B & operator = (const B & b); // ~B(); // Destructor;..... private: int size_B; int *array_B; }; // Derived class B & B::operator = (const B & b) { A::operator=(b); if (size_B != b.size_B()) { delete [] array_B; array_B = new int[size_B]; }; for (int i =0; i<size_B; i++) array_B[i] = b.array_B[i]; return *this; } This will take care of the variables in the base class

11 Inheritance and destructor // Derived class class B : public A { public: B(int b); B(const & B); //Copy constructor; B & operator = (const B & b); // ~B(); // Destructor;..... private: int size_B; int *array_B; }; // Derived class B::~B() { delete [] array_B; }