Object Oriented Design

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

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

Computer Programming C++ Classes and Objects 6 th Lecture

How to engineer a class to separate its interface from its implementation and encourage reuse.

by Pearson Education, Inc. All Rights Reserved. 2

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

CE221 Programming in C++ Part 1 Introduction

Fundamentals of Programming Session 25

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.

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

Lecture 7: Classes and Objects CS2301

Fundamentals of Programming Session 23

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

AN OVERVIEW OF C++ 1

PIC 10A Objects/Classes

Introduction to Programming session 24

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Functions and Recursion

Operator overloading

Classes: A Deeper Look

Chapter 10 Introduction to Classes

Functions and an Introduction to Recursion Pearson Education, Inc. All rights reserved.

Lecture 18 Tao Wang 1

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

Introduction Of Classes ( OOPS )

Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay

III. Classes (Chap. 3)

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Object Oriented Design

Fast Introduction to Object Oriented Programming and C++

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Object Oriented Design

CS304 Object Oriented Programming Final Term

Functions and Recursion

Interview Questions of C++

Introduction to C++ Programming. Adhi Harmoko S, M.Komp

CS 376b Computer Vision

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. 1

2 nd Week Lecture Notes

Object-Oriented Programming

IS 0020 Program Design and Software Tools

Question 1 Consider the following structure used to keep employee records:

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

Functions, Arrays & Structs

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Chapter 1 Introduction to Computers and C++ Programming

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

Introduction to C++ Systems Programming

CS201 Latest Solved MCQs

Fundamentals of Programming Session 24

a. a * c - 10 = b. a % b + (a * d) + 7 =

Lab 1: First Steps in C++ - Eclipse

Implementing an ADT with a Class

Data Structures using OOP C++ Lecture 3

OBJECT ORIENTED PROGRAMMING USING C++

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II

CSE 303: Concepts and Tools for Software Development

Introduction to Programming

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

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

G52CPP C++ Programming Lecture 13

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

COMP322 - Introduction to C++

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

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

Introduction to Classes and Objects

CSC1322 Object-Oriented Programming Concepts

IS 0020 Program Design and Software Tools

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)

M.CS201 Programming language


ADTs & Classes. An introduction

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

Unit 1 : Principles of object oriented programming

Fundamentals of Programming Session 24

Programming II Lecture 2 Structures and Classes

IS0020 Program Design and Software Tools Midterm, Fall, 2004

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

Introduction to Computer Science I

Object-Oriented Design (OOD) and C++

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Control Statements: Part Pearson Education, Inc. All rights reserved.

Classes and Data Abstraction

Computer Programming with C++ (21)

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

Abstract Data Types. Different Views of Data:

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

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

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

Programming C++ Lecture 3. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor

CSCE 110 PROGRAMMING FUNDAMENTALS

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Classes and Data Abstraction. Topic 5

Transcription:

Object Oriented Design Lecture 3: Introduction to C++ (Continue) Examples using declarations that eliminate the need to repeat the std:: prefix 1

Examples using namespace std; enables a program to use all the names in any standard C++ header file C Struct // defines a type, referred to as struct account struct Account { }; int account_number; char *first_name; char *last_name; float balance; A struct declaration consists of a list of fields //To create a new variable of this type account struct Account s; 2

C++ Classes C++ classes are extensions of the C struct, to contain functions or overloaded operators Member functions Overloaded operators C++ classes add more control of the accessibility of it field members public, private, friend, inheritance C++ classes is self contained Construction and destruction A Class // defines a class class Account { int account_number; char *first_name; char *last_name; float balance; }; Member variables -- private on default //To create a new variable of class account Account s; 3

A Class // defines a class class Account { public: int account_number; char *first_name; char *last_name; float balance; }; Member variables -- defined as public //To create a new variable of class account Account s; A Class // defines a class class Account { int account_number; char *first_name; public: char *last_name; float balance; }; Member variables -- private on default Member variables -- defined as public //To create a new variable of class account Account s; 4

#include <iostream> #include <string> using namespace std; class Person //declare a class person { public: string name; int age; }; int main () { Person a, b; } a.name = "Calvin"; b.name = "Hobbes"; a.age = 30; b.age = 20; cout << a.name << ": " << a.age << endl; cout << b.name << ": " << b.age << endl; return 0; //define new variables with person class //access member variables in variable a Access-specifier The access-specifier label public: contains the keyword public is an access specifier. Indicates that the function is available to the public that is, it can be called by other functions in the program (such as main), and by member functions of other classes (if there are any). Access specifiers are always followed by a colon (:). 5

Private and Public Most data-member declarations appear after the accessspecifier label private: Like public, keyword private is an access specifier. Variables or functions declared after access specifier private (and before the next access specifier) are accessible only to member functions of the class for which they re declared. The default access for class members is private so all members after the class header and before the first access specifier are private. The access specifiers public and private may be repeated, but this is unnecessary and can be confusing. Private and Public Declaring data members with access specifier private is known as data hiding. When a program creates (instantiates) an object, its data members are encapsulated (hidden) in the object and can be accessed only by member functions of the object s class. 6

Use Member Function Typically, you cannot call a member function of a class until you create an object of that class. First, create an object of class GradeBook called mygradebook. The variable s type is GradeBook. The compiler does not automatically know what type GradeBook is it s a user-defined type. Tell the compiler what GradeBook is by including the class definition. Each class you create becomes a new type that can be used to create objects. Call the member function displaymessage- by using variable mygradebook followed by the dot operator (.), the function name display-message and an empty set of parentheses. Causes the displaymessage function to perform its task. 7

// defines a class class Account { int account_number; char *first_name; char *last_name; float balance; public: void setaccountnumber(int a) { account_number=a; } }; // defines a class class Account { int account_number; char *first_name; char *last_name; float balance; public: void setaccountnumber(int); }; void Account::setAccountNumber(int a) { account_number=7; } 8

Constructor Each class can provide a constructor that can be used to initialize an object of the class when the object is created. A constructor is a special member function that must be defined with the same name as the class, so that the compiler can distinguish it from the class s other member functions. An important difference between constructors and other functions is that constructors cannot return values, so they cannot specify a return type (not even void). Normally, constructors are declared public. Constructor -- continue C++ requires a constructor call for each object that is created, which helps ensure that each object is initialized before it s used in a program. The constructor call occurs implicitly when the object is created. If a class does not explicitly include a constructor, the compiler provides a default constructor that is, a constructor with no parameters. 9

A constructor specifies in its parameter list the data it requires to perform its task. 10

place the data here to pass it to The constructor Line 46 creates and initializes a GradeBook object called gradebook1. When this line executes, the GradeBook constructor (lines 14 17) is called (implicitly by C++) with the argument "CS101 Introduction to C++ Programming" to initialize gradebook1 s course name. Line 47 repeats this process for the GradeBook object called gradebook2, this time passing the argument "CS102 Data Structures in C++" to initialize gradebook2 s course name. 11

Any constructor that takes no arguments is called a default constructor. A class gets a default constructor in one of two ways: The compiler implicitly creates a default constructor in a class that does not define a constructor. Such a constructor does not initialize the class s data members, but does call the default constructor for each data member that is an object of another class. An uninitialized variable typically contains a garbage value. You explicitly define a constructor that takes no arguments. Such a default constructor will call the default constructor for each data member that is an object of another class and will perform additional initialization specified by you. If you define a constructor with arguments, C++ will not implicitly create a default constructor for that class. Homework 1 and Midterm Exam 1 Homework 1 is due on Wednesday Jan 21 at 23:59pm Midterm Exam 1 is on Jan 26 in class 12

Separated Header file One of the benefits of creating class definitions is that, when packaged properly, our classes can be reused by programmers potentially worldwide. Programmers who wish to use our GradeBook class cannot simply include the file from Fig. 3.7 in another program. Function main begins the execution of every program, and every program must have exactly one main function. Header File vs Source-Code File Each of the previous examples in the chapter consists of a single.cpp file, also known as a source-code file, that contains a GradeBook class definition and a main function. When building an object-oriented C++ program, it s customary to define reusable source code (such as a class) in a file that by convention has a.h filename extension known as a header file. Programs use #include preprocessor directives to include header files and take advantage of reusable software components. 13

Separate Code Our next example separates the code from Fig. 3.7 into two files GradeBook.h (Fig. 3.9) and fig03_10.cpp (Fig. 3.10). As you look at the header file in Fig. 3.9, notice that it contains only the GradeBook class definition (lines 8 38), the appropriate header files and a using declaration. The main function that uses class GradeBook is defined in the source-code file fig03_10.cpp (Fig. 3.10) in lines 8 18. For the larger programs in industry, we often use a separate source-code file containing function main to test our classes (this is called a driver program). 14

15

Header File A header file such as GradeBook.h (Fig. 3.9) cannot be used to begin program execution, because it does not contain a main function. To test class GradeBook (defined in Fig. 3.9), you must write a separate source-code file containing a main function (such as Fig. 3.10) that instantiates and uses objects of the class. To help the compiler understand how to use a class, we must explicitly provide the compiler with the class s definition That s why, for example, to use type string, a program must include the <string> header files. This enables the compiler to determine the amount of memory that it must reserve for each object of the class and ensure that a program calls the class s member functions correctly. Object -- Memory The compiler creates only one copy of the class s member functions and shares that copy among all the class s objects. Each object, of course, needs its own copy of the class s data members, because their contents can vary among objects. The member-function code, however, is not modifiable, so it can be shared among all objects of the class. Therefore, the size of an object depends on the amount of memory required to store the class s data members. By including GradeBook.h in line 4, we give the compiler access to the information it needs to determine the size of a GradeBook object and to determine whether objects of the class are used correctly. 16

Include A #include directive instructs the C++ preprocessor to replace the directive with a copy of the contents of GradeBook.h before the program is compiled. When the source-code file fig03_10.cpp is compiled, it now contains the GradeBook class definition (because of the #include), and the compiler is able to determine how to create GradeBook objects and see that their member functions are called correctly. Now that the class definition is in a header file (without a main function), we can include that header in any program that needs to reuse our GradeBook class. Include vs. < > Notice that the name of the GradeBook.h header file in line 4 of Fig. 3.10 is enclosed in quotes (" ") rather than angle brackets (< >). Normally, a program s source-code files and user-defined header files are placed in the same directory. When the preprocessor encounters a header file name in quotes, it attempts to locate the header file in the same directory as the file in which the #include directive appears. If the preprocessor cannot find the header file in that directory, it searches for it in the same location(s) as the C++ Standard Library header files. When the preprocessor encounters a header file name in angle brackets (e.g., <iostream>), it assumes that the header is part of the C++ Standard Library and does not look in the directory of the program that is being preprocessed. 17

Hide Implementation Placing a class definition in a header file reveals the entire implementation of the class to the class s clients. Conventional software engineering wisdom says that to use an object of a class, the client code needs to know only what member functions to call, what arguments to provide to each member function and what return type to expect from each member function. The client code does not need to know how those functions are implemented. If client code does know how a class is implemented, the clientcode programmer might write client code based on the class s implementation details. Ideally, if that implementation changes, the class s clients should not have to change. Hiding the class s implementation details makes it easier to change the class s implementation while minimizing, and hopefully eliminating, changes to client code. Interfaces Interfaces define and standardize the ways in which things such as people and systems interact with one another. The interface of a class describes what services a class s clients can use and how to request those services, but not how the class carries out the services. A class s public interface consists of the class s public member functions (also known as the class s public services). 18

Separating Interface from Implementation In our prior examples, each class definition contained the complete definitions of the class s public member functions and the declarations of its private data members. It s better to define member functions outside the class definition, so that their implementation details can be hidden from the client code. Ensures that you do not write client code that depends on the class s implementation details. How to Separate Member-function definitions are placed in a source-code file of the same base name (e.g., GradeBook) as the class s header file but with a.cpp filename extension. 19

Interface The function definitions in Fig. 3.9 are replaced here with function prototypes (lines 12 15) that describe the class s public interface without revealing the class s memberfunction implementations. A function prototype is a declaration of a function that tells the compiler the function s name, its return type and the types of its parameters. Including the header file GradeBook.h in the client code (line 5 of Fig. 3.13) provides the compiler with the information it needs to ensure that the client code calls the member functions of class GradeBook correctly. 20

Implementation Source-code file GradeBook.cpp (Fig. 3.12) defines class GradeBook s member functions, which were declared in lines 12 15 of Fig. 3.11. Each member-function name in the function headers (lines 9, 15, 21 and 27) is preceded by the class name and ::, which is known as the binary scope resolution operator. This ties each member function to the (now separate) GradeBook class definition (Fig. 3.11), which declares the class s member functions and data members. Separating Interface from Implementation 21

Separating Interface from Implementation To indicate that the member functions in GradeBook.cpp are part of class GradeBook, we must first include the GradeBook.h header file (line 5 of Fig. 3.12). This allows us to access the class name GradeBook in the GradeBook.cpp file. When compiling GradeBook.cpp, the compiler uses the information in GradeBook.h to ensure that the first line of each member function matches its prototype in the GradeBook.h file, and that each member function knows about the class s data members and other member functions 22

Separating Interface from Implementation Before executing this program, the sourcecode files in Fig. 3.12 and Fig. 3.13 must both be compiled, then linked together that is, the member-function calls in the client code need to be tied to the implementations of the class s member functions a job performed by the linker. The diagram in Fig. 3.14 shows the compilation and linking process that results in an executable GradeBook application that can be used by instructors. 23

End 24