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

Similar documents
CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 8: Abstract Data Types

Lecture 18 Tao Wang 1

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

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

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.

Chapter 10 Introduction to Classes

Problem Solving with C++

Object Oriented Design

Absolute C++ Walter Savitch

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

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

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

ADTs & Classes. An introduction

Object-Oriented Design (OOD) and C++

Classes: A Deeper Look

CS201 Latest Solved MCQs

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

Introduction to Classes


CSI33 Data Structures

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

Short Notes of CS201

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

CS201 - Introduction to Programming Glossary By

CGS 2405 Advanced Programming with C++ Course Justification

CS201 Some Important Definitions

Introduction to Programming session 24

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

Classes. Classes (continued) Syntax The general syntax for defining a class:

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

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

Implementing Abstract Data Types (ADT) using Classes

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

An Introduction to C++

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

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

IS 0020 Program Design and Software Tools

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

Fundamentals of Programming Session 24

QUIZ Friends class Y;

Interview Questions of C++

Ch. 10: Name Control

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

Intermediate Programming & Design (C++) Classes in C++

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Chapter 4 Defining Classes I

Object Oriented Design

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

XII- COMPUTER SCIENCE VOL-II MODEL TEST I

Lecture 5. Function Pointers

Fast Introduction to Object Oriented Programming and C++

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

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

Padasalai.Net s Model Question Paper

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

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES IV

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Abstraction in Software Development

Computer Programming C++ Classes and Objects 6 th Lecture

CS313D: ADVANCED PROGRAMMING LANGUAGE

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Object Oriented Programming. Solved MCQs - Part 2

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

Chapter 8. Chapter Objectives. Chapter Objectives. User-Defined Classes and ADTs

C++ 8. Constructors and Destructors

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

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

VALLIAMMAI ENGINEERING COLLEGE

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Scope. Scope is such an important thing that we ll review what we know about scope now:

C++: Const Function Overloading Constructors and Destructors Enumerations Assertions

Ch. 12: Operator Overloading

CS304 Object Oriented Programming Final Term

B.C.A 2017 OBJECT ORIENTED PROGRAMMING USING C++ BCA303T MODULE SPECIFICATION SHEET

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli

Object Oriented Programming with c++ Question Bank

Midterm Exam #2 Review. CS 2308 :: Spring 2016 Molly O'Neil

CS313D: ADVANCED PROGRAMMING LANGUAGE

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

Instantiation of Template class

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

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

The Class Construct Part 1

Preview 9/20/2017. Object Oriented Programing with C++ Object Oriented Programing with C++ Object Oriented Programing with C++

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Fundamental Concepts and Definitions

Chapter 6 Structures and Classes. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

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

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

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

Data Structures using OOP C++ Lecture 3

Computer Science 306 Study Guide

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

SRI SARASWATHI MATRIC HR SEC SCHOOL PANAPAKKAM +2 IMPORTANT 2 MARK AND 5 MARK QUESTIONS COMPUTER SCIENCE VOLUME I 2 MARKS

Transcription:

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

Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members of a class Explore how classes are implemented Examine constructors and destructors Learn about the abstract data type (ADT) 2

Objectives (cont'd.) Explore how classes are used to implement ADTs Learn about information hiding Explore how information hiding is implemented in C++ Learn about the static members of a class 3

Classes Class: collection of a fixed number of components (members) Definition syntax: Defines a data type, no memory is allocated Don t forget the semicolon after closing brace 4

Classes (cont'd.) Class member can be a variable or a function If a member of a class is a variable It is declared like any other variable In the definition of the class You cannot initialize a variable when you declare it If a member of a class is a function Function prototype is listed Function members can (directly) access any member of the class 5

Classes (cont'd.) Three categories of class members private (default) Member cannot be accessed outside the class public Member is accessible outside the class protected 6

Classes (cont'd.) These functions cannot modify the member variables of a variable of type clocktype const: formal parameter can t modify the value of the actual parameter private members, can t be accessed from outside the class 7

Unified Modeling Language Class Diagrams +: member is public -: member is private #: member is protected 8

Variable (Object) Declaration Once a class is defined, you can declare variables of that type clocktype myclock; clocktype yourclock; A class variable is called a class object or class instance 9

Accessing Class Members Once an object is declared, it can access the public members of the class Syntax: The dot (.) is the member access operator If object is declared in the definition of a member function of the class, it can access the public and private members 10

Accessing Class Members (cont d.) 11

Built-in Operations on Classes Most of C++ s built-in operations do not apply to classes Arithmetic operators cannot be used on class objects unless the operators are overloaded You cannot use relational operators to compare two class objects for equality Built-in operations valid for class objects: Member access (.) Assignment (=) 12

Assignment Operator and Classes 13

Class Scope An object can be automatic or static A member of the class is local to the class You access a class member outside the class by using the class object name and the member access operator (.) 14

Functions and Classes Objects can be passed as parameters to functions and returned as function values As parameters to functions Objects can be passed by value or by reference If an object is passed by value Contents of data members of the actual parameter are copied into the corresponding data members of the formal parameter 15

Reference Parameters and Class Objects (Variables) Passing by value might require a large amount of storage space and a considerable amount of computer time to copy the value of the actual parameter into the formal parameter If a variable is passed by reference The formal parameter receives only the address of the actual parameter 16

Reference Parameters and Class Objects (Variables) (cont'd.) Pass by reference is an efficient way to pass a variable as a parameter Problem: when passing by reference, the actual parameter changes when formal parameter changes Solution: use const in the formal parameter declaration 17

Implementation of Member Functions Scope resolution operator 18

Implementation of Member Functions (cont'd.) 19

Implementation of Member Functions (cont'd.) 20

Implementation of Member Functions (cont'd.) 21

Implementation of Member Functions (cont'd.) 22

Implementation of Member Functions (cont'd.) Once a class is properly defined and implemented, it can be used in a program A program that uses/manipulates the objects of a class is called a client of that class When you declare objects of the class clocktype, every object has its own copy of the member variables (hr, min, and sec) Variables such as hr, min, and sec are called instance variables of the class Every object has its own instance of the data 23

Order of public and private Members of a Class C++ has no fixed order in which you declare public and private members By default all members of a class are private Use the member access specifier public to make a member available for public access 24

Order of public and private Members of a Class (cont'd.) 25

Order of public and private Members of a Class (cont'd.) 26

Order of public and private Members of a Class (cont'd.) 27

Roles of member function 1. Inspector function (access information) : only accesses the value(s) of member variable(s) typically has name such as : displayvalues(). 2. Mutator function(implentors): change or modifies the value(s) of member variable(s) it is often has name as: SetData() or ComputeValues(). 3. Auxiliary function(facilitators):- perform some actions or services such as sorting data, search for data. typically has name such as : SortAscending(), FindMinValue(); 4. Constant function: Member function that cannot modify member variables Use const in function heading 5. Manager function :- create and destroy objects. They perform initialization function and cleanup.it is called constructors and deconstructors. 28

Constructors Use constructors to guarantee that data members of a class are initialized Two types of constructors: With parameters Without parameters (default constructor) The name of a constructor is the same as the name of the class A constructor has no type 29

Constructors (cont'd.) A class can have more than one constructor Each must have a different formal parameter list Constructors execute automatically when a class object enters its scope They cannot be called like other functions Which constructor executes depends on the types of values passed to the class object when the class object is declared 30

Constructors (cont'd.) 31

Constructors (cont'd.) Can be replaced with: settime(hours, minutes, seconds); 32

Invoking a Constructor A constructor is automatically executed when a class variable is declared 33

Invoking the Default Constructor To invoke the default constructor: Example: clocktype yourclock; 34

Syntax: Invoking a Constructor with Parameters The number of arguments and their type should match the formal parameters (in the order given) of one of the constructors Otherwise, C++ uses type conversion and looks for the best match Any ambiguity leads to a compile-time error 35

Constructors and Default Parameters If you replace the constructors of clocktype with the constructor in Line 1, you can declare clocktype objects with zero, one, two, or three arguments as follows: clocktype clock1; //Line 2 clocktype clock2(5); //Line 3 clocktype clock3(12, 30); //Line 4 clocktype clock4(7, 34, 18); //Line 5 36

Classes and Constructors: A Precaution If a class has no constructor(s), C++ provides the default constructor However, object declared is still uninitialized If a class includes constructor(s) with parameter(s), but not the default constructor C++ does not provide the default constructor 37

Arrays of Class Objects (Variables) and Constructors If a class has constructors and you declare an array of that class s objects, the class should have the default constructor 38

Arrays of Class Objects (Variables) and Constructors (cont'd.) 39

Destructors Destructors are functions without any type The name of a destructor is the character '~' followed by class name For example: ~clocktype(); A class can have only one destructor The destructor has no parameters The destructor is automatically executed when the class object goes out of scope 40

Data Abstract, Classes, and Abstract Abstraction Data Types Separating design details from usage Separating the logical properties from the implementation details Abstraction can also be applied to data Abstract data type (ADT): data type that separates the logical properties from the implementation details 41

Data Abstract, Classes, and Abstract Data Types (cont'd.) 42

Data Abstract, Classes, and Abstract Data Types (cont'd.) 43

Data Abstract, Classes, and Abstract Data Types (cont'd.) 44

A struct Versus a class By default, members of a struct are public private specifier can be used in a struct to make a member private By default, the members of a class are private classes and structs have the same capabilities 45

A struct Versus a class (cont'd.) In C++, the definition of a struct was expanded to include member functions, constructors, and destructors If all member variables of a class are public and there are no member functions Use a struct 46

Information Hiding Information hiding: hiding the details of the operations on the data Interface (header) file: contains the specification details Implementation file: contains the implementation details In header file, include function prototypes and comments that briefly describe the functions Specify preconditions and/or postconditions 47

Information Hiding (cont'd.) Header file has an extension.h Implementation file has an extension.cpp Implementation file must include header file via include statement In include statement: User-defined header files are enclosed in double quotes System-provided header files are enclosed between angular brackets 48

Information Hiding (cont'd.) Precondition: A statement specifying the condition(s) that must be true before the function is called Postcondition: A statement specifying what is true after the function call is completed 49

Executable Code To use an object in a program The program must be able to access the implementation Visual C++, Visual Studio.NET, C++ Builder, and CodeWarrior put the editor, compiler, and linker into a package With one command, the program is compiled and linked with the other necessary files These systems also manage multiple file programs in the form of a project 50

Executable Code (cont'd.) A project consists of several files, called the project files These systems usually have a command, called build, rebuild, or make When applied to a project, system compiles and links all files required to create the executable code When file(s) in the project change, use these commands to recompile and relink the files 51

Executable Code (cont'd.) 52

Static Members of a Class Use the keyword static to declare a function or variable of a class as static A public static function or member of a class can be accessed using the class name and the scope resolution operator static member variables of a class exist even if no object of that class type exists 53

Pointer this Every object of a class maintains a (hidden) pointer to itself called this When an object invokes a member function this is referenced by the member function 54

Friend Functions of Classes Friend function (of a class): nonmember function of the class that has access to all the members of the class To make a function friend to a class Reserved word friend precedes the function prototype in the class definition 55

Definition of a friend Function "friend" doesn t appear in function definition When writing the friend function definition The name of the class and the scope resolution operator are not used 56

Summary Class: collection of a fixed number of components Members: components of a class Accessed by name Classified into one of three categories: private, protected, and public Class variables are called class objects or, simply, objects 57

Summary (cont'd.) Member access operator, ->, accesses the object component pointed to by a pointer The only built-in operations on classes are the assignment and member selection Constructors guarantee that data members are initialized when an object is declared Default constructor has no parameters 58

Summary (cont'd.) Destructors automatically execute when a class object goes out of scope A class can have only one destructor The destructor has no parameters Abstract data type (ADT): data type that separates the logical properties from the implementation details A public static member, function or data, of a class can be accessed using the class name and the scope resolution operator 59

Summary (cont'd.) Static data members of a class exist even when no object of the class type exists Instance variables: non-static data members Can pass an object of a derived class to a formal parameter of the base class type Binding of virtual functions occurs at execution time (dynamic or run-time binding) 60