Lecture 18 Tao Wang 1

Similar documents
Chapter 10 Introduction to Classes

Object Oriented Design

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 12: Classes and Data Abstraction

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

And Even More and More C++ Fundamentals of Computer Science

Classes: A Deeper Look

Cpt S 122 Data Structures. Introduction to C++ Part II

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Object Oriented Programming(OOP).

Introduction to Programming Using Java (98-388)

Introduction Of Classes ( OOPS )

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

Lecture 8 Tao Wang 1

Lecture 5 Tao Wang 1

CS304 Object Oriented Programming Final Term

A A B U n i v e r s i t y

Introduction to Programming session 24

Introduction to Classes

CS313D: ADVANCED PROGRAMMING LANGUAGE

An Introduction to C++

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?


C++ (Non for C Programmer) (BT307) 40 Hours

Iterator: A way to sequentially access ( traverse ) each object in a container or collection. - Access is done as needed, not necessarily at once.

Lecture 5. Function Pointers

Object Oriented Design

Constructors and Destructors. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Classes and Objects. Class scope: - private members are only accessible by the class methods.

Fundamentals of Programming Session 24

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Abstraction in Software Development

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS201 Some Important Definitions

Lecture 2 Tao Wang 1

C++ 8. Constructors and Destructors

CSE 12, Week Six, Lecture Two Discussion: Getting started on hw7 & hw8

ADTs & Classes. An introduction

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

XII- COMPUTER SCIENCE VOL-II MODEL TEST I

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

CMSC202 Computer Science II for Majors

C++ C and C++ C++ fundamental types. C++ enumeration. To quote Bjarne Stroustrup: 5. Overloading Namespaces Classes

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE

CS 162, Lecture 25: Exam II Review. 30 May 2018

Fundamental Concepts and Definitions

KLiC C++ Programming. (KLiC Certificate in C++ Programming)

Data Structures using OOP C++ Lecture 3

Lecture 3 Tao Wang 1

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

Absolute C++ Walter Savitch

Chapter 11. Abstract Data Types and Encapsulation Concepts

IS 0020 Program Design and Software Tools

AN OVERVIEW OF C++ 1

Chapter 12. OOP: Creating Object-Oriented Programs The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

Recharge (int, int, int); //constructor declared void disply();

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

Programming in C and C++

OBJECTS. An object is an entity around us, perceivable through our senses. Types of Object: Objects that operate independently.

Classes and Data Abstraction

C++ Important Questions with Answers

MaanavaN.Com CS1203 OBJECT ORIENTED PROGRAMMING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Lecture 7. Log into Linux New documents posted to course webpage

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Chapter 6 Introduction to Defining Classes

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

IS0020 Program Design and Software Tools Midterm, Fall, 2004

EL2310 Scientific Programming

EEE-425 Programming Languages (2013) 1

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

C++ : Object Oriented Features. What makes C++ Object Oriented

CS201 Latest Solved MCQs

More C++ Classes. Systems Programming

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II

OBJECT ORIENTED PROGRAMMING

Implementing Abstract Data Types (ADT) using Classes

Ch02. True/False Indicate whether the statement is true or false.

Syllabus of C++ Software for Hands-on Learning: This course offers the following modules: Module 1: Getting Started with C++ Programming

Fundamentals of Programming Session 25

Lecture 4 Tao Wang 1

Constructors for classes

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming

clarity. In the first form, the access specifier public : is needed to {

+2 Volume II OBJECT TECHNOLOGY OBJECTIVE QUESTIONS R.Sreenivasan SanThome HSS, Chennai-4. Chapter -1

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

Introduction To C#.NET

Abstract Data Types and Encapsulation Concepts

Ch. 12: Operator Overloading

ADTs in C++ In C++, member functions can be defined as part of a struct

Computer Programming C++ Classes and Objects 6 th Lecture

Transcription:

Lecture 18 Tao Wang 1

Abstract Data Types in C++ (Classes) A procedural program consists of one or more algorithms that have been written in computerreadable language Input and display of program output take a back seat to processing Clear emphasis on formulas and calculations An object oriented approach fits graphically windowed environments Abstract data types: Central to creation of objects; a user defined data type rather than a built-in data type Scientists, Third Edition 2

Abstract Data Types Data type: Combination of data and associated operations A data type defines both the types of data and the types of operations that can be performed on the data Data type = Allowable Data Values + Operational Capabilities Operations in C++ are an inherent part of each data type Scientists, Third Edition 3

Abstract Data Types (continued) Abstract data type (ADT): User defined type that specifies both a type of data and the operations that can be performed on it User defined types are required when you want to create objects that are more complex than simple integers and characters Data structure: How data is stored Class: C++ name for an abstract data type Scientists, Third Edition 4

A Quick Look of a Class Fundamentals of Information Systems, Fifth Edition 5

Class Construction A class is usually constructed in two parts Declaration section Declares both the data types and functions for the class Implementation section Defines the functions whose prototypes have been declared in the declaration section Scientists, Third Edition 6

Class Construction (continued) Class declaration section: Scientists, Third Edition 7

Class Construction (continued) Class members: Both the variables and the functions listed in the declaration section Data members or instance variables: Variables listed in the declaration section Member functions: Functions listed in the declaration section When a function is part of a class it is referred to as a method to denote class membership Scientists, Third Edition 8

Class Construction (continued) Initial upper case letter in class name Date not required but followed by convention Keywords public and private are access specifiers that define access rights private: Indicates that class member can only be accessed by class functions Restricting user from access to data storage implementation details is called data hiding After a class category like private is designated, it remains in force until a new category is specified Scientists, Third Edition 9

Class Construction (continued) public functions can be called from outside the class In general, all class functions should be public so that they provide capabilities to manipulate class variables from outside the class The function with same name as class is the class s constructor function Used to initialize class data members with values Scientists, Third Edition 10

Class Construction (continued) Implementation section is where member functions declared in the declaration section are written General form for functions written in the implementation section is the same as all C++ functions with the addition of the class name and the scope resolution operator :: Scientists, Third Edition 11

Class Construction (continued) Variables of a user-declared class must: Be defined before use in a program Are referred to as objects An object name s attribute is referenced with the period operator objectname.attributename objectname is the name of a specific object attributename is the name of a data member defined for the object s class Scientists, Third Edition 12

Class Construction (continued) The syntax for referring to an object s method is: objectname.methodname(parameters) objectname is the name of the specific object methodname is name of a function defined for the object s class Scientists, Third Edition 13

Terminology Class: Programmer defined data type from which objects can be created Objects: Created from classes Referred to as instances of a class Process of creating a new object is called instantiation of the object Each time a new object instantiated a new set of data members belonging to the object is created Values contained in these data members determine the object s state Scientists, Third Edition 14

Constructors A constructor function is any function with the same name as its class Multiple constructors can be defined for each class as long as they can be distinguished by number and types of their parameters A constructor s intended purpose is to initialize a new object s data members If no constructor function is written the compiler supplies a default constructor In addition to initialization, a constructor can perform other tasks when it is called Scientists, Third Edition 15

Constructors (continued) General format of a constructor includes: The same name as the class to which it belongs No return type (not even void) A constructor that does not require arguments is called the default constructor Scientists, Third Edition 16

Constructors (continued) Scientists, Third Edition 17

Calling Constructors Constructors are called when an object is created Declaration can be made in a variety of ways Date c(4,1,2006); Date c = Date(4,1,2006); Date c = 8; // similar to above with // defaults for day and // year An object should never be declared with empty parentheses Date a(); Not the same as the declaration Date a; Does not result in an object being created Scientists, Third Edition 18

Overloaded and Inline Constructors Primary difference between a constructor and other user-written functions is how the constructor is called Constructors are called automatically each time an object is created Most other functions must be called explicitly by name Inline functions are functions defined in the class declaration section Scientists, Third Edition 19

Destructors Destructor functions: Counterpart to the constructor functions Destructors: Are functions with the same name as constructors but are preceded with a tilde (~) For the Date class the destructor name is ~Date() Take no parameters and return no values There can only be one destructor per class Scientists, Third Edition 20

Destructors (continued) Destructors: Called automatically when an object goes out of existence Clean up any undesirable effects the object might leave, such as releasing memory stored in a pointer Scientists, Third Edition 21

Common Programming Errors Failing to terminate class declaration section with semicolon Including return type with constructor s prototype or failing to include return type with other functions prototypes Using same name for a data member as for a member function Defining more than one default constructor Forgetting to include class name and scope operator, ::, in the function header Scientists, Third Edition 22

Summary A class Is a programmer-defined data type Consists of a declaration and implementation section Class functions can be written inline or included in the class implementation section Fundamentals of Information Systems, Fifth Edition 23