Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I."— Presentation transcript:

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

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

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

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

5 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

6 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

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

8 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

9 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

10 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

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

12 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

13 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

14 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

15 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

16 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)

17 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;

18 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

19 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

20 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

21 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; }

22 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

23 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

24 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

25 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

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

27 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; }

28 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.

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

30 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.

31 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. 10004 5 Memory 10000 10001 10002 10003 10004 10005 10006 p 5 p

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

33 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

34 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++

35 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 ?

36 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

37 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

38 Summary Classes, Objects and Constructors Pointers

39 Questions ?


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

Similar presentations


Ads by Google