Object-Oriented Programming. Procedural Programming All algorithms in a program are performed with functions and data can be viewed and changed directly.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Operator overloading redefine the operations of operators
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Chapter 19 - Inheritance Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Chapter 20- Virtual Functions and Polymorphism Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng.
Esempi Ereditarietà1 // Definition of class Point #ifndef POINT_H #define POINT_H #include using std::ostream; class Point { friend ostream &operator
Approfondimento Classi - Esempi1 // Argomento: Oggetti membri di altre classi // Declaration of the Date class. // Member functions defined in date1.cpp.
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
CPSC 231 C++ Review1 Learning Objectives §Review of the object oriented design goals. §Review of C++ classes l data members l member functions (methods)
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20- Virtual Functions and Polymorphism Outline 20.1Introduction 20.2Type Fields and switch Statements.
Computer Science 1620 Function Scope & Global Variables.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
C++ Classes & Object Oriented Programming. Object Oriented Programming  Programmer thinks about and defines the attributes and behavior of objects. 
1 Object-Oriented Programming Using C++ CLASS 27.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Savitch Chapter 10  Defining Classes  Topics: Structures Class Definitions Members ○ Data Members ○ Member Functions ○ Public/Private Sections ○ Types.
Structured Programming Instructor: Prof. K. T. Tsang Lecture 13:Object 物件 Oriented 面向 Programming (OOP)
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 20 - C++ Virtual Functions and Polymorphism.
Starting Out with C++, 3 rd Edition 1 Chapter 13 – Introduction to Classes.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
Comp 245 Data Structures (A)bstract (D)ata (T)ypes ADT.
11 Introduction to Object Oriented Programming (Continued) Cats.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance Part 2 Outline 19.8Direct Base Classes and Indirect Base Classes 19.9Using Constructors.
1 C++ Classes: Access (II) Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
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.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
Object-Oriented Paradigm The Concept  Bundled together in one object  Data Types  Functionality  Encapsulation.
Lecture 19: Introduction to Classes Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.
1 Reference Variables Chapter 8 Page Reference Variables Safer version of C/C++ pointer. "Refers to" a variable. Like a pointer. Effectively.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance – Part 1 Outline 19.1Introduction 19.2Inheritance: Base Classes and Derived Classes.
Matthew Small Introduction to Object-Oriented Programming.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Polymorphism Outline 20.5Polymorphism 20.6Case Study: A Payroll System Using Polymorphism.
1 Introduction to Object Oriented Programming Chapter 10.
Lecture #6 Classes and Objects.
Object Oriented Programming COP3330 / CGS5409.  Classes & Objects  DDU Design  Constructors  Member Functions & Data  Friends and member functions.
1 Compiler directive: #define, usage 1 #include using namespace std; #define TAX //double TAX=0.08; #define LAST_NAME "Li" #define FIRST_NAME "Dennis"
1 Object-Oriented Programming Using C++ CLASS 2 Honors.
1 Class 19 Chapter 13 – Creating a class definition.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
#define #include<iostream> using namespace std; #define GO
Review What is an object? What is a class?
Programmer-Defined Functions, Call-by-Value, Multiple Files Lab 5
group work #hifiTeam
Chapter 20- Virtual Functions and Polymorphism
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
CPS120: Introduction to Computer Science
COP 3330 Object-oriented Programming in C++
Introduction to Classes and Objects
NAME 436.
CPS120: Introduction to Computer Science
Class rational part2.
Lecture 8 Object Oriented Programming (OOP)
Presentation transcript:

Object-Oriented Programming

Procedural Programming All algorithms in a program are performed with functions and data can be viewed and changed directly by programmer. Object-Oriented Programming All program can be made up as individual entity called objects. A program that is written utilizing these objects is called object-oriented program. Each of the object used can be written by any vendor (programmer), which has a specific purpose (task). Object can be created and deleted as it goes out of scope. Object has its own attributes and functionality. The data in an object is not intended to be accessed directly by code that is outside of the object. This “hiding” of data is known as the term encapsulation.

// circle.h #ifndef _CIRCLE_H #define _CIRCLE_H const double PI= ; class circle { public: circle(); // default constructor circle(const circle &); // copy constructor void SetRadius(double); // member functions double Area(); private: double radius;// data }; // default constructor circle::circle() { radius = 0.0; } circle::circle( const circle & obj) { radius = obj.radius; } // member functions void circle::SetRadius( double R) { radius = R; } double circle::Area() { return PI * radius * radius; }

The circle class has two constructor: The first constructor is known as the default constructor. It is used when the object is declared. The second constructor is known as the copy constructor. It is used when the object is declared with a reference to another object as an argument. Compiler Directive: #ifndef, #define, #endif If is used to make sure the compiler would not define the class definition if it has already been defined. If the compiler has gone through the code (class definition), it will skip the code the subsequent time.

18 // oop.cpp--- This object-oriented program shows the use of simple objects which represent circles. #include "circle.h" // contains the circle class #include using namespace std; int main() { circle Circle_One; // declare objects circle Circle_Two; // of type circle double User_Radius; double Area; cout << "\nWhat is the radius of the first circle? "; cin >> User_Radius; // send a message to Circle_One telling it to set its radius to User_Radius Circle_One.SetRadius(User_Radius);

18 cout << "\nWhat is the radius of the second circle? "; cin >> User_Radius; // send a message to Circle_Two telling it to set its radius to User_Radius Circle_Two.SetRadius(User_Radius); // send a message to Circle_One asking for its area Area = Circle_One.Area(); cout.setf(ios::fixed); cout << "\nThe area of the first circle is " << Area << ".\n"; // send a message to Circle_Two asking for its area cout << "\nThe area of the second circle is " << Circle_Two.Area() << ".\n"; circle Circle_Three(Circle_Two); // send a message to Circle_Two asking for its area cout << "\nThe area of the second circle is " << Circle_Three.Area() << ".\n"; return 0; }