Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.

Slides:



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

C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Road Map Introduction to object oriented programming. Classes
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Abstract Data Types and Encapsulation Concepts
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Pointer Data Type and Pointer Variables
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4 – August 30, 2001.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Concordia TAV 2002 Comp5421_421 Comp5421 Object Oriented Programming Using C++ Efficiently Lecture 4 (2) Tianxiang Shen Summer 2002 Department of Computer.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract 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.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
1 Chapter 1 C++ Basics Review Reading: Sections 1.4 and 1.5.
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.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.
Introduction to Object-Oriented Programming Lesson 2.
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.
Recap Stale Pointers and Double Delete Reference Variables Reference Type vs Pointer Type Structures Pointers to Structures Exogenous vs Indigenous Data.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
1 Chapter 1 C++ Templates Readings: Sections 1.6 and 1.7.
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.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Object Access m1.write(44); m2.write(m2.read() +1); std::cout
Chapter 2 Objects and Classes
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
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.
Chapter 1 C++ Basics Review
Introduction to C++ and Algorithm Analysis
Chapter 3: Using Methods, Classes, and Objects
Chapter 5 Classes.
Pointers Revisited What is variable address, name, value?
Chapter 2 Objects and Classes
Dynamic Memory Allocation
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Introduction to Classes
Abstract Data Types and Encapsulation Concepts
Defining Classes and Methods
9-10 Classes: A Deeper Look.
CS410 – Software Engineering Lecture #5: C++ Basics III
CMSC 341 C++ and OOP.
CMSC 341 C++ and OOP.
CMSC 341 C++ and OOP.
CMSC 341 C++ and OOP.
CMSC 341 C++ and OOP.
9-10 Classes: A Deeper Look.
SPL – PS3 C++ Classes.
Presentation transcript:

Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I

Purpose of C++ Review Brush up on basic C++ Become familiar with Weiss’s style Introduce specific constructs useful for implementing data structures

Topics Classes and Objects Extra Syntax  Default Parameters  Initializer List  explicit Constructor  Constant Member Function Separation of Interface and Implementation  Preprocessor Directives  Scoping Operator

Topics (cont’d) C++ Details  Pointers  Assignment and Comparison of Pointers  Address-Of Operator

Classes and Objects A class in C++ consists of its members A class in C++ is an encapsulation of data members and member functions that manipulate the data Data members ≡ properties Member functions ≡ methods Encapsulation = data + methods Each instance of a class is an object. Each object contains the data components (except static data members) specified in the class Member functions are used to act on an object

Classes and Objects (cont’d) class IntCell { public: IntCell() { storedValue = 0; } IntCell(int initialValue) { storedValue = initialValue; } int read() { return storedValue; } void write(int x) { storedValue = x; } private: int storedValue; }; Class name 4 Methods (2 constructors, 1 read method, 1 write method) Can be public or private that determines visibility of class members Data member Information Hiding

Classes and Objects (cont’d) IntCell obj1; // Declares an instance/object obj1 of // class IntCell IntCell obj2; storedValue 0 obj1storedValue 0 obj2

Classes and Objects (cont’d) There are 4 methods  2 constructors  The read method  The write method Members of a class can be  Public  Private Public members may be accessed by any method in any class Private members may only be accessed by methods in the class Determine visibility of class members

Classes and Objects (cont’d) Private allows for information-hiding Private restricts access to internal details of the class With private data members, we can change the internal representation of the object without having an effect on other parts of the program that use the object Ideally, the users of the class do not need to know the internal details of how the class is implemented Member functions (Methods) can also be private In a class, all members are private by default

Classes and Objects (cont’d) Constructor  A method that describes how an instance of the class is constructed If no explicit constructor is defined, one that initializes the data members using language defaults is automatically generated IntCell obj3; IntCell obj4(5); storedValue 0 obj3storedValue 5 obj4

Extra Syntax Default parameters Initializer list Explicit constructor Constant member function  Accessor methods  Mutator methods

Extra Syntax: Default Parameters class IntCell { public: explicit IntCell(int initialValue = 0) : storedValue(initialValue) { } int read() const { return storedValue; } void write(int x) { storedValue = x; } private: int storedValue; }; Default Parameters Initializer List Explicit Constructor Accessor Mutator

Extra Syntax: Default Parameters (cont’d) explicit IntCell(int initialValue = 0) : storedValue(initialValue) { } There are still 2 constructors  One is the zero-parameter constructor  One that accepts an initial value storedValue 0 obj5storedValue 7 obj6 IntCell obj5; IntCell obj6(7); The default value of 0 signifies that 0 is used if no parameter is provided

Extra Syntax: Initializer List explicit IntCell(int initialValue = 0) : storedValue(initialValue) {} Initializer list is used to initialize the data members directly Initializer list is more efficient than an assignment statement in the body of the constructor Initializer List

Extra Syntax: Initializer List (cont’d) When is initializer list required?  If a data member is declared const  If a data member is itself a class type that does not have zero-parameter constructor

Extra Syntax: explicit Constructor You should make all one-parameter constructor explicit to avoid behind-the-scene type conversions. If the constructor is explicit IntCell obj; obj = 37; // Will not compile because of // type mismatch (which is what we want)

Extra Syntax: explicit Constructor (cont’d) If the constructor is NOT explicit IntCell obj; obj = 37; // Will compile // A bad thing since 37 is not an IntCell What actually happens: IntCell temp = 37; obj = temp;

Extra Syntax: Constant Member Function An accessor is a member function that examines but does not change the state of its object A mutator is a member function that changes the state of its object

Extra Syntax: Constant Member Function (cont’d) class IntCell { public: IntCell() { storedValue = 0; } IntCell(int initialValue) { storedValue = initialValue; } int read() { return storedValue; } void write(int x) { storedValue = x; } private: int storedValue; }; Accessor Mutator

Extra Syntax: Constant Member Function (cont’d) By default, all member functions are mutator To make a member function an accessor, add the keyword const after the closing parenthesis that ends the parameter type list

Extra Syntax: Constant Member Function (cont’d) int read() const { return storedValue; } int read() const { storedValue = 3; // Will not compile provided storedValue is not declared mutable return storedValue; }

Separation of Interface and Implementation It is a good idea to separate the class interface and its implementation The interface lists the class and its members (data and functions) The implementation provides the implementation of the functions The class interface is placed in a file that ends with.h The class implementation is placed in a file that ends with.cpp

Separation of Interface and Implementation (cont’d) #ifndef IntCell_H #define IntCell_H class IntCell { public: explicit IntCell(int initialValue = 0); int read() const; void write(int x); private: int storedValue; }; #endif IntCell.h

Separation of Interface and Implementation (cont’d) #include “IntCell.h” IntCell::IntCell(int initialValue) : storedValue(initialValue) { } int IntCell::read() const { return storedValue; } void IntCell::write(int x) { storedValue = x; } IntCell.cpp

Separation of Interface and Implementation (cont’d) #include #include “IntCell.h” using namespace std; int main() { IntCell m; m.write(5); cout << “Cell contents: ” << m.read() << endl; return 0; } main.cpp

Preprocessor Directives The preprocessor directives  #ifndef IntCell_H  #define IntCell_H are used to avoid reading the interface twice during compilation

Scoping Operator Denoted by :: Used by each member function in the implementation file to identify the class it is part of int IntCell::read() const { return storedValue; }

Scoping Operator (cont’d.) Syntax: ClassName::member The signature of an implemented member function must match exactly the signature listed in the class interface. Note: const is part of signature.

C++ Details Pointers Parameter passing Return passing Reference variables Destructor, copy constructor, operator=

C++ Details: Pointers A pointer variable is a variable that stores the address where another object resides in memory. For example: int *p; // p is a pointer variable // p points to an integer // The value of p is the address of the // integer it points to.

C++ Details: Pointers (cont’d) int *p; // p is a pointer variable // p points to an integer // The value of p is the address of the // integer it points to Memory p 5 p

C++ Details: Address-Of Operator Denoted: & Returns the memory address where an object resides For example int x = 5; int *p; p = &x;

C++ Details: Pointers (cont’d) IntCell *m; // m is a pointer variable. // It can point to an IntCell object. // m is not yet initialized. // Never ever use an uninitialized pointer. // Can initialize m to NULL m = new IntCell(3); // new returns the memory address // of newly created object which is // assigned to m storedValue 3 m

C++ Details: Pointers (cont’d) m->write(5); cout read() << endl; delete m; // Deallocates the memory assigned to IntCell object // Be sure to have a corresponding delete for every new to avoid memory leak storedValue 5 m No automatic garbage collection in C++

C++ Details: Pointers (cont’d) IntCell *m; m = new IntCell(3); IntCell n(0); n.write(3); cout << n.read() << endl; What is the difference between m and n ?

C++ Details: Pointers (cont’d) IntCell *m; m = new IntCell(3); IntCell n(0); n.write(3); cout << n.read() << endl; storedValue 3 m 3 n

C++ Details: Assignment and Comparison of Pointers IntCell *a; IntCell *b; a = new IntCell(10); b = new IntCell(20); if (a == b) // Checks if a and b point at the same // object cout << “same” << endl; // Not executed a = b; // Makes a point at the same object that b // points to. if (a == b) cout << “same” << endl; // Executed storedValue 10 a storedValue 20 b

Summary Classes, Objects and Constructors Pointers

Questions ?