View
219
Download
0
Tags:
Embed Size (px)
Citation preview
Introduction to Data Structures, Spring 2007 Slide- 1
California State University, Fresno
Introduction to Data Structure
Object Oriented Programming Concepts
Ming LiDepartment of Computer Science
California State University, FresnoFall 2006
Introduction to Data Structures, Spring 2007 Slide- 2
California State University, Fresno
Main Concepts of OO Programming
• Encapsulation– abstraction, information hiding
• Inheritance– code reuse, specialization
• Polymorphism– allows old code to use new code
– The actual implementation of the called function depends on type of object
Introduction to Data Structures, Spring 2007 Slide- 3
California State University, Fresno
Object Oriented ProgrammingEncapsulation
• Hiding information within one abstraction
• Achieved via classes and objects.
Introduction to Data Structures, Spring 2007 Slide- 4
California State University, Fresno
Object Oriented Programming - Inheritance
• New code using old code– Code reuse– Less development time– Better code quality
• Example– Table is the original class– DinningTable and ComputerTable can reuse the
code of Table class
Introduction to Data Structures, Spring 2007 Slide- 5
California State University, Fresno
Object Oriented Programming - Polymorphism
• Old code using new code.– reverse reuse
– add new code to a program without having to change the old code
• Calling the same method may yield different results.
• Implemented by virtual function
Introduction to Data Structures, Spring 2007 Slide- 6
California State University, Fresno
What are Classes?
• Classes are blueprints and include– Data (attributes) contained within the class
– Operations (methods) on those data
• Implementation is hidden to user of classes.– A lot of benefits
Introduction to Data Structures, Spring 2007 Slide- 7
California State University, Fresno
More on Classes
• The basics elements of a class are– constructors
• Used in the creation of objects.
• Normally overloaded for the convenience of the class users
– methods• instance methods and class (static) methods
– variables (or fields)• instance variables and class variables (static)
– constants• instance (rare) and class (static)
– other less common things including inner classes, static initializer blocks, non static initializer blocks
Introduction to Data Structures, Spring 2007 Slide- 8
California State University, Fresno
Question?
• What is the difference between class and struct?– Visibility!
Introduction to Data Structures, Spring 2007 Slide- 9
California State University, Fresno
Visibility Modifiers
• All parts of a class have visibility modifiers– public, protected, private
• Public: constructor, method, or field can be accessed outside of the class. – constructors and methods are generally public
• Private: part of the class is hidden and inaccessible by code outside of the class– data fields are generally private
• Protected: available to all friend classes and subclasses
Introduction to Data Structures, Spring 2007 Slide- 10
California State University, Fresno
Example
• Declaration of “person” class:
class Person {public:
Person();Person(char* firstName, char* lastName);SetName(char* firstName, char* lastName);char* GetName();
private:char firstName[20];char lastName[20];
}
Introduction to Data Structures, Spring 2007 Slide- 11
California State University, Fresno
Object - Instantiation• Consider the Person class
Person* p1 = new Person();Person* p2 = new Person();Person* p3 = new Person(“Ming”, “Li”);
• When the new operator is invoked control is transferred to the Person class and the specified constructor is executed, based on parameter matching (if multiple)
• Space(memory) is set aside for the new object's fields
• The memory address of the new object is passed back and stored in the object variable (pointer)
Introduction to Data Structures, Spring 2007 Slide- 12
California State University, Fresno
Object – After Creation
• Every object created has its own instance of the variables declared in the class
• Data and methods of the class can be called by the . or -> operation
Person p1;
printf(“%s\n”,p1.firstName); /* Wrong!!!*/
p1.setName(“Ming”, “Li”);
printf(“%s\n”,p1.getName());
Introduction to Data Structures, Spring 2007 Slide- 13
California State University, Fresno
Class Variables and Class Methods
• Sometimes every object of a class does not need its own copy of a variable or constant– Usually used for constants
• The keyword static is used
public static final int DEFAULT_SIDES = 6;
• Called directly with class name
– Class::X;
Introduction to Data Structures, Spring 2007 Slide- 14
California State University, Fresno
class user{ private: int id; static int next_id;
public: static int next_user_id() { next_id++; return next_id; } /* More stuff for the class user */ user() { id = user::next_id++; //or, id = user.next_user_id(); }};int user::next_id = 0;
Introduction to Data Structures, Spring 2007 Slide- 15
California State University, Fresno
Static Methods
• Static methods are normally utility methods or used to manipulate static variables
• Static methods may not manipulate any instance variables
• Called directly with class name
– Class::method();
Introduction to Data Structures, Spring 2007 Slide- 16
California State University, Fresno
Inheritance• Classes can inherit from other classes• Format
public class Student extends PersonClass Student: public Person
• Person is said to be – the parent class of Student– the super class of Student– the base class of Student– an ancestor of Student
• Student is said to be– a child class of Person– a sub class of Person– a derived class of Person– a descendant of Person
Introduction to Data Structures, Spring 2007 Slide- 17
California State University, Fresno
Results of Inheritance
Public class A
Public class B extends A
• The subclass inherits (gains) all instance variables and instance methods of the super class automatically
• Additional methods can be added to class B (specialization)
• The sub class can replace (redefine, override) methods from the super class
Introduction to Data Structures, Spring 2007 Slide- 18
California State University, Fresno
Overriding methods
• Any method that is not final may be overridden by a descendant class
• Same signature as method in ancestor
• May not reduce visibility
• May use the original method if simply want to add more behavior to existing– Super.X();
Introduction to Data Structures, Spring 2007 Slide- 19
California State University, Fresno
Example
• Declaration of “person” class:
class Person {public:
Person();Person(char* firstName, char* lastName);SetName(char* firstName, char* lastName);char* GetName();
private:char firstName[20];char lastName[20];
}
Introduction to Data Structures, Spring 2007 Slide- 20
California State University, Fresno
Example
• Declaration of “student” class:
class Student:public Person {public:
Student();Student(char* firstName, char* lastName);SetProgram(char* program);char* GetProgarm();SetGrade(int grade);int GetGrade();
private:char program[50];int grade;
}
Introduction to Data Structures, Spring 2007 Slide- 21
California State University, Fresno
Example
• Usage of “Student” class
Student s1;
s1.SetName(“Ming”, “Li”);
printf(“%s\n”,s1.GetName());
s1.SetProgram(“Computer Science”);
printf(“%s\n”,s1.GetProgram());
S1.SetGrade(83);
printf(“%d\n”,s1.GetGrade());
Introduction to Data Structures, Spring 2007 Slide- 22
California State University, Fresno
Overriding example:
class Pet { ....
void speak();};
void Pet::speak() { cout << "Growl" << endl;}
class Rat: public Pet { ...
void speak();};
void Rat::speak() { cout << "Rat noise" << endl;}
Introduction to Data Structures, Spring 2007 Slide- 23
California State University, Fresno
Polymorphism
• Literally “having many forms”
• Object variables can refer to different types
• The actual type is known only at run-time
• C++ uses a virtual table which defines the message to method mapping for a given class
Introduction to Data Structures, Spring 2007 Slide- 24
California State University, Fresno
Acknowledgement
This slides is based partially on the two slides of
the following webpage:
http://www.cs.utexas.edu/~scottm/cs307/schedule.htm
Some examples come from
http://www.cplusplus.com/doc/language/tutorial