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.

Slides:



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

Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
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.
A RRAYS, P OINTERS AND R EFERENCES 1. A RRAYS OF O BJECTS Arrays of objects of class can be declared just like other variables. class A{ … }; A ob[4];
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Constructors & Destructors Review CS 308 – Data Structures.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
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.
1 C++ Structures Starting to think about objects...
C++ Classes & Object Oriented Programming. Object Oriented Programming  Programmer thinks about and defines the attributes and behavior of objects. 
Review of C++ Programming Part II Sheng-Fang Huang.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
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 Chapter 13: Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
C++ Review (3) Structs, Classes, Data Abstraction.
C++ Lecture 4 Tuesday, 15 July Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
Chapter 9 Classes: A Deeper Look, Part I Part II.
Review of Last Lecture. What we have learned last lecture? What does a constructor do? What is the way to define a constructor? Can it be defined under.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.
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.
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 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.
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.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
CS1201: Programming Language 2 Classes and objects.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
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.
Classes II Lecture 7 Course Name: High Level Programming Language Year : 2010.
1 Introduction to Object Oriented Programming Chapter 10.
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.
Constructors And Destructors. 2 Constructor Special Member Function used for Initialization -- Same Name as the Class Name Special Member Function used.
Object Oriented Programming  Programmer thinks about and defines the attributes and behavior of objects.  Often the objects are modeled after real- world.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Object Lifetimes and Dynamic Objects Lecture-7. Object Lifetimes and Dynamic Objects External (Global) Objects Persistent (in existence) throughout the.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Pointers and Dynamic Arrays
Learning Objectives Pointers as dada members
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
Review: Two Programming Paradigms
Introduction to Classes
Object-Oriented Programming
group work #hifiTeam
C++ Classes & Object Oriented Programming
Introduction to Classes
Object Oriented Programming (OOP) Lecture No. 9
Starting to think about objects...
9-10 Classes: A Deeper Look.
Introduction to Classes and Objects
Recitation Course 0520 Speaker: Liu Yu-Jiun.
Review: C++ class represents an ADT
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
More C++ Classes Systems Programming.
9-10 Classes: A Deeper Look.
Presentation transcript:

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 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 automatically 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 in heap ??? 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 } 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

Destructors –Special member function –Same name as class Preceded with tilde ( ~ ) –No arguments –No return value –Cannot be overloaded –Before system reclaims object’s memory Reuse memory for new objects Mainly used to de-allocate dynamic memory locations

18 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

Another class Example This class shows how to handle time parts. class Time { private: int *hour,*minute,*second; public: Time(); Time(int h,int m,int s); void printTime(); void setTime(int h,int m,int s); int getHour(){return *hour;} int getMinute(){return *minute;} int getSecond(){return *second;} void setHour(int h){*hour = h;} void setMinute(int m){*minute = m;} void setSecond(int s){*second = s;} ~Time(); }; Destructor

Time::Time() { hour = new int; minute = new int; second = new int; *hour = *minute = *second = 0; } Time::Time(int h,int m,int s) { hour = new int; minute = new int; second = new int; *hour = h; *minute = m; *second = s; } void Time::setTime(int h,int m,int s) { *hour = h; *minute = m; *second = s; } Dynamic locations should be allocated to pointers first

void Time::printTime() { cout<<"The time is : ("<<*hour<<":"<<*minute<<":"<<*second<<")" <<endl; } Time::~Time() { delete hour; delete minute;delete second; } void main() { Time *t; t= new Time(3,55,54); t->printTime(); t->setHour(7); t->setMinute(17); t->setSecond(43); t->printTime(); delete t; } Output: The time is : (3:55:54) The time is : (7:17:43) Press any key to continue Destructor: used here to de- allocate memory locations When executed, the destructor is called

Reasons for OOP 1.Simplify programming 2.Interfaces Information hiding: –Implementation details hidden within classes themselves 3.Software reuse Class objects included as members of other classes