CS212: Object Oriented Analysis and Design Lecture 5: Classes and Objects - II.

Slides:



Advertisements
Similar presentations
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
Advertisements

Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Chapter 14: Overloading and Templates
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
7M701 1 Class Diagram advanced concepts. 7M701 2 Characteristics of Object Oriented Design (OOD) objectData and operations (functions) are combined 
Classes: A Deeper Look Systems Programming.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Lecture 9 Concepts of Programming Languages
More C++ Classes Systems Programming. Systems Programming: C++ Classes 2 Systems Programming: 2 C++ Classes  Preprocessor Wrapper  Time Class Case Study.
Chapter 13: Overloading.
Abstract Data Types and Encapsulation Concepts
Chapter 15: Operator Overloading
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
Review of C++ Programming Part II Sheng-Fang Huang.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Chapter 12: Adding Functionality to Your Classes.
Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
CS212: Object Oriented Analysis and Design Lecture 4: Objects and Classes - I.
More C++ Classes Systems Programming. C++ Classes  Preprocessor Wrapper  Time Class Case Study –Two versions (old and new)  Class Scope and Assessing.
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.
CSCI-383 Object-Oriented Programming & Design Lecture 14.
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.
Learners Support Publications Classes and Objects.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
CS212: Object Oriented Analysis and Design Lecture 2: Introduction to C++
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Copyright 2008 Oxford Consulting, Ltd 1 October C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
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.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
CS212: Object Oriented Analysis and Design Lecture 16: Runtime Polymorphism.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
CITA 342 Section 1 Object Oriented Programming (OOP)
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
1 CS Programming Languages Class 22 November 14, 2000.
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Object-Oriented Programming (OOP) and C++
C++ General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Chapter 11: Abstract Data Types Lecture # 17. Chapter 11 Topics The Concept of Abstraction Advantages of Abstract Data Types Design Issues for Abstract.
Chapter 13: Overloading and Templates
Abstract Data Types and Encapsulation Concepts
Review: Two Programming Paradigms
Lecture 9 Concepts of Programming Languages
CS212: Object Oriented Analysis and Design
Abstract Data Types and Encapsulation Concepts
CS212: Object Oriented Analysis and Design
More Object-Oriented Programming
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Chapter 11: Inheritance and Composition
Submitted By : Veenu Saini Lecturer (IT)
NAME 436.
Lecture 10 Concepts of Programming Languages
More C++ Classes Systems Programming.
CSG2H3 Object Oriented Programming
Lecture 9 Concepts of Programming Languages
Presentation transcript:

CS212: Object Oriented Analysis and Design Lecture 5: Classes and Objects - II

Books and References C++ complete reference: Herbert Schildt Object Oriented Programming with C++: E balagurusamy C++ How to Program, Deitel and Dietel

Recap of Lecture 3 Class identification and CRC chart Defining classes in C++ Execution of blue print Access specifiers – Public, private, protected Getter and setter function Separating interface from implementation

Today’s objective Scope of class members Nesting member function Class members and arrays Static class members Friendly classes and function

Data hiding in classes CLASS Data Function Data Function Private Area Public Area Entry allowed to public area

Scope of class members Scope is an enclosing context where values and expressions are associated Scope resolution operator helps to identify and specify the context to which an identifier refers Scope resolution operator is written as " :: ". It is used to qualify hidden names so that you can still use them It is a unary scope operator

Scope resolution Global variables and local variables Class members Member variable Member functions

Nesting of member functions Member functions can be called by another member function of the same class The private data member remains safely encapsulated Get and set function helps the client to interact with the object Demonstration

Private member function Usually member data are made private while functions (or methods) are made public. You may not want the user to directly access these functions Private functions can only be called from within public member functions. These functions are also called ‘helper functions’ Demonstration

Memory Allocation for Objects Common for all objects Member function 1 Member function 2 Object 1Object 2Object 3 Member variable 1 Member variable 2 Memory created when functions defined Memory created when objects defined

Class members as arrays Arrays can be used as member variables of a class Individual array is created in the memory for each objects Caution: Segmentation fault Dynamic memory allocation Copying data Demonstration

Static Member Variable A data member of a class can be static A static member has following characteristics It is initialized when first object is created Only one copy of that member is created The member is shared by all the objects of the class Visible only within the class Lifetime is entire program A global definition must be provided

Static member function Member functions can also be static Restrictions: Only static member variables are accessible (apart from global ones) Does not have a this pointer Static and non-static of same function is not allowed May not be virtual, constant Demonstration

Friend function A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions. A friend can be a function, function template, or member function, or a class or class template

Properties of friend functions Friend of the class can be member of some other class. Friend of one class can be friend of another class or all the classes in one program: GLOBAL FRIEND. Can access the private or protected members of the class in which they are declared to be friend, but they can use the members for a specific object Do not get “this” pointer. Can be friend of more than one class, hence they can be used for message passing between the classes. Can be declared anywhere (in public, protected or private section) in the class.

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

Friend Class Friendship may allow a class to be better encapsulated by 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

Logistic Group No. Roll NumbersDay of WeekTime 1 B14CS001 – 043, B14SS002 – 017, B14BS011 – 015 THURSDAY1 P.M. 2 UG – 043, UG – 031, UG – 08, FRIDAY1 P.M.

Thank you Next Lecture: Constructor and destructors