CS212: Object Oriented Analysis and Design Lecture 14: Reusing classes in C++

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a.
Introduction to Programming Lecture 39. Copy Constructor.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
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.
Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
OOP Etgar 2008 – Recitation 61 Object Oriented Programming Etgar 2008 Recitation 6.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
OOP Spring 2007 – Recitation 41 Object Oriented Programming Spring 2007 Recitation 4.
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
Review of C++ Programming Part II Sheng-Fang Huang.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Chapter 12: Adding Functionality to Your Classes.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
INHERITANCE IN C++ 3 CSC1201: PROGRAMMING LANGUAGE 2 ASEEL ALHADLAQ_KSU 1.
CS212: Object Oriented Analysis and Design Lecture 4: Objects and Classes - I.
Learners Support Publications edited by Taranjit singh Aulakh, BGIET sangrur,CSE deptt Constructors and Destructors.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations.
CS212: Object Oriented Analysis and Design Lecture 13: Relationship between Classes.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 23 November 19, 2009.
CS212: Object Oriented Analysis and Design
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Order of Constructor Call. Base class constructors are always called in the derived class constructors. Whenever you create derived class object, first.
1 CSC241: Object Oriented Programming Lecture No 02.
Object Management. Constructors –Compiler-generated –The Initializer List –Copy Constructors –Single-arg (conversion ctors) The Assignment Operator.
Programming Fundamentals1 Chapter 8 OBJECT MANIPULATION - INHERITANCE.
Object Oriented Programming COP3330 / CGS5409.  Inheritance  Assignment 5.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Introduction to Object-Oriented Programming Lesson 2.
CS212: Object Oriented Analysis and Design Lecture 16: Runtime Polymorphism.
Inheritance and Composition Reusing the code and functionality Unit - 04.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
Inheritance in C++ 2 CSC1201: Programming Language 2 1Aseel AlHadlaq_KSU.
Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
1 CSC241: Object Oriented Programming Lecture No 17.
1 Introduction to Object Oriented Programming Chapter 10.
C:\Temp\Templates 4 5 Use This Main Program 6.
Monday, Jan 27, 2003Kate Gregory with material from Deitel and Deitel Week 4 Questions from Last Week Hand in Lab 2 Classes.
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Learners Support Publications Constructors and Destructors.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
CS1201: Programming Language 2 Classes and objects Inheritance Nouf Aljaffan Edited by : Nouf Almunyif.
Seventh step for Learning C++ Programming CLASS Declaration Public & Private Constructor & Destructor This pointer Inheritance.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Chapter 2 Objects and Classes
Constructors and Destructors
CS212: Object Oriented Analysis and Design
Polymorphism.
group work #hifiTeam
Can perform actions and provide communication
CS1201: Programming Language 2
Lecture 22 Inheritance Richard Gesick.
Local Variables, Global Variables and Variable Scope
Constructors and destructors
Constructors and Destructors
Object Oriented Programming
CS148 Introduction to Programming II
Java Programming Language
CMSC202 Computer Science II for Majors Lecture 10 and 11 – Inheritance
COP 3330 Object-oriented Programming in C++
Presentation transcript:

CS212: Object Oriented Analysis and Design Lecture 14: Reusing classes in C++

Recap of lecture 13 Relationship between classes Types of relationship Association Inheritance Genericity

Outline of Lecture 14 Reusing classes in C++ Composition Inheritance Constructor and destructor

Introduction Fascinating feature of C++ is code reuse. To do a lot more than copy code and change it. Use existing classes that someone else has built Composition and Inheritance Source: Tech Comics

Composition A straightforward method Create objects of your existing class inside the new class. The new class is composed of objects of existing classes Composing classes primarily with built-in types Containment, Aggregation, Sub-object Demonstration

Inheritance Mechanism for deriving new classes from existing classes. Base class/ Superclass Sub-class/ derived class Generalization Specialization

Inheritance Syntax There’s a new and different form State this in code by giving the name of the class Before the opening brace of the class body, put a colon Followed by the name of the base class(es) class derived-class-name : access base-class-name { // body of class }; “This new class is like that old class.”

Constructor & Member initializer list It is important to guarantee proper initialization Constructors for all of its sub-objects are to be called Derived class constructor doesn’t have permission to access the private data elements So it can’t initialize them directly Solution: Constructor initializer list Demonstration

Initializing base class members Initialize Base class data member when we create a Derived object Restrictions on how to initialize inherited member variables. The value of a variable can only be set in an initialization list of a constructor belonging to the same class as the variable Ensures that all variables are initialized only once.

Example class Derived: public Base { public: double m_dValue; Derived(double dValue=0.0, int nValue=0) : m_dValue(dValue), m_nValue = nValue { } }; Derived(double dValue=0.0, int nValue=0) : m_dValue(dValue) { m_nValue = nValue; }

Passing parameter to Base class Expanded form of the derived class's constructor declaration derived-constructor(arg-list) : base1(arg-list), base2(arg-list), //... baseN(arg-list) { // body of derived constructor } Derived(double dValue=0.0, int nValue=0) : Base(nValue), // Call Base(int) constructor m_dValue(dValue) { } Demonstration

What about member objects of built-in types? No object (or part of an object) can get out of the starting gate without its constructor being called. “Pseudo-constructor calls” is to perform a simple assignment. class X { int i; float f; char c; char* s; public: X() : i(7), f(1.4), c('x'), s("howdy") {} };

Composition & Inheritance Like some class(es) and composed of some class(es) B C A C inherits from B and has a member object (“is composed of”) of type A. Demonstration

Destructor calls To make explicit constructor calls in the initializer list Never need to make explicit destructor calls Compiler still ensures that all destructors are called Starting with the most-derived destructor and working back to the root Order of constructor & destructor calls

Some common mistakes int main(){ int a, *pa, &ra; pa = &a; a=++a + a++; ra = a; cout <<"a= "<<a <<"; *pa= "<<*pa <<"; ra= "<<ra ; } #define CIRCUM(R)(3.14*R*R); int main() { float r=1.0,c; c = CIRCUM(r); cout << "\n" << c << endl; if(CIRCUM(r)==6.28) cout <<"Hello student"<<endl; }

Mid-Sem II Average: 9Max: 18Min: 2

Thank you Next Lecture: Inheritance in C++