Object-Oriented Programming

Slides:



Advertisements
Similar presentations
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Advertisements

CS-212 Intro to C++. Abstract Data Type Abstraction of a real world object as a mathematical entity Implementation independent model of the entity Emphasis.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
1 C++ Structures Starting to think about objects...
C++ Classes & Object Oriented Programming. Object Oriented Programming  Programmer thinks about and defines the attributes and behavior of objects. 
Review of C++ Programming Part II Sheng-Fang Huang.
1 Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
CHAPTER 13 CLASSES AND DATA ABSTRACTION. In this chapter, you will:  Learn about classes  Learn about private, protected, and public members of a class.
C++ Review (3) Structs, Classes, Data Abstraction.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 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.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
CLASSES : A DEEPER LOOK Chapter 9 Part I 1. 2 OBJECTIVES In this chapter you will learn: How to use a preprocessor wrapper to prevent multiple definition.
Chapter 9 Classes: A Deeper Look, Part I Part II.
Programming Fundamentals1 Chapter 8 OBJECT MANIPULATION - INHERITANCE.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
1 2/21/05CS250 Introduction to Computer Science II Destructors, Get and Set, and Default Memberwise Assignment.
1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
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.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Chapter 9 Classes: A Deeper Look, Part 1 Seventh Edition C++ How to Program © by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
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
About the Presentations
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.
This technique is Called “Divide and Conquer”.
Object-Oriented Programming Using C++
This pointer, Dynamic memory allocation, Constructors and Destructor
C++ Classes & Object Oriented Programming
Introduction to Classes
Classes and Data Abstraction
Object-Oriented Programming Using C++ Second Edition
Classes Short Review of Topics already covered Constructor
Starting to think about objects...
Constructors and Destructors
Classes and Objects.
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
CPS120: Introduction to Computer Science
7 Arrays.
9-10 Classes: A Deeper Look.
Object-Oriented Programming Using C++
Submitted By : Veenu Saini Lecturer (IT)
Recitation Course 0520 Speaker: Liu Yu-Jiun.
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Chapter 9 Introduction To Classes
More C++ Classes Systems Programming.
9-10 Classes: A Deeper Look.
(4 – 2) Introduction to Classes in C++
SPL – PS3 C++ Classes.
Presentation transcript:

Object-Oriented Programming AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: 0511261 Fall 2014

Classes and Objects ►Class is a new data type which is used to define objects. A class serves as a plan, or a template. It specifies what data and what functions will be included in objects of that class. Writing a class doesn’t create any objects. ► A class is a description of similar objects. ► Objects are instances of classes.

Example A model (class) to define points in a graphics program. ►Points on a plane must have two properties (states): – x and y coordinates. We can use two integer variables to represent these properties. ►In our program, points should have the following abilities (behavior): – Points can move on the plane: move function – Points can show their coordinates on the screen: print function – Points can answer the question whether they are on the zero point (0,0) or not: is_zero function

Class Definition : Point class Point { // Declaration of Point Class int x,y; public: void move(int, int); void print(); bool is_zero(); }; Data and functions in a class are called members of the class. In our example only the prototypes of the functions are written in the class declaration. Every element following public:keyword is accessible from outside of class If the body of a function is written in the class declaration, then this function is defined as an inline function (macro). Classes are same as structures BUT members of classes are private by default, but members of Structures are public.

Bodies of member function // A function to move the points void Point::move (int new_x, int new_y) { x = new_x; // assigns new value to x coordinate y = new_y; // assigns new value to y coordinate } Now we have a model (template) to define point objects. We can create necessary points (objects) using the model. int main() { Point point1; //declare object or class point point1; point1.move(100,50); // point1 moves to (100,50) Return 0;

Example Class Diagram ► A class is a grouping of data and functions .used to create a variable which can be manipulated in a program. ► An object is an instance of a class, which is similar to a variable defined as an instance of a type. An object is what you actually use in a program.

Inline Function A normal function is placed in a separate section of code and a call to the function generates a jump to this section of code. In Inline Function: A copy of the function statements is placed directly into the compiled calling program. Less overhead. Used when: » The number of statements is small (one or two lines in the body of the function) » The function is called few times

Inline Function…Cont. Or Two way to write inline function: class Point { int x,y; public: void print(){ cout<<x <<“ ” <<y}; //inline function }; Or Inline void print(); //inline function Void print ::Point{ cout<<x <<“ ” <<y }

Controlling Accesses to Members We can divide programmers into two groups: class creators (those who create new data types) and client programmers (the class consumers who use the data types in their applications) The goal of the class creator is to build a class that includes all necessary properties and abilities. The class should expose only what’s necessary to the client programmer and keeps everything else hidden The hidden parts are only necessary for the internal machinations of the data type but not part of the interface that users need in order to solve their particular problems The class creator can change the hidden portion at will without worrying about the impact to anyone else.

Controlling Accesses to Members public: , private and protected are used to control access to a class' data members and functions. Private class members can be accessed only by members of that class. Public members may be accessed by any function in the program. The default access mode for classes is private

Constructors The class designer can guarantee initialization of every object by providing a special member function called the constructor. The constructor is invoked automatically each time an object of that class is created (instantiated). These functions are used to (for example): assign initial values to the data members open files establish connection to a remote computer etc. The constructor can take parameters as needed, but it cannot have a return value The constructor has the same name as the class itself.

Defaults constructor Defaults constructor: a constructor that can be invoked with no arguments Example class Point{ int x,y; public: Point(); // Declaration of the default constructor bool move(int, int); void print(); }; Point::Point() { // Default Constructor cout << "Constructor is called..." << endl; x = 0; y = 0; }

Example :Constructor with Parameters class Point{ int x,y; public: Point(int,int); bool move(int, int); void print(); }; Point::Point(int x_first, int y_first) { if ( x_first < 0 ) x = 0; else x = x_first; if ( y_first < 0 ) y = 0; // Assigns zero to x y = y_first; } int main() { Point p1(20, 100); // Construct is called 2 times Point p3; // ERROR! There is not a default constructor :

Constructor Parameters with default value Example class Point{ int x,y; public: Point(int x_first = 0, int y_first = 0); bool move(int, int); void print(); }; Point::Point(int x_first, int y_first) { if ( x_first < 0 ) x = 0; else x = x_first; if ( y_first < 0 ) y = 0; // Assigns zero to x y = y_first; } int main() { Point p1(20, 100); // Construct is called Point p3; // x=0, y=0 :

Copy Constructor It is a special type of constructors and used to copy the contents of an object to a new object during construction of that new object. The type of its input parameter is a reference to the object that will be copied into the new object. Example #include<iostream.h> class point{ public: int x,y; point(const point &c):x(c.x), y(c.y){} point():x(0), y(0){} print(){cout<<x<<" "<<y;} }; int main() { point p1; p1.x=20; p1.y=50; p1.print(); point p2(p1); // or point p2=p1; p2.print(); }

Multiple Constructors Class may have more than one constructor with different type of input parameters. Example Point::Point() { // Default constructor ............... // Body is not important } Point::Point(int x_first, int y_first) { // A constructor with parameters ................. // Body is not important The client programmer can define objects in different ways int main() { Point p1(20, 100); // Construct is called Point p3; Point p3(10); //ERROR! There isn't a constructor with one parameter :

Destructors The destructor is very similar to the constructor except that it is called automatically when a dynamic object is deleted from memory by using the delete operator. A destructor is characterized as having the same name as the class but with a tilde ‘~’ preceded to the class name. A destructor has no return type and receives no parameters. A class may have only one destructor.

Example: Destructors Example class Point{ int x,y; public: Point(int x_first = 0, int y_first = 0); bool move(int, int); void print(); ~Point(); }; Point::~Point() { cout << "Destructor has been invoked" << endl; }

Static Class Member Normally, each object of a class has its own copy of all data members of the class. Static data :one copy of a particular data member should be shared by all objects of a class Static data must be initialized once (and only once) at file scope and it can be altered Constant data cannot be changed but a copy is made for every object. class AA{ char c; static int i; }; int main() { AA p, q, r; : }

Example double Student::RegistrationFee = 50.5; void Student::setIdNum(int num) { idNum = num; } int Student::getIdNum() return idNum; double Student::getRegFee() return RegistrationFee; #include<iostream> class Student { private: int idNum; static double RegistrationFee; public: void setIdNum(int); int getIdNum(); double getRegFee(); };

Example…Cont. int main() { Student Std1, Std2; Std1.setIdNum(1234); Std2.setIdNum(2345); cout << “Std1: " << Std1.getIdNum() << " Registration fee: " << Std1.getRegFee() <<endl; cout << “Std2: " << Std2.getIdNum() << " Registration fee: " << Std2.getRegFee() <<endl; return 0; } Result: Std1: 1234 Registration Fee: 50.5 Std2: 2345 Registration Fee: 50.5

Static Class Member ..Cont. Static member can be accessed without using an object (if it is public) Ex: cout << "The Registration fee for all students is " << Student::RegistrationFee << endl; Or Student::RegistrationFee = 139.95; OR Std1.RegistrationFee= 45 Because Static fields frequently are constant use const static double RegistrationFee; Constant static fields hold one unchangeable value for every object. A static function is one you can use with or without a declared object A non-static function can use a static field, but a static function cannot use a non-static field.

This Pointer Use This pointer in a function: Within a function like getIdNum(), the address of the calling object is stored in a special pointer called the this pointer. The this pointer holds the memory address of the current object that is using the function The this pointer is automatically supplied every time you call a non-static member function of a class. The this pointer is a constant pointer. You cannot modify it. When you make the function call Std1.getIdNum() the actual function call made by the compiler is getIdNum(&Std1) The actual argument list used by the compiler for the getIdNum()function is getIdNum(Student *this). Use This pointer in a function: void Student::setIdNum(int id) { (*this).idNum = id; } int Student ::getIdNum() return (*this).idNum;

Pointers to objects class Point { // Declaration of Point Class public: int x,y; void move(int, int); }; int main() { Point *p; //declare pointer to an object P= new point; (*p).x =20; P -> y =50; P -> move(100,50); //or (*p).mive(100,50) return 0; }

Implement Time class class Time { int hour; int minute; int second ; public: (Inline)int GetHour() {return hour;} ; int GetMinute() {return minute;} ; int GetSecond () { return second;} ; Void time(int h=0,int m=0,int s=0); void SetTime(int h,int m,int s); void SetHour(int h); void SetMinute(int m); void SetSecond(int s); void PrintTime(); } Hour,minut,second private attribute we update or gets iys value throught the function

Time class…cont. Void Time::SetTime(int h,int m,int s) {hour=h;minute=m;second=s;}//or Time::hour=h;… void Time ::SetHour(int h) {hour= h;} void Time ::SetMinute(int m) {minute= m;} void Time ::SetSecond (int s) {second= s;} void Time ::PrintTime (){ Cout<<hour<<“:”<<minut<<“:”<<second<<endl; } Void Time::time (int h=0,int m=0,int s=0) {hour=h;minute=m;second=s;} :: Scope resolution operator

Time class…cont. Main(){ Time t(9,12,46); t. GetHour(); t. GetMinute(); t. GetSecond (); t. SetTime(10,00,00); t. SetHour(11); t. SetMinute(15); t. SetSecond(30); t. PrintTime(); } 9 12 46 11:15:30

Student class #include<iostream.h> class Student { private: int idNum; Char * lastName; double gradePointAverage; public: void displayStudentData(); void setIdNum(int); void setLastName(string); void setGradePointAverage(double); };

Student class …cont. int main() { Student aStudent; Student anotherStudent; aStudent.setLastName("Smith"); aStudent.setIdNum(3456); aStudent.setGradePointAverage(3.5); aStudent.displayStudentData(); anotherStudent.setLastName(“Ali"); anotherStudent.setIdNum(34567); anotherStudent.setGradePointAverage(4.5); anotherStudent.displayStudentData(); return 0; } Result: Student #3456’s last name is Smith The grade point avarage for this student is 3.5 Student #9999’s last name is Ali The grade point avarage for this student is 0 two colons (::)