Review: Two Programming Paradigms

Slides:



Advertisements
Similar presentations
Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
Advertisements

1 A pointer variable is a variable whose value is the address of a location in memory int x; x = 5; int* ptr1; ptr1 = &x; int* ptr2; ptr2 = ptr1; *ptr1.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Introduction to C++ Programming Concept Basic C++ C++ Extension from C.
C++ fundamentals.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedural and Object-Oriented Programming 13.1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Learners Support Publications Classes and Objects.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
Object-Oriented Programming Chapter Chapter
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
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?
Introduction to Object-Oriented Programming Lesson 2.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
1 CSE Programming in C++. 2 Overview Sign roster list Syllabus and Course Policies Introduction to C++ About Lab 1 Fill Questionnaire.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Chapter 12 Classes and Abstraction
Pointer to an Object Can define a pointer to an object:
Lecture 3 (UNIT -1) SUNIL KUMAR CIT-UPES.
Procedural and Object-Oriented Programming
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?
Classes and OOP.
Abstract Data Types Programmer-created data types that specify
7. Inheritance and Polymorphism
Polymorphism.
Chapter 3: Using Methods, Classes, and Objects
11.1 The Concept of Abstraction
Introduction to Classes
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?
Chapter 5 Classes.
Chapter Structured Types, Data Abstraction and Classes
This technique is Called “Divide and Conquer”.
CS212: Object Oriented Analysis and Design
C++ Classes & Object Oriented Programming
CS212: Object Oriented Analysis and Design
Introduction to Classes
CS212: Object Oriented Analysis and Design
Object-Oriented Programming
Pointers, Dynamic Data, and Reference Types
Object-Oriented Programming Using C++ Second Edition
Chapter 9 Objects and Classes
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
CPS120: Introduction to Computer Science
COP 3330 Object-oriented Programming in C++
Object-Oriented Programming
Introduction to Classes and Objects
Review: C++ class represents an ADT
Object-Oriented PHP (1)
CPS120: Introduction to Computer Science
Lecture 8 Object Oriented Programming (OOP)
Pointers, Dynamic Data, and Reference Types
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
C++ Object Oriented 1.
Presentation transcript:

Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM OBJECT Operations Data FUNCTION FUNCTION OBJECT Operations Data A list of tasks to perform. Viewed as a collection of interacting objects. Each object can be viewed as an independent machine with a distinct role or responsibility. Operations are closely associated with the objects, carry their own operators around with them . OBJECT Operations Data FUNCTION Function calls Messages passing

Review: Object-Oriented Programming Language Features 1. Data abstraction 2. Inheritance of properties 3. Dynamic binding of operations to objects abstraction: a concept or idea not associated with any specific instance. virtual in C++, after declaration, it’s the responsibility of the programmer to implement a class to instantiate the object of the declaration

Review: C++ Data Types simple structured address integral enum floating float double long double array struct union class char short int long bool address pointer reference

Object-Oriented Programming-- Introduction to Classes Class Definition Class Examples Objects

Classes & Objects The class is the cornerstone of C++ Class: Object: It gives the C++ its identity from C It makes possible encapsulation, data hiding and inheritance Class: Consists of both data and methods Defines properties and behavior of a set of entities Object: An instance of a class A variable identified by a unique name Aim of class: a) provide the programmer with a tool for creating new types that can be used as conveniently as the built-in types (like float) b) user-defined type why? Separate the incidental details of the implementation from the properties essential to the correct use of it 2. Derived class, templates: organizing related classes that allow the programmer to take advantage of their relationships.

Classes & Objects class Rectangle { private: int width; int length; public: void set(int w, int l); int area(); } Rectangle r1; Rectangle r2; Rectangle r3; …… 1. Now, still concrete, don’t differ much from built-in types. (actually no difference when used, only in the way they are created) 3. Class definition: class X {…} 3. Declaration vs. Definition int a;

Define a Class Type class Rectangle { private: int width; int length; public: void set(int w, int l); int area(); }; class class_name { permission_label: member; ... }; Header Body

Class Definition-Data Members Abstract the common attributes of a group of entities, their values determine the state of an object Can be of any type, built-in or user-defined non-static data member Each class object has its own copy Cannot be initialized explicitly in the class body Can be initialized with member function, or class constructor static data member Acts as a global object, part of a class, not part of an object of that class One copy per class type, not one copy per object Can be initialized explicitly in the class body WHY? a) Sometimes, some constraints can cause a class to be useless outside the context. Users will be annoyed by using such context-dependent classes b) machine becomes messy. c) public permission is not willing to be given. Allocated in static memory, constructed once and persists to the end of the program, always has the same address. All objects belonging to the same class share these ( data or functions) We are going to talk one application of this next time when we talk about construction of an object.

Static Data Member class Rectangle { private: int width; int length; Rectangle r1; Rectangle r2; Rectangle r3; class Rectangle { private: int width; int length; static int count; public: void set(int w, int l); int area(); } count r1 r2 width length width length width length r3

Class Definition – Member Functions Used to access the values of the data members (accessor) perform operations on the data members (implementor) Are declared inside the class body, in the same way as declaring a function Their definition can be placed inside the class body, or outside the class body Can access both public and private members of the class Can be referred to using dot or arrow member access operator 1. Functions declared within a class definition are member functions.

Define a Member Function class Rectangle { private: int width, length; public: void set (int w, int l); int area() {return width*length; } } class name member function name scope operator inline 1.Different structures can have member functions with the same name. 2. If defined outside, the structure name when defining a member function must be specified. (::) 3.In a member function, member names can be used without explicit reference to an object (non member fuctions). In this case, the name refers to the member of the object for which the function was invoked. Always know for which object it was invoked. 4. Inline function: the compiler is requested to insert the complete body of the function in every place that the function is called, rather than generation code to call the function in the one place it is defined void Rectangle :: set (int w, int l) { width = w; length = l; } r1.set(5,8); rp->set(8,10);

Class Definition – Member Functions static member function const member function declaration return_type func_name (para_list) const; definition return_type func_name (para_list) const { … } return_type class_name :: func_name (para_list) const { … } Makes no modification about the data members (safe function) It is illegal for a const member function to modify a class data member A static member can be referred to without mentioning an object ( function call outside of the class, but need to be with the name of it’s class. Usage: let the compiler help you find errors, fast and lass expensive. 3. A const member function can be invoked for both const and non-const objects, but a non-const member function can be invoked only for non-const objects. F( const type& m)

Const Member Function class Time { private : int hrs, mins, secs ; public : void Write ( ) const ; } ; function declaration function definition void Time :: Write( ) const { cout <<hrs << “:” << mins << “:” << secs << endl; }

Class Definition - Access Control Information hiding To prevent the internal representation from direct access from outside the class Access Specifiers public may be accessible from anywhere within a program private may be accessed only by the member functions, and friends of this class, not open for nonmember functions protected acts as public for derived classes (virtual) behaves as private for the rest of the program Difference between classes and structs in C++ the default access specifier is private in classes the default access specifier is public in structs Here is the difference between struct and class. Struct is one kind of class. Struct is a class whose members are public by default. (simple examples here) WHY? Generally, any change to the behavior of the class type can and must be effected by changes to its members. Usage: a) For example: Debugging. Illegal value must be caused by code in a member function. b) if we want to change the representation of a class, we need only change the member functions. User code directly depends only on the public interface and need not be rewritten. c) a potential user need examine only the definition of the member functions in order to learn to use a class because other functions cannot deal with the data.

class Time Specification { public : void Set ( int hours , int minutes , int seconds ) ; void Increment ( ) ; void Write ( ) const ; Time ( int initHrs, int initMins, int initSecs ) ; // constructor Time ( ) ; // default constructor private : int hrs ; int mins ; int secs ; } ; Constructor. When you initialize for class objects, some functions need writing to do the tasks. Default: basic initialize float, int, some basic ones. 15

Class Interface Diagram Time class Set Private data: hrs mins secs Increment Write Operations vs. Data Time Time

Class Definition - Access Control The default access specifier is private The data members are usually private or protected A private member function is a helper, may only be accessed by another member function of the same class (exception friend function) The public member functions are part of the class interface Each access control section is optional, repeatable, and sections may occur in any order Friend function vs. static function 1.) access the private part of class declaration 2.) in the scope of the class 3.) must be invoked on an object Friend function, not only in the scope in one class

Objects Object: Declaration of an Object Initiation of an Object a variable or an instance of a class Declaration of an Object Initiation of an Object

What is an object? OBJECT Operations Data set of methods (public member functions) internal state (values of private data members) Operations Data

Declaration of an Object class Rectangle { private: int width; int length; public: void set(int w, int l); int area(); } main() { Rectangle r1; Rectangle r2; r1.set(5, 8); cout<<r1.area()<<endl; r2.set(8,10); cout<<r2.area()<<endl; }

Another Example #include <iostream.h> class circle { private: double radius; public: void store(double); double area(void); void display(void); }; // member function definitions void circle::store(double r) { radius = r; } double circle::area(void) return 3.14*radius*radius; void circle::display(void) cout << “r = “ << radius << endl; int main(void) { circle c; // an object of circle class c.store(5.0); cout << "The area of circle c is " << c.area() << endl; c.display(); }

Take Home Message Class can be considered as a user-defined data type, while an object is just a variable of certain class. There are three parts in the definition of a class: data members, member functions, and access control.