Object Oriented Programming Data Structures and Other Objects Using C++ Dr. Donghyun Kim Department of Computer Science College of Computing and Software.

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

 Chapter 2 introduces Object Oriented Programming.  OOP is a relatively new approach to programming which supports the creation of new data types and.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 14: Overloading and Templates
Classes and Objects Systems Programming.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
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.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Operator Overloading in C++
Review of C++ Programming Part II Sheng-Fang Huang.
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.
Case Study - Fractions Timothy Budd Oregon State University.
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.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 3 ADT and C++ Classes (II) Instructor: Zhigang Zhu Department of Computer Science.
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.
1 CSC241: Object Oriented Programming Lecture No 02.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 25 December 1, 2009.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
Computing and Statistical Data Analysis Lecture 6 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Introduction to classes and objects:
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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.
 Chapter 2 introduces Object Oriented Programming.  OOP is a relatively new approach to programming which supports the creation of new data types and.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
EGR 2261 Unit 13 Classes Read Malik, Chapter 10.
Chapter 13: Overloading and Templates
C++ Templates.
Object Oriented Programming
CSC212 Data Structure - Section BC
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Constructors and Other Tools Dr.
Object-Oriented Design (OOD) and C++
Chapter 14 Templates C++ How to Program, 8/e
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
CSC212 Data Structure - Section EF
group work #hifiTeam
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.
Separate Compilation and Namespaces
Object Oriented Programming
Operator Overloading; String and Array Objects
Operator Overloading; String and Array Objects
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Introduction to Classes and Objects
Classes and Objects.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Class: Special Topics Overloading (methods) Copy Constructors
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
C++ data types.
Classes and Objects Systems Programming.
(4 – 2) Introduction to Classes in C++
Introduction to Classes and Objects
Presentation transcript:

Object Oriented Programming Data Structures and Other Objects Using C++ Dr. Donghyun Kim Department of Computer Science College of Computing and Software Engineering Kennesaw State University Marietta, GA, USA This slide is courtesy of Dr. Michael Main and Dr. Walter Savitch at University of Colorado at Boulder.

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 components 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]; };

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 Our thinking cap has at least three member functions: 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]; }; Function bodies will be elsewhere.

Thinking Cap Implementation The keyword const appears after two prototypes: 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]; }; T his me ans that the se fun ctio ns w ill not cha nge th e da ta s tor ed i n a thin king _ca p.

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 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"); Na me o f th e o bje ct

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 Per iod

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"); N ame of t he Fu ncti on

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"); Ar gu men ts

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 Owls!", "Boo!"); student. push_green( ); fan. push_green( ); student. push_red( );...

What you knowabout 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 Remember that the member function’s bodies generally appear in a separate.cpp file. 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]; }; Function bodies will be in.cpp file.

Thinking Cap Implementation We will look at the body of slots, which must copy its two arguments to the two private member variables. 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]; };

Thinking Cap Implementation For the most part, the function’s body is no different than any other function body. 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 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 Here is the implementation of the push_green member function, which prints the green message: void thinking_cap::push_green { cout << green_string << endl; }

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

C++Class Syntax ● 2 files – Header file (.h) – specification – Implementation file (.cpp,.cxx,.cc, …) - implementation ● Need to include the header file ● Constructors – Call to default constructor – no empty parentheses

C++Class Syntax ● namespace – Prevent identifier conflict ● ● ● namespace xxx { } ● – Never “use” namespace in a header file Copy constructor – Specifies how to copy an object – Default automatically created

C++Parameters ● ● ● Value parameters – Syntax: type_name var_name – Copy of actual argument is sent to function Reference parameters – Syntax: type_name & var_name – Reference to actual argument is sent to function const reference parameters – Syntax: const type_name & var_name – Reference to actual argument is sent to function – function cannot change actual argument

Operator Overloading ● ● Overload standard operators in given the operator a meaning in a new context Overloading binary comparison operators – – bool opera t or op ( c onst type_name & var_name1, const type_name & var_name2); Allows developer to give a meaning to that operator for objects of type type_name ● Overloading arithmetic operators – Same idea

Operator Overloading ● Overloading I/O operators – Allow direct input and/or output of “new types”

Friend Functions ● ● Not a member function of a class Can access private members of the class

Standard Template Library ● STL – contains template classes for containers and standard operations