C++ Lecture 4 Tuesday, 15 July 2003. Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l.

Slides:



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

C++ Classes & Data Abstraction
OOP Using Classes - I. Structures vs. Classes C-style structures –No “interface” If implementation changes, all programs using that struct must change.
1 CS 201 Introduction to c++ (1) Debzani Deb. 2 History of C++ Extension of C (C++ for better C) Early 1980s: Bjarne Stroustrup (Bell Laboratories) Provides.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 16: Classes and Data Abstraction Outline 16.1Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 16: Classes and Data Abstraction Outline 16.1Introduction 16.2Implementing a Time Abstract Data.
Introduction to Classes and Data Abstraction
More C++ Classes Systems Programming. Systems Programming: C++ Classes 2 Systems Programming: 2 C++ Classes  Preprocessor Wrapper  Time Class Case Study.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
1 Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
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. 
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
 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.
PART I CHAPTER 16 CS116 SENEM KUMOVA METİN 1. Structures Structures : Aggregate data types built using elements of other types struct Time { int hour;
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.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
1 Classes and Data Abstraction Part I Introduction Object-oriented programming (OOP)  Encapsulates data (attributes) and functions (behavior)
1 Classes and Data Abstraction 2 Objectives Understand encapsulation and data hiding Understand data abstraction and Abstract Data Types (ADTs) Create.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 6: Classes and Data Abstraction Outline 6.1Introduction 6.2Structure Definitions 6.3Accessing.
CLASSES : A DEEPER LOOK Chapter 9 Part I 1. 2 OBJECTIVES In this chapter you will learn: How to use a preprocessor wrapper to prevent multiple definition.
Chapter 9 Classes: A Deeper Look, Part I Part II.
Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
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.
EEL 3801 Part VI Fundamentals of C and C++ Programming Classes and Objects.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
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.
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.
1 Chapter 9 Introduction to Classes and Objects Program Development and Design Using C++, Third Edition.
Chapter 9 Classes: A Deeper Look, Part 1 Seventh Edition C++ How to Program © by Pearson Education, Inc. All Rights Reserved.
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. Këpuska Summer 2004 from Dr. S. Kozaitis Spring 2003 slides 1 Summary of Chapter 6: Classes.
1 Example Original Array Array After 2 nd Pass Array After 1 st Pass Array After 3 rd Pass.
Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.
Yan Shi CS/SE 2630 Lecture Notes
Chapter 16: Classes and Data Abstraction
Procedural and Object-Oriented Programming
Classes and Data Abstraction
Chapter 16: Classes and Data Abstraction
Introduction to Classes
C++ Classes & Object Oriented Programming
Introduction to Classes
Classes: A Deeper Look Outline
Chapter 9 Classes: A Deeper Look, Part 1
Starting to think about objects...
Recitation Course 0520 Speaker: Liu Yu-Jiun.
NAME 436.
Chapter 6: Classes and Data Abstraction
More C++ Classes Systems Programming.
Presentation transcript:

C++ Lecture 4 Tuesday, 15 July 2003

Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l Examples

OOP Key Concepts l OOP encapsulates data (attributes) and function (behavior) into packages called classes. l Class is like a blueprint of house. One can build many houses (of the same type) with one blueprint. l Out of a class, programmer can create object.

Structure Definition struct Time { int hour; int minute; int second; };

Declaration Time timeObject, // a variable timeArray[10], // array *timePtr, // pointer to... &timeRef = timeObject; // ref

Accessing Members of Structure cout << timeObject.hour; cout << timeRef.hour; timePtr = &timeObject; cout hour; cout << (*timePtr).hour; // the same

Example Fig.6.1 l Structure - declaration and use. l Conditional expression expr ? A : B l the value is A if expr is true and B if expr is false. C.f. Fig.6.1.

Abstract Data Type with a Class class Time { public: Time(); // constructor void setTime(int, int, int); void printMilitary(); void printStandard(); private: int hour; int minute; int second; };

Declaration of Objects of Type "Time" Time sunset, // object of type Time arrayOfTimes[5], // array of Time obj *ptrToTime, // pointer to Time obj &dinnerTime = sunset; // reference

What is an Object? l Object is a variable, l Object is a function? l Object is data and functions l Object is an abstraction of something that has attributes (property) and operations (function calls).

Member Functions l Member functions of Time are defined outside the class with :: binary scope resolution operator. l E.g., void Time::setTime(int h, int m, int s) { hour = ( h>= 0 && h < 24) ? H : 0;... }

Class Example Fig.6.3 l Class definition l Member function definition l Main program C.f. Fig. 6.3.

Class Scope l Class data members and member functions belong to that class's scope. l Within a class's scope, class members are references by name. l Outside a class's scope, class members are referenced through one of the handles on an object.

The Handles l Use dot (.) notation for object and references. l Use arrow (->) for pointer to the object l E.g., c.x, cpt -> x C.f. Fig. 6.4

Separating interface from implementations l Header files contains class declarations only l Class function definition in source file l Driver program in another file

Public and Private Data or Function l Public data or functions are accessible from outside l Private data or functions are not directly accessible by the user outside the class scope l Public functions are used as an interface to access or modify private data

Initializing Class Objects with Constructors l A constructor is a class member function with the same name as the class. l The constructor is called whenever an object of that class is created. l The constructor does not return a value (not even void).

Destructors l The name of the destructor for a class is the tilde (~) character followed by the class name. l Destructor is called when an object is going to disappear (out of scope).

When Constructors and Destructors are Called l Example of Fig , constructor and destructor of a class. C.f. Fig

Default Constructor class Time { public: Time(); // default constructor Time(int, int, int); …. } Used as Time t, t(12, 06, 0); C.f. Fig.18-20

Assignment by Default Memberwise Copy l If date1 and date2 are objects of the same type, we can say date2 = date1; l The date members values are copied memberwise. C.f. Fig.6.24

Questions l In the class assignment example, if the member data are arrays, are the values of the array elements copied? l If it is a pointer, is the pointer value (address) copied? l If a pointer pointing to an array, are the values of array elements copied?

Copy Constructor l You can override what the class assignment a=b means by write a so-called copy constructor l We declare and implement a function, e.g., for the class Date Date(Date &d) C.f. an example in Fig

Problems l Find error(s) in each of the following and explain how to correct it. Assuming the following prototype is declared in class Time. void ~Time(int);

Problems l Find error(s) - The following is a partial definition of class Time: class Time { public: // function prototypes private: int hour = 0; int minute = 0; int second = 0; };

Problems l Find error(s) – Assuming the following prototype is declared in class Employee: int Employee(const char *, const char *)