1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
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.
C++ Classes & Data Abstraction
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Chapter 6 Object-Oriented Software Development.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
1 Introduction to C++ Programming Concept Basic C++ C++ Extension from C.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
C++ Classes & Object Oriented Programming. Object Oriented Programming  Programmer thinks about and defines the attributes and behavior of objects. 
1 Chapter 14-1 Object- Oriented Software Development Dale/Weems.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
1 Chapter 14 Object- Oriented Software Development Dale/Weems.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
1 Chapter 14 Object-Oriented Software Development Dale/Weems/Headington.
Chapter 5 Classes and Objects §5.1 Specifying a class §5.2 Defining Member Functions §5.3 Memory Allocation and Static Members §5.4 Passing Objects to/from.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
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.
1 Chapter 14 Object-Oriented Software Development Dale/Weems.
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. 6.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Classes Structured Programming 256 Chapter 8 Classes - Part I OOP & Class Object Terminology File Management.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and 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?
1 Review: C++ class represents an ADT 2 kinds of class members: data members and function members Class members are private by default Data members are.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Introduction to Object Oriented Programming (OOP) Object Oriented programming is method of programming.
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
CS1201: Programming Language 2 Classes and objects.
1 CSE Programming in C++. 2 Overview Sign roster list Syllabus and Course Policies Introduction to C++ About Lab 1 Fill Questionnaire.
1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Classes: Defining Your Own Data Types Basic principles in OOP Define a new data type as a class and use objects of a class Member Functions –Constructors.
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
Class & Objects C++ offers another user-defined data type known class which is the most important feature of the object-oriented programming. A class can.
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.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
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
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?
Review: Two Programming Paradigms
Introduction to Classes
Chapter 5 Classes.
CS212: Object Oriented Analysis and Design
Introduction to Classes
Classes and Objects.
COP 3330 Object-oriented Programming in C++
Introduction to Classes and Objects
CPS120: Introduction to Computer Science
Lecture 8 Object Oriented Programming (OOP)
Presentation transcript:

1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT Operations Data Function calls Messages passing

2 Review: Object-Oriented Programming Language Features 1. Data abstraction 2. Inheritance of properties 3. Dynamic binding of operations to objects

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

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

5 Classes & Objects The class is the cornerstone of C++ –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

6 class Rectangle { private: int width; int length; public: void set(int w, int l); int area(); } Classes & Objects Rectangle r1; Rectangle r2; Rectangle r3; …… int a;

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

8 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

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

10 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

11 Define a Member Function class Rectangle { private: int width, length; public: void set (int w, int l); int area() {return width*length; } } void Rectangle :: set (int w, int l) { width = w; length = l; } inline class name member function name scope operator r1.set(5,8); rp->set(8,10);

12 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 Class Definition – Member Functions

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

14 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 Class Definition - Access Control

15 class Time Specification class Time { 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 ; } ; 15

16 Class Interface Diagram Private data: hrs mins secs Set Increment Write Time Time class

17 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 Class Definition - Access Control

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

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

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

21 Another Example #include 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(); }

22 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.