Beruflich Dokumente
Kultur Dokumente
Car
Door
Seat
Type
Model
Drive
Stop
Lock
Unlock
Object
Analogy
Apple pie recipe and apple pies
Encapsulation and Inheritance
Encapsulation means both data and methods for an object are
contained inside the object.
Inheritance is a relationship between classes where one class is the
parent class (superclass) of its child class (subclass).
Encapsulation and Inheritance (Cont.)
In C++, for example, if we have a class Student, an object (say
Angelina) is created as follows:
Student Angelina;
We can then create a subclass Primary from its superclass Student as
follows:
class Primary: public class Student
Object Communications
Object communicate by sending messages
Object-Oriented Programming
Three important steps:
Develop class to establish the objects data and behavior
Create object or instance (instantiating)
Sending message to establish object communications
Object-Oriented Programming (Cont.)
class Hello {
public:
void DisplayDetail(); };
// class Hello
void Hello::DisplayDetail()
{
//display details of method
cout << " Hello World! ";
};
// method DisplayDetail
int main()
{ // create a new instance of a class Hello
Hello word;
// invoke a method
word.DisplayDetail();
return 0; };
Advantages of OOP
Reusability
Extensibility
Maintainability
Object and Class (Recall)
Class is a template from which objects are instantiated
Object is created (instantiated) from class
Class Sum
Attributes/Data
Sum2No Services/Methods/Functions
DisplayDetails
Class Template
class class_name {
private:
variables_declaration;
public:
methods_declaration;
}
return_type class_name::method_name(parameters)
{
.
}
Class File vs Class Main
//Filename: Sum.cpp //Filename: SumDr.cpp
//This program declares a class Sum for //This program serves as a driver (main) for class Sum.
//summation of two numbers //It instantiates the class Sum and call method from class
#include <iostream.h> //Sum
class Sum { #include <iostream.h>
public: #include "Sum.cpp"
int Sum2No(int no1, int no2); int main()
void DisplayDetails(int x, int y); {
}; // class Sum // create a new instance of a class Sum
Sum Sumof2No;
int Sum::Sum2No(int no1, int no2) {
//return result of summation of 2 numbers // declaration of variables
return no1 + no2; int x, y;
}; // method Sum2No cout << "Type a number for variable x : ";
cin >> x;
void Sum::DisplayDetails(int x, int y) { cout << "Type a number for variable y : ";
// display output via method cin >> y;
cout << "\nThe sum of " << x << " and " << y; // invoke method
cout << " are " << Sum2No(x, y) << '\n'; Sumof2No.DisplayDetails(x, y);
}; // method DisplayDetails return 0;
}
Object-Oriented Programming
class Hello {
public:
void DisplayDetail(); };
// method implementation
void Hello::DisplayDetail()
{
//display details of method
cout << " Hello World! ";
};
// method DisplayDetail
int main()
{ // create a new instance of a class Hello
Hello word;
// invoke a method
word.DisplayDetail();
return 0; };
Header (*.h) vs. Implementation (*.cpp)
/Filename: StudentD.cpp
//This program serves as a driver (main) for class Student.
//It instantiates the class Student and call methods from class Student
#include "Student.cpp"
int main()
{
// create a new instance of student
Student student1;
// invoke methods
student1.SetName();
student1.GetName();
return 0;
};
Constructor, Destructor and Data
Structures
Constructor and Destructor
A constructor is used as a method to initialize the data of an object
once it has been instantiated from the class.
A destructor is used as a method to delete the object once it has
been used.
Constructor and Destructor (Cont.)
class class_name {
private: variables_declaration;
public: class_name; // declaration of the constructor
public:~class_name;// declaration of the destructor
public: methods_declaration;
};
Use of Constructor and Destructor
class Point{ class Point{
private: private:
int x, y; // point coordinate
int x, y;
public:
public:
Point(); // c1
void setX(int val); Point(int val1, int val2); //c2
void setY(int val); ~Point(); // destructor
int getX(); void setX(int val);
void setY(int val);
int getY();
int getX();
}; // class Point
int getY();
}; // class Point
Data Structures struct
struct struct_name struct Student_Data
{ {
member_declaration; char Name[20];
member_declaration; char Course[30];
. int Result:
}; };
void StudentList::Remove(char
name[25]) void StudentList::operator-=(char
{ name[25])
{
}; // method Remove
}; // method Remove aka -=
Example (Cont.)
#include <iostream.h> #include <iostream.h>
#include "StdList.cpp" #include "StdList.cpp"
int main() { int main() {
const int size = 5; const int size = 5;
int i; int i;
Student newstudent; Student newstudent;
StudentList alist; StudentList alist;
char name[25]; char name[25];
cout <<"Inserting " <<size <<" objects\n"; cout <<"Inserting " <<size <<" objects\n";
for (i=0; i<size; i++) { for (i=0; i<size; i++) {
//create an instance of class Student //create an instance of class Student
newstudent.SetData(); newstudent.SetData();
//add an instance of class Student to the list //add an instance of class Student to the list
alist.Add(newstudent); alist+=newstudent;
} }
alist.DisplayList(); alist.DisplayList();
cout <<"deleting\n"; cout <<"deleting\n";
cout<<"Enter name to be deleted : "; cout<<"Enter name to be deleted : ";
cin>>name; cin>>name;
alist.Remove(name) ; alist-=name ;
cout <<"after removing 1 object\n"; cout <<"after removing 1 object\n";
alist.DisplayList(); alist.DisplayList();
return 0; }; return 0; };
Demo: Operators for Remove and Add Functions
Class Student
Class StudentList using Operator += for Add Function and Operator -=
for Remove Function
Inheritance
Inheritance
Inheritance is a relationship between classes where one class is the
parent class (superclass) of its child class (subclass).
Inheritance is also used to communicate the concept that one class
can inherit part of its behaviour and data from another class.
For example, a subclass of a program can inherit some code from its
superclass. In particular, in specification B inherits A, class B
contains the data and methods defined for class A in addition to
those defined for B.
Inheritance (Cont.)
Inheritance can also be divided into two categories: single inheritance
and multiple inheritance.
Single inheritance means that a subclass may inherit instance
variables and methods of a single parent class, possibly adding some
methods and instance variables of its own. Once the new class
subclasses the other class, it does not need to re-implement the basic
functionality of its superclasses but needs only to add its own
specialized behaviour and state.
Multiple inheritance means that a subclass may inherit instance
variables and methods from multiple parent classes.
Inheritance (Cont.)
Careful: There is a tendency to be confused between the concept of
inheritance and object.
Recall that an object is created (instantiated) from a class.
Inheritance, is created via the relationships between subclasses and
superclass.
An object is created in C++ as follows:
Student Angelina;
A subclass Primary is created as follows:
class Primary: public class Student
Inheritance (Cont.)
C++ permits a new class to be created from an existing class by
inheritance.
The standard C++ syntax for using inheritance in a class is as follows:
class derived_class_name : public base_class_name
{
private: variables_declaration;
public: methods_declaration; //may include constructor declaration
};
Inheritance (Cont.)
Example: Recall Class Point
Then can use class Point to introduce class Circle using inheritance as
follows:
class Circle: public Point {
private :
float Result;
public:
Circle(); //constructor declaration
void SetPoint();
void GetResult();
};
Polymorphism
Polymorphism
Polymorphism is a term used for more than one method that has the
same name.
The methods must belong to different kinds of objects.
Polymorphism also allows significant code sharing and code reuse.
Polymorphism (Cont.)
The compiler determines which function to invoke depending on the
object from which it is instantiated
This invocation is also known as early binding (static binding)
For dynamic binding, C++ introduces virtual function
Virtual Function
For polymorphism, C++ also introduces virtual function for
abstraction and dynamic binding.
Virtual functions are used in C++ to ensure that the correct function is
called from the appropriate derived class.
This is done at run time where dynamic binding occurs.
Virtual Function (Cont.)
class ThisCircle: public ThisPoint {
private:
float Result;
public:
void SetPoint();
void GetResult();
virtual void DisplayDetails(); //changed it to virtual function
};
Digital advertising
History of mobile applications
Integrated into our daily lives
User data:
Users
Tasks
location or context
info requirement to complete task
info available to users
info accessibility or delivery
Designing
Illustrates all of the steps
MAF AMX set data visualization tools, supports offline use (transfer
data from remote source and store it locally, enabling end users to
view info when they are not connected
Development
A set of wizards and editors that
build basic application itself
application features that are implemented from MAF AMX and local HTML
content
Set the endpoint to the access control service (hosts applications user roles)
Cross-platform technologies can build same for Android and iOS devices
Mobile Application Frameworks
Build same application for smartphones or tablets, thereby allowing
reusability of business logic and target various types of devices
Sencha Touch powered by HTML5 and CSS3, providing APIs, animations, and
components that are compatible with the current mobile platforms and browsers.
Ratchet a collection of User Interface and JavaScript plugins, providing reusable HTML
classes
Android: Java
BlackBerry: Java
Why has OOP become so important?
Emergence of the Internet as a place of business
azah@uthm.edu.my