1 Review: C++ class represents an ADT 2 kinds of class members: data members and function members Class members are private by default Data members are.

Slides:



Advertisements
Similar presentations
Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
Advertisements

1 A pointer variable is a variable whose value is the address of a location in memory int x; x = 5; int* ptr1; ptr1 = &x; int* ptr2; ptr2 = ptr1; *ptr1.
Destructors Math 130 Lecture # xx Mo/Da/Yr B Smith: New lecture for 05. Use this for evolving to Java B Smith: New lecture for 05. Use this for evolving.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Constructors & Destructors Review CS 308 – Data Structures.
1 Lecture 29 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
More C++ Classes Systems Programming. Systems Programming: C++ Classes 2 Systems Programming: 2 C++ Classes  Preprocessor Wrapper  Time Class Case Study.
1 Abstract Data Type (ADT) a data type whose properties (domain and operations) are specified (what) independently of any particular implementation (how)
1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
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.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedural and Object-Oriented Programming 13.1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Class Miscellanea Details About Classes. Review We’ve seen that a class has two sections: class Temperature { public: //... public members private: //...
Chapter 9 Classes: A Deeper Look, Part I Part II.
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.
1 Final Exam Tues 3/16/10 (2-3:50) (Same classroom) Old Textbook - Chapters 11-16, 18 Focus is on 15, 16 and 18 Final Exam Tues 3/16/10 (2-3:50) (Same.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own 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?
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Constructors & Destructors, Proxy Classes, Friend Function and example of static member.
1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Starting Out with C++, 3 rd Edition 1 Chapter 13 – Introduction to Classes Procedural and Object-Oriented Programming Procedural programming is a method.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Object Access m1.write(44); m2.write(m2.read() +1); std::cout
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
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Learning Objectives Pointers as dada members
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?
Review: Two Programming Paradigms
Introduction to 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?
Chapter Structured Types, Data Abstraction and Classes
LinkedList Class.
Introduction to Structured Data Types and Classes
Introduction to Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
Classes: A Deeper Look Outline
Chapter 15 Pointers, Dynamic Data, and Reference Types
Introduction to Classes and Objects
Review: C++ class represents an ADT
Lecture 7.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Destructors, Copy Constructors & Copy Assignment Operators
Lecture 8 Object Oriented Programming (OOP)
Operator overloading Friend Function This Operator Inline Function
Destructors, Copy Constructors & Copy Assignment Operators
More C++ Classes Systems Programming.
Presentation transcript:

1 Review: C++ class represents an ADT 2 kinds of class members: data members and function members Class members are private by default Data members are generally private Public function members are part of class interface Private class members are helpers, can be accessed only by the class member functions (and friend functions).

2 Objects  Objects: variables or instances of a class that is declared in a program  Declaration of an Object  Initiation of Objects  Constructors & Destructors  Working with Multiple Files

3 What is an object? OBJECT Operations Data set of methods (member functions) internal state (values of private data members)

4 class Rectangle { private: int width; int length; public: void set(int w, int l); int area(); } Declaration of an Object main() { Rectangle r1; r1.set(5, 8); } r1 is statically allocated width length r1 width = 5 length = 8

5 class Rectangle { private: int width; int length; public: void set(int w, int l); int area(); } Declaration of an Object main() { Rectangle r1; r1.set(5, 8); Rectangle *r2; r2 = &r1; r2->set(8,10); } r2 is a pointer to a Rectangle object width length r1 width = 5 length = ??? r width = 8 length = 10 //dot notation //arrow notation

6 class Rectangle { private: int width; int length; public: void set(int w, int l); int area(); } Declaration of an Object main() { Rectangle *r3; r3 = new Rectangle(); r3->set(80,100); delete r3; r3 = NULL; } r3 is dynamically allocated ??? r width length 5000 width = 80 length = 100 ??? NULL //arrow notation

7 #include class circle { public: double radius; }; Object Initialization int main() { circle c1; // Declare an instance of the class circle c1.radius = 5; // Initialize by assignment circle c2 = {-10}; // Initialize by aggregation, not valid for a circle } 1. By Assignment Only work for public data members No control over the operations on data members

8 #include class circle { private: double radius; public: void set (double r) {radius = r;} double get_r () {return radius;} }; int main(void) { circle c; // an object of circle class c.set(5.0); // initialize an object with a public member function cout << "The radius of circle c is " << c.get_r() << endl; // access a private data member with an accessor } Object Initialization 2. By Public Member Functions Accessor Implementor

9 class Rectangle { private: int width; int length; public: void set(int w, int l); int area(); } Declaration of an Object main() { Rectangle r1; r1.set(5, 8); Rectangle *r2; r2 = &r1; r2->set(8,10); } r2 is a pointer to a Rectangle object //dot notation //arrow notation r1 and r2 are both initialized by public member function set

10 class Rectangle { private: int width; int length; public: Rectangle(); Rectangle(const Rectangle &r); Rectangle(int w, int l); void set(int w, int l); int area(); } Object Initialization 3. By Constructor Default constructor Copy constructor Constructor with parameters There is no return type Are used to initialize class data members Have the same name as the class They are publicly accessible They have different signatures

11 class Rectangle { private: int width; int length; public: void set(int w, int l); int area(); } Object Initialization Default constructor When a class is declared with no constructors, the compiler automatically assumes default constructor and copy constructor for it. Rectangle :: Rectangle() { }; Copy constructor Rectangle :: Rectangle (const Rectangle & r) { width = r.width; length = r.length; };

12 class Rectangle { private: int width; int length; public: void set(int w, int l); int area(); } Object Initialization Initialize with default constructor Rectangle r1; Rectangle *r3 = new Rectangle(); Initialize with copy constructor Rectangle r4; r4.set(60,80); Rectangle r5 = Rectangle(r4); Rectangle *r6 = new Rectangle(r4);

13 class Rectangle { private: int width; int length; public: Rectangle(int w, int l) {width =w; length=l;} void set(int w, int l); int area(); } Object Initialization If any constructor with any number of parameters is declared, no default constructor will exist, unless you define it. Rectangle r4;// error Initialize with constructor Rectangle r5(60,80); Rectangle *r6 = new Rectangle(60,80);

14 class Rectangle { private: int width; int length; public: Rectangle(); Rectangle(int w, int l); void set(int w, int l); int area(); } Object Initialization Write your own constructors Rectangle :: Rectangle() { width = 20; length = 50; }; Rectangle *r7 = new Rectangle(); width length width = 20 length = ??? r

15 class Account { private: char *name; double balance; unsigned int id; //unique public: Account(); Account(const Account &a); Account(const char *person); } Object Initialization With constructors, we have more control over the data members Account :: Account() { name = NULL; balance = 0.0; id = get_unique_id(); }; Account :: Account(const Account &a) { name = new char[strlen(a.name)+1]; strcpy (name, a.name); balance = a.balance; id = get_unique_id(); }; Account :: Account(const char *person) { name = new char[strlen(person)+1]; strcpy (name, person); balance = 0.0; id = get_unique_id(); };

16 So far, … An object can be initialized by a class constructor –default constructor –copy constructor –constructor with parameters Resources are allocated when an object is initialized Resources should be revoked when an object is about to end its lifetime

17 Cleanup of An Object class Account { private: char *name; double balance; unsigned int id; //unique public: Account(); Account(const Account &a); Account(const char *person); ~Account(); } Destructor Account :: ~Account() { delete[] name; release_id (id); } Its name is the class name preceded by a ~ (tilde) It has no argument It is used to release dynamically allocated memory and to perform other "cleanup" activities It is executed automatically when the object goes out of scope

18 Putting Them Together class Str { char *pData; int nLength; public: //constructors Str(); Str(char *s); Str(const Str &str); //accessors char* get_Data(); int get_Len(); //destructor ~Str(); }; Str :: Str() { pData = new char[1]; *pData = ‘\0’; nLength = 0; }; Str :: Str(const Str &str) { int n = str.nLength; pData = new char[n+1]; nLength = n; strcpy(pData,str.pData); }; Str :: Str(char *s) { pData = new char[strlen(s)+1]; strcpy(pData, s); nLength = strlen(s); };

19 Putting Them Together class Str { char *pData; int nLength; public: //constructors Str(); Str(char *s); Str(const Str &str); //accessors char* get_Data(); int get_Len(); //destructor ~Str(); }; char* Str :: get_Data() { return pData; }; Str :: ~Str() { delete[] pData; }; int Str :: get_Len() { return nLength; };

20 Putting Them Together class Str { char *pData; int nLength; public: //constructors Str(); Str(char *s); Str(const Str &str); //accessors char* get_Data(); int get_Len(); //destructor ~Str(); }; int main() { int x=3; Str *pStr1 = new Str(“Joe”); //initialize from char* Str *pStr2 = new Str(); //initialize with constructor }

21 Memory Layout intx3 type var value Str *pStr1Address Str *pStr2Address char *pDataaddress type var value intnLengthstrlen("joe")+1 Stack Memory Heap Memory Str object char *pDataaddress type var value intnLength0 char '\0' type value char'J' char'o' char'e' char'\0'

22 Interacting Objects

23 Take Home Message There are different types of member functions in the definition of a class –Accessor int CStr :: get_length(); –implementor/worker void Rectangle :: set(int, int); –helper void Date :: errmsg(const char* msg); –constructor Account :: Account(); Account :: Account(const Account& a); Account :: Account(const char *person); –destructor Account :: ~Account();

24 Working with Multiple Files To improve the readability, maintainability and reusability, codes are organized into modules. When working with complicated codes, –A set of.cpp and.h files for each class groups.h file contains the prototype of the class.cpp contains the definition/implementation of the class –A.cpp file containing main() function, should include all the corresponding.h files where the functions used in.cpp file are defined

25 Example : time.h // SPECIFICATION FILE ( time.h ) // Specifies the data members and // members function prototypes. #ifndef TIME_H #define TIME_H class Time { public:... private:... } ; #endif

26 // IMPLEMENTATION FILE ( time.cpp ) // Implements the member functions of class Time #include “ time.h”// also must appear in client code #include … … bool Time :: Equal ( Time otherTime ) const // Function value == true, if this time equals otherTime // == false, otherwise { return ( (hrs == otherTime.hrs) && (mins == otherTime.mins) && (secs == otherTime.secs) ) ; }... Example : time.cpp

27 Example : main.cpp // Client Code ( main.cpp ) #include “ time.h” // other functions, if any int main() { … } Compile and Run g++ -o main main.cpp time.cpp

28 Separate Compilation and Linking of Files time.h main.cpp time.cpp main.obj main.exe time.obj Compiler Linker #include “time.h” implementation file specification file main program