 Chapter 2 introduces Object Oriented Programming.  OOP is a relatively new approach to programming which supports the creation of new data types and.

Slides:



Advertisements
Similar presentations
Container Classes A container class is a data type that is capable of holding a collection of items. In C++, container classes can be implemented as.
Advertisements

Copyright  Hannu Laine C++-programming Part 5 Strings.
 Chapter 2 introduces Object Oriented Programming.  OOP is a relatively new approach to programming which supports the creation of new data types and.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Classes and Objects Systems Programming.
Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Lecture 5-6 OOP Overview. Outline A Review of C++ Classes (Lecture 5) OOP, ADTs and Classes Class Definition, Implementation and Use Constructors and.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
תכנות מונחה עצמים Object Oriented Programming (OOP) אתגר מחזור ב' Classes.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.
EEL 3801 Part VIII Fundamentals of C and C++ Programming Template Functions and Classes.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
The Java Programming Language
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
 A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with.
Abstract Data Type and C++ Classes 1. Object-oriented Programming 2. Abstract Data Types 3. Classes and Objects 4. Examples 5. Member Functions and Member.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
Container Classes  A container class is a data type that is capable of holding a collection of items.  In C++, container classes can be implemented as.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
CITA 342 Section 1 Object Oriented Programming (OOP)
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Xiaoyan Li CSC211 Data Structures Lecture 2 ADT and C++ Classes (I) Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 2 ADT and C++ Classes (I) Instructor: Zhigang Zhu Department of Computer Science City.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Object Oriented Programming Data Structures and Other Objects Using C++ Dr. Donghyun Kim Department of Computer Science College of Computing and Software.
EGR 2261 Unit 13 Classes Read Malik, Chapter 10.
Chapter 1.2 Introduction to C++ Programming
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
Object Oriented Programming
CSC212 Data Structure - Section BC
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Object Oriented Programming
IFS410: Advanced Analysis and Design
Value returning Functions
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 2: Introduction to C++.
Review of Previous Lesson
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Classes and Objects Systems Programming.
Presentation transcript:

 Chapter 2 introduces Object Oriented Programming.  OOP is a relatively new approach to programming which supports the creation of new data types and operations to manipulate those types.  This presentation introduces OOP. Object Oriented Programming Data Structures and Other Objects Using C++

What is this Object ?  There is no real answer to the question, but we’ll call it a “thinking cap”.  The plan is to describe a thinking cap by telling you what actions can be done to it.

Using the Object’s Slots  You may put a piece of paper in each of the two slots (green and red), with a sentence written on each.  You may push the green button and the thinking cap will speak the sentence from the green slot’s paper.  And same for the red button.

Example

That test was a breeze !

Example I should study harder !

Thinking Cap Implementation  We can implement the thinking cap using a data type called a class. class thinking_cap {... };

Thinking Cap Implementation  The class will have two components called green_string and red_string. These compnents are strings which hold the information that is placed in the two slots.  Using a class permits two new features... class thinking_cap {... char green_string[50]; char red_string[50]; };

Basic strings in C++  the are defined as arrays of characters  the variables are defined as arrays of characters   A library class for C++ provides string objects similar to what Java provides   The end of the string is indicated in the array by a character with code 0: the null character   the longest green/red string that is allowed is 49 characters, leaving space for the null character   What happens if we try to store a 70 character string in green_string?   Buffer overflow

Thinking Cap Implementation  The two components will be private member variables. This ensures that nobody can directly access this information. The only access is through functions that we provide for the class. class thinking_cap {... private: char green_string[50]; char red_string[50]; };

Thinking Cap Implementation  In a class, the functions which manipulate the class are also listed. class thinking_cap { public:... private: char green_string[50]; char red_string[50]; }; Prototypes for the thinking cap functions go here, after the word public:

Thinking Cap Implementation  In a class, the functions which manipulate the class are also listed. class thinking_cap { public:... private: char green_string[50]; char red_string[50]; }; Prototypes for the thinking cap member functions go here

Thinking Cap Implementation class thinking_cap { public: void slots(char new_green[ ], char new_red[ ]); void push_green( ) const; void push_red( ) const; private: char green_string[50]; char red_string[50]; }; Our thinking cap has at least three member functions: Function bodies will be elsewhere.

Thinking Cap Implementation class thinking_cap { public: void slots(char new_green[ ], char new_red[ ]); void push_green( ) const; void push_red( ) const; private: char green_string[50]; char red_string[50]; }; The keyword const appears after two prototypes: This means that these functions will not change the data stored in a thinking_cap.

Files for the Thinking Cap  The thinking_cap class definition, which we have just seen, is placed with documentation in a file called thinker.h, outlined here.  The implementations of the three member functions will be placed in a separate file called thinker.cpp, which we will examine in a few minutes. Documentation Class definition: thinking_cap class definition which we have already seen

Using the Thinking Cap  A program that wants to use the thinking cap must include the thinker header file (along with its other header inclusions). #include #include "thinker.h"...

Using the Thinking Cap  Just for fun, the example program will declare two thinking_cap variables named student and fan. #include #include "thinker.h" int main( ) { thinking_cap student: thinking_cap fan;

Using the Thinking Cap  Just for fun, the example program will declare two thinking_cap objects named student and fan. #include #include "thinker.h" int main( ) { thinking_cap student; thinking_cap fan;

Using the Thinking Cap  The program starts by calling the slots member function for student. #include #include "thinker.h" int main( ) { thinking_cap student; thinking_cap fan; student. slots( "Hello", "Goodbye");

Using the Thinking Cap  The program starts by activating the slots member function for student. #include #include "thinker.h" int main( ) { thinking_cap student: thinking_cap fan; student. slots( "Hello", "Goodbye");

Using the Thinking Cap  The member function activation consists of four parts, starting with the object name. int main( ) { thinking_cap student; thinking_cap fan; student. slots( "Hello", "Goodbye"); Name of the object

Using the Thinking Cap  The instance name is followed by a period. int main( ) { thinking_cap student; thinking_cap fan; student. slots( "Hello", "Goodbye"); A Period

Using the Thinking Cap  After the period is the name of the member function that you are activating. int main( ) { thinking_cap student; thinking_cap fan; student. slots( "Hello", "Goodbye"); Name of the Function

Using the Thinking Cap  Finally, the arguments for the member function. In this example the first argument (new_green) is "Hello" and the second argument (new_red) is "Goodbye". #include "thinker.h" int main( ) { thinking_cap student; thinking_cap fan; student. slots( "Hello", "Goodbye"); Arguments

A Quiz How would you activate student's push_green member function ? What would be the output of student's push_green member function at this point in the program ? int main( ) { thinking_cap student; thinking_cap fan; student. slots( "Hello", "Goodbye");

A Quiz Notice that the push_green member function has no arguments. At this point, activating student.push_green will print the string Hello. int main( ) { thinking_cap student; thinking_cap fan; student. slots( "Hello", "Goodbye"); student. push_green( );

A Quiz Trace through this program, and tell me the complete output. int main( ) { thinking_cap student; thinking_cap fan; student. slots( "Hello", "Goodbye"); fan. slots( "Go Owls!", "Boo!"); student. push_green( ); fan. push_green( ); student. push_red( );...

A Quiz Hello Go Owls! Goodbye int main( ) { thinking_cap student; thinking_cap fan; student. slots( "Hello", "Goodbye"); fan. slots( "Go Owlss!", "Boo!"); student. push_green( ); fan. push_green( ); student. push_red( );...

What you know about Objects  Class = Data + Member Functions.  You know how to define a new class type, and place the definition in a header file.  You know how to use the header file in a program which declares instances of the class type.  You know how to activate member functions.  But you still need to learn how to write the bodies of a class’s member functions.

Thinking Cap Implementation class thinking_cap { public: void slots(char new_green[ ], char new_red[ ]); void push_green( ); void push_red( ); private: char green_string[50]; char red_string[50]; }; Remember that the member function’s bodies generally appear in a separate.cxx file. Function bodies will be in.cxx file.

Thinking Cap Implementation class thinking_cap { public: void slots(char new_green[ ], char new_red[ ]); void push_green( ); void push_red( ); private: char green_string[50]; char red_string[50]; }; We will look at the body of slots, which must copy its two arguments to the two private member variables.

Thinking Cap Implementation void thinking_cap::slots(char new_green[ ], char new_red[ ]) { assert(strlen(new_green) < 50); assert(strlen(new_red) < 50); strcpy(green_string, new_green); strcpy(red_string, new_red); } For the most part, the function’s body is no different than any other function body. But there are two special features about a member function’s body...

Thinking Cap Implementation  In the heading, the function's name is preceded by the class name and :: - otherwise C++ won't realize this is a class’s member function. void thinking_cap::slots(char new_green[ ], char new_red[ ]) { assert(strlen(new_green) < 50); assert(strlen(new_red) < 50); strcpy(green_string, new_green); strcpy(red_string, new_red); }

Thinking Cap Implementation  Within the body of the function, the class’s member variables and other member functions may all be accessed. void thinking_cap::slots(char new_green[ ], char new_red[ ]) { assert(strlen(new_green) < 50); assert(strlen(new_red) < 50); strcpy(green_string, new_green); strcpy(red_string, new_red); }

Thinking Cap Implementation  Within the body of the function, the class’s member variables and other member functions may all be accessed. void thinking_cap::slots(char new_green[ ], char new_red[ ]) { assert(strlen(new_green) < 50); assert(strlen(new_red) < 50); strcpy(green_string, new_green); strcpy(red_string, new_red); } But, whose member variables are these? Are they student.green_string student.red_string fan.green_string fan.red_string ?

Thinking Cap Implementation  Within the body of the function, the class’s member variables and other member functions may all be accessed. void thinking_cap::slots(char new_green[ ], char new_red[ ]) { assert(strlen(new_green) < 50); assert(strlen(new_red) < 50); strcpy(green_string, new_green); strcpy(red_string, new_red); } If we activate student.slots: student.green_string student.red_string

Thinking Cap Implementation  Within the body of the function, the class’s member variables and other member functions may all be accessed. void thinking_cap::slots(char new_green[ ], char new_red[ ]) { assert(strlen(new_green) < 50); assert(strlen(new_red) < 50); strcpy(green_string, new_green); strcpy(red_string, new_red); } If we activate fan.slots: fan.green_string fan.red_string

Thinking Cap Implementation void thinking_cap::push_green { cout << green_string << endl; } Here is the implementation of the push_green member function, which prints the green message:

Thinking Cap Implementation void thinking_cap::push_green { cout << green_string << endl; } Here is the implementation of the push_green member function, which prints the green message: Notice how this member function implementation uses the green_string member variable of the object.

A Common Pattern  Often, one or more member functions will place data in the member variables... class thinking_cap { public: void slots(char new_green[ ], char new_red[ ]); void push_green( ) const; void push_red( ) const; private: char green_string[50]; char red_string[50]; }; ...so that other member functions may use that data. slots push_green & push_red

 Classes have member variables and member functions. An object is a variable where the data type is a class.  You should know how to declare a new class type, how to implement its member functions, how to use the class type.  Frequently, the member functions of an class type place information in the member variables, or use information that's already in the member variables.  In the future we will see more features of OOP. Summary