Recap Stale Pointers and Double Delete Reference Variables Reference Type vs Pointer Type Structures Pointers to Structures Exogenous vs Indigenous Data.

Slides:



Advertisements
Similar presentations
Lecture 5: Interfaces.
Advertisements

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Using Templates Object-Oriented Programming Using C++ Second Edition 11.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 12: Adding Functionality to Your Classes.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Learners Support Publications Classes and Objects.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4 – August 30, 2001.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 10 Introduction to Classes
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
1 Using Templates COSC 1567 C++ Programming Lecture 10.
1 Chapter 1 C++ Basics Review Reading: Sections 1.4 and 1.5.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
Introduction to Object-Oriented Programming Lesson 2.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
The const Keyword Extreme Encapsulation. Humble Beginnings There are often cases in coding where it is helpful to use a const variable in a method or.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Chapter 2 Objects and Classes
Classes (Part 1) Lecture 3
Chapter 7: User-Defined Functions II
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.
Java Programming: Guided Learning with Early Objects
Java Primer 1: Types, Classes and Operators
Lecture 13.
Chapter 3: Using Methods, Classes, and Objects
Chapter 2 Objects and Classes
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Classes and Objects.
Defining Classes and Methods
Java Programming Language
Presentation transcript:

Recap Stale Pointers and Double Delete Reference Variables Reference Type vs Pointer Type Structures Pointers to Structures Exogenous vs Indigenous Data Shallow vs Deep Copying Linked Lists Common Errors

Selective Questions from Exercise

Q: Name and illustrate five operations that can be applied to pointers Answer: Pointers can be declared and initialized They can be assigned to point at an object They can be dereferenced They can be involved in arithmetic The address-of operator can be applied to a pointer in the same manner as any other object

Q: Consider int a, b; int *ptr; / / A pointer int **ptrPtr; / / A pointer to a pointer ptr = &a; ptr Ptr = &ptr; a. Is this code legal? Answer: Yes b. What are the values of * ptr and * * ptrPtr ? Answer: Both have the same value as A

Continued…. c. Using no other objects besides those already declared, how can you alter ptrPtr so that it points at a pointer to b without directly touching ptr ? Answer: *ptrPtr=&b d. Is the following statement legal? ptrPtr = ptr ; Answer: No; these objects have different types.

Q: a. Is *&x always equal to x? If not, give an example. Answer: Yes, as long as x is an object b. Is & *x always equal to x? If not, give an example. Answer: No, because x might not be a pointer

Q: Is the following code legal? Why or why not? int a = 3; const int & b = a; Answer: This is perfectly legal. However if the const is moved from the second declaration to the first, then the declaration and initialization of b would be illegal.

Q: What is wrong with omitting spacing in *x/ * y? Answer: /* begins a comment.

Chapter 2 Objects and Classes

Introduction In this chapter we will study how C++ uses the class to achieve encapsulation and information hiding; how classes are implemented; and several examples of classes, including classes used to manipulate rational numbers and strings

Object-Oriented Programming Object-oriented programming appears to be emerging as the dominant paradigm At the heart of object-oriented programming is the object An object is an entity-an instance of a data type-that has structure and state Each object defines operations that may access or manipulate that state User-defined type should behave in the same way as predefined types

Continued…. Following points can be taken for granted when dealing with basic data types of a language We can declare new objects, possibly with initialization. We can copy or test for equality We can perform input and output on these objects If the object is an automatic variable, then when the function it is declared in terminates the object goes away We can perform type conversions when appropriate, and the compiler complains when they are inappropriate

Continued…. Object can be view as an atomic unit, whose parts cannot be dissected by the general user The atomicity principle is known as information hiding In other words, the user does not have direct access to the parts of the object nor their implementations They can be accessed only indirectly by functions supplied with the object The grouping of data and the operations that apply to them to form an aggregate, while hiding implementation details of the aggregate, is known as encapsulation

Continued…. Another important goal of object-oriented programming is to support code reuse Just as engineers use components over and over in their designs, programmers should be able to reuse objects rather than repeatedly re-implementing them When we have an implementation of the exact object that we need to use, doing so is a simple matter The challenge is to use an existing object when the object needed is not an exact match but is merely very similar

Inheritance Mechanism The inheritance mechanism allows us to extend the functionality of an object We can create new types with extended properties of the original type Inheritance goes a long way toward our goal of code reuse

Polymorphism Mechanism Another important object-oriented principle is polymorphism A polymorphic object can hold objects of several different types When operations are applied to the polymorphic type, the operation appropriate to the actual stored type is automatically selected In C++ polymorphism is implemented as part of inheritance Polymorphism allows us to implement new types (classes) that share common logic The use of inheritance to create these hierarchies distinguishes object-oriented programming from object-based programming, which involves the use of encapsulation and information hiding but not inheritance.

Class A class is the same as a structure except that, by default, all members are inaccessible to the general user of the class Because functions that manipulate the object's state are members of the class, they are accessed by use of the dot member operator (. )-just like any other structure member-and thus are called member functions These functions are also called methods

Basic class Syntax

Class Members A class in C++ consists of its members These members can be either data or functions The functions are called member functions Each instance of a class is an object Each object contains the data components specified in the class A member function is used to act on an object Member functions are also called methods

1 // A class for simulating an integer memory cell. 2 3 class IntCell 4 { 5 public: 6 7 // Construct the IntCell. Initial value is 0. 8 Intcell ( ) 9 { storedvalue = 0; } // Construct the IntCell. Initial value is initialvalue. 12 IntCell( int initialvalue ) 13 { storedvalue = initialvalue; } // Return the stored value. 16 int read( ) 17 { return storedvalue; } // Change the stored value to x. 20 void write( int x ) 21 { storedvalue = x; } private: 24 int storedvalue; 25 };

Brief Explanation of Example In this example, there are four methods Two of these methods are read and write The other two are special methods known as constructors The labels public and private determine the visibility of class members Everything except the storedValue data member is public while storedValue is private A public member is visible to all routines and may be accessed by any method in any class A private member is not visible to non-class routines and may be accessed only by methods in its class

Continued…. Data members are typically declared private, thus restricting access to internal details of the class, while methods intended for general use are made public Restricting access is also known as information-hiding By using private data members, we can change the internal representation of object, without affecting other parts of the program that use the object It is possible because the object is accessed through the public member functions, whose viewable behavior remains unchanged

Continued…. The users of the class do not need to know the internal details of how the class is implemented In many cases having this access leads to trouble For instance, in a class that stores dates by month, day, and year, if we make the month, day, and year private, we prohibit an outsider from setting these data members to illegal dates, such as February 29, 2001 Methods strictly for internal use can be private. In fact, in a class, all members are private by default, so the initial public is required

Continued…. A constructor is a method that describes how an instance of the class is created If no constructor is explicitly defined, one that initializes the data members using language defaults is automatically generated The IntCell class defines two constructors The first is called if no parameter is specified The second is called if an int parameter is provided and uses that int to initialize the storedValue member If the declaration of an IntCell object does not match any of the known constructors, the compiler complains

Extra Constructor Syntax and Accessors Default Parameters Initializer List The explicit Constructor Constant Member Function

Default Parameters The IntCell constructor illustrates the default parameter As a result, two IntCell constructors are still defined One accepts an initialValue The other is the zero-parameter constructor, which is implied because the one-parameter constructor says that initialValue is optional by having a default value The default value of 0 signifies that 0 is used if no parameter is provided Default parameters can be used in any function, but they are most commonly used in constructors

IntCell Class with Revision 1 // A class for simulating an integer memory cell. 2 3 class IntCell 4 { 5 public: 6 explicit IntCell( int initialvalue = 0 ) 7 : storedValue( initialvalue ) { } 8 int read( ) const 9 { return storedvalue; } 10 void write( int x ) 11 { storedvalue = x; } private: 14 int storedvalue; 15 };

Initializer List The Intcell constructor uses an initializer list prior to the body of the constructor as shown in Line 7 of previous example The initializer list is used to specify non-default initialization of each data member in an object directly Using initializer lists instead of an assignment statement in the body saves time in the previous code when the data members are class types that have complex initializations In some cases it is required For instance If a data member is const, then the data member's value can be initialized only in the initializer list. Also, if a data member is itself a class type that does not have a zero-parameter constructor, then it must be initialized in the initializer list

The explicit Constructor The IntCell constructor is explicit All one-parameter constructors should generally be made explicit to avoid behind the scenes type conversions Otherwise, C++'s somewhat lenient rules allow type conversions without explicit casting operations Usually, this behavior is unwanted as it destroys strong typing and can lead to hard-to-find bugs

Continued…. Consider the following: IntCell obj; // obj is an IntCell obj = 37; / / Should not compile: type mismatch This code fragment constructs an Intcell object obj and then performs an assignment statement But the assignment statement should not work, because the right-hand side of the assignment operator is not another Intcell Instead, obj's write method should have been used

Continued…. A one-parameter constructor defines an implicit type conversion, in which a temporary object is created that makes an assignment compatible In this case, the compiler would attempt to convert obj = 37; // Should not compile: type mismatch to IntCell temp = 37; obj = temp; So, the one-parameter constructor can be used to construct temp The use of explicit means that a one-parameter constructor cannot be used to generate an implicit temporary Thus, because Intcell's constructor is declared explicit, the compiler will correctly complain of a type mismatch.

Constant Member Function A method that examines but does not change the state of its object is an accessor A member function that changes the state of an object is a mutator For instance: isEmpty is an accessor makeEmpty is a mutator

Continued…. In C++, each member function is marked as being an accessor or a mutator This is an important part of the design process and should not be viewed as simply a comment Indeed, not doing so has important semantic consequences For instance Mutators cannot be applied to constant objects. By default, all member functions are mutators. To make a member function an accessor, we must add the keyword const after the closing parenthesis that ends the parameter type list. The result is a constant member function A constant member function is a function that does not change any class data members

Continued…. The const-ness is part of the signature, and const can have many different meanings The function declaration can have const in three different contexts Only the const after a closing parenthesis signifies an accessor second is parameter passing third is the return type The function signature includes the types of parameters, including const and & directives, but not the return type