CS212: Object Oriented Analysis and Design

Slides:



Advertisements
Similar presentations
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Advertisements

Class and Objects.
OOP Using Classes - I. Structures vs. Classes C-style structures –No “interface” If implementation changes, all programs using that struct must change.
Classes: A Deeper Look Systems Programming.
A Deeper Look at Classes CS-2303, C-Term A Deeper Look at Classes CS-2303 System Programming Concepts (Slides include materials from The C Programming.
Lecture 9 Concepts of Programming Languages
Review of C++ Programming Part II Sheng-Fang Huang.
OOP Languages: Java vs C++
Chapter 12: Adding Functionality to Your Classes.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
CS212: Object Oriented Analysis and Design Lecture 5: Classes and Objects - II.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
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.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Learners Support Publications Constructors and Destructors.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Chapter 12 Classes and Abstraction
Constructors and Destructors
Eine By: Avinash Reddy 09/29/2016.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Pointers and Dynamic Arrays
Abstract Data Types and Encapsulation Concepts
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Andy Wang Object Oriented Programming in C++ COP 3330
Programming with ANSI C ++
Review: Two Programming Paradigms
Introduction to Classes
Object Lifetime and Dynamic Objects
Constructor & Destructor
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Class: Special Topics Copy Constructors Static members Friends this
Structs.
This pointer, Dynamic memory allocation, Constructors and Destructor
Lecture 9 Concepts of Programming Languages
CS212: Object Oriented Analysis and Design
Constructor & Destructor
Introduction to Classes
Abstract Data Types and Encapsulation Concepts
10.2 const (Constant) Objects and const Member Functions
Dr. Bhargavi Dept of CS CHRIST
Constructors and destructors
Constructors and Destructors
Classes and Objects.
9-10 Classes: A Deeper Look.
Recitation Course 0520 Speaker: Liu Yu-Jiun.
CS410 – Software Engineering Lecture #5: C++ Basics III
Chapter 9 Introduction To Classes
Destructors, Copy Constructors & Copy Assignment Operators
Destructors, Copy Constructors & Copy Assignment Operators
Constructors & Destructors
More C++ Classes Systems Programming.
9-10 Classes: A Deeper Look.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
SPL – PS3 C++ Classes.
Object Oriented Programming
Lecture 9 Concepts of Programming Languages
Presentation transcript:

CS212: Object Oriented Analysis and Design Friends, Constructors and Destructors (Using C++)

Object Oriented Anslysis and Design (CS 212) Recap of last week Class and Objects Identification Design of attributes and functionalities Getter and Setters Class Interface Object Oriented Anslysis and Design (CS 212)

Class Interface Diagram Time class Set Private data: hrs mins secs Increment Write Time Time Object Oriented Anslysis and Design (CS 212)

Object Oriented Anslysis and Design (CS 212) Friend function A friend function of a class is defined Outside that class' scope Has the right to access all private and protected members of the class. Friends are not member functions. Their prototypes appear in the class definition. A friend can be a function, function template, or member function, or a class or class template Object Oriented Anslysis and Design (CS 212)

Properties of friend functions Friend of the class can be member of more than one class. Friend of one class can be GLOBAL FRIEND. Can access the private or protected members of the friend. Do not get “this” pointer. They can be used for message passing between the classes. Can be declared anywhere in the class (in public, protected or private section). Object Oriented Anslysis and Design (CS 212)

When to use friend function Three different circumstances where friend functions are useful Operator overloading - for certain types of operators Creation of I/O operations Multiple classes share common functionality Object Oriented Anslysis and Design (CS 212)

Object Oriented Anslysis and Design (CS 212) Friend Class Friendship may allow a class to be better encapsulated Granting per-class access to parts of its API That would otherwise have to be public. This increased encapsulation comes at the cost of tighter coupling between classes Friendships are not symmetric Friendships are not transitive Friendships are not inherited Access due to friendship is inherited Object Oriented Anslysis and Design (CS 212)

Construction and Destruction Object Oriented Anslysis and Design (CS 212)

Object Oriented Anslysis and Design (CS 212) Constructors void foo(){ int n = 5; double z[10] = { 0.0 }; struct gizmo { int i, j; } w = { 3, 4 }; ····· } All of the variables are created at block entry when foo() is invoked. Uses a runtime system stack. The class needs a mechanism to specify object creation and destruction so that a client can use objects like native types. Object Oriented Anslysis and Design (CS 212)

Object Oriented Anslysis and Design (CS 212) Constructors It is a member function whose name is the same as the class name It creates objects of the class type It involves initializing data members Allocating storage from the heap by using new. The simplest use of a constructor is for initialization. Demonstration Object Oriented Anslysis and Design (CS 212)

Object Oriented Anslysis and Design (CS 212) Implicit Constructor An implicitly-declared default constructor is an inline public member of its class It performs the initialization operations That are needed to create an object of this type. These operations do not involve initialization of user-declared data members or allocation of memory from the free store Demonstration Object Oriented Anslysis and Design (CS 212)

Parameterized Constructors It may be necessary to initialize different member variables with different values To achieve this we can pass arguments to a constructor The constructor that can take arguments are called parameterized constructors Implicit and Explicit calling of parameterized constructors Demonstration Object Oriented Anslysis and Design (CS 212)

Multiple Constructors in a Class There can be multiple constructors in a class Each function has its own significance In some cases it is necessary to have both parameterized and default constructors Avoid Reduplicating Identical Pieces Of Constructors' Code Object Oriented Anslysis and Design (CS 212)

Efficient constructor design class string { private: char * pc; size_t capacity; size_t length; enum { DEFAULT_SIZE = 32}; public: string(const char * s); string(size_t initial_capacity ); string(); //... }; Object Oriented Anslysis and Design (CS 212)

Object Oriented Anslysis and Design (CS 212) Efficient … (contd.) class string { private: char * pc; size_t capacity; size_t length; enum { DEFAULT_SIZE = 32}; // following function is called by every constructor void init( size_t cap = DEFAULT_SIZE); public: string(const char * s); string(size_t initial_capacity ); string(); //...other member functions }; void string::init( size_t cap) { pc = new char[cap]; capacity = cap; } Object Oriented Anslysis and Design (CS 212)

Object Oriented Anslysis and Design (CS 212) Efficient … (contd.) string::string(const char * s){ size_t size = strlen (s); init(size + 1); length = size; strcpy(pc, s); } string::string(size_t initial_capacity ){ init(initial_capacity); length=0; string::string(){ init(); length = 0; Object Oriented Anslysis and Design (CS 212)

Object Oriented Anslysis and Design (CS 212) Default Argument class Fraction{ private:     int m_nNumerator;     int m_nDenominator;   public:     // Default constructor     Fraction(int nNumerator=0, int nDenominator=1) {         m_nNumerator = nNumerator;         m_nDenominator = nDenominator;     }     int GetNumerator() { return m_nNumerator; }     int GetDenominator() { return m_nDenominator; } }; Object Oriented Anslysis and Design (CS 212)

Object Oriented Anslysis and Design (CS 212) A special case Applicable to constructor with exactly one parameter Implicit type conversion Demonstration Object Oriented Anslysis and Design (CS 212)

Is A Default Constructor Always Necessary? The existence of a user-defined constructor blocks the synthesis of an implicitly-declared default constructor. A class with no default constructor limits its users to a narrower set of allowed uses. Demonstration Object Oriented Anslysis and Design (CS 212)

Object Oriented Anslysis and Design (CS 212) Constant Object Class objects can be made constant using the const keyword Constant variables must be initialized at time of creation In the case of classes, this initialization is done via constructors Once initialized via constructor, any attempt to modify the member variables of the object is disallowed It would violate the const(ant)-ness of the object Object Oriented Anslysis and Design (CS 212)

Object Oriented Anslysis and Design (CS 212) Constant object Changing member variables directly (if they are public) is not allowed Calling member functions that sets the value of member variables is not allowed const class objects can only call const member functions Object Oriented Anslysis and Design (CS 212)

Object Oriented Anslysis and Design (CS 212) Trivial Constructors Compilers synthesize a default constructor for every class unless a constructor was already defined by the user Such a synthesized constructor is redundant A constructor is considered trivial when all the following hold true: Its class has no virtual member functions and no virtual base classes. All the direct base classes of the constructor's class have trivial constructors. All the member objects in the constructor's class have trivial constructors. Object Oriented Anslysis and Design (CS 212)

Object Oriented Anslysis and Design (CS 212) Destructors It is a member function whose name is the class name preceded by the tilde ~ character A destructor’s usual purpose is finalizing or destroying objects of the class type. Finalizing objects involves retrieving resources allocated to the object. Requires using delete to deallocate store assigned to the object. They can not be overloaded or take arguments Object Oriented Anslysis and Design (CS 212)

When constructor and destructors are called An object's constructor is called when the object comes into existence An object's destructor is called when the object is destroyed For a local object: constructor  object's declaration, destructor  reverse order of constructor For a global object: constructor  before main(), , in order of their declaration, within the same file destructor  reverse order of constructor, after main() has terminated Object Oriented Anslysis and Design (CS 212)

Object Oriented Anslysis and Design (CS 212) Thank you Next Lecture: Polymorphism Object Oriented Anslysis and Design (CS 212)