Programming Paradigms

Post on 12-Jan-2016

34 views 0 download

Tags:

description

Programming Paradigms. Lecturer Hamza Azeem. Lecture 9. Objects and Classes. Revision of Objects and Classes. What is an Object ? An object is a single identity which compromises of following concepts; characteristics responsibilities (or behaviors) Or simply Object = Data + Function. - PowerPoint PPT Presentation

transcript

Programming Paradigms

LecturerHamza Azeem

Lecture 9

Objects and Classes

Revision of Objects and Classes

• What is an Object ?

• An object is a single identity which compromises of following concepts;

• characteristics• responsibilities (or behaviors)

• Or simply

Object = Data + Function

Classes

• A class is like a cookie-cutter; it defines the shape of object

• Objects are like cookies; they are instances of the class

Classes versus Objects

• A class is a prototype specification from which one can generate a number of similar objects

• A class can be considered as an object factory

• An object is said to be a instance of a class

Example of a Class in C++

class student //declares a class { private: int id, age; //class data public: int getage() //method to get age of object {

return (age); }

}

Class and its Objects

person data: name, address, age methods: getage()

Class

person data: Khalid, Clifton, 24

person data: Rashid, Tariq Road, 25

person data: Ali, N.Nazimabad, 28

Class Example

• This class example shows how we can encapsulate (gather) a circle information into one package (class)

class Circle{ private:

double radius; public:

void setRadius(double r);double getDiameter();

double getArea();double getCircumference();

};

No need for others classes to access and retrieve its value directly. Theclass methods are responsible forthat only.

They are accessible from outsidethe class, and they can access themember (radius)

Creating an object of a Class

• Declaring a variable of a class type creates an object. You can have many variables of the same type (class).– Creating Instance

• Once an object of a certain class is created, a new memory location is created for it to store its data members and code

• You can create many objects from a class type.– circle smallcircle; – circle bigcircle;

10

The two steps of Object Oriented Programming

• Making Classes: Creating, extending or reusing abstract data types.

• Making Objects interact: Creating objects from abstract data types and defining their relationships.

Creating Class

• Here we will create a class “KIETStudent” which has following characteristics and behaviors

• Name, Age, InTake, CGPA• getAge, getName, getInTake, getCGPA

Creating Class

class KIETStudent{ public: int age; char *name; double cgpa; char* intake;};

Creating Instanceclass KIETStudent{ public:

char *name; int age; double cgpa; char *intake;}; int main(){ // Create instane of class KIETStudent KIETStudent student1; // Fill out the data student1.age = 15; student1.name = "Ali"; student1.cgpa = 3.12; student1.intake = "FALL09";

// Display the student data printf("Student's Data"); printf("\nName: %s",student1.name); printf("\nAge: %d",student1.age); printf("\nIntake: %s",student1.intake); printf("\nCPGA: %.2lf",student1.cgpa); getch(); }

Public and Private Fields

• We learned in previous lecture that basic characteristics should not be made public but rather private

• So the characteristics should be transferred in private domain

Creating Class

class KIETStudent{ private: int age; char *name; double cgpa; char* intake;};

Class Methods (Functions)

• Since the characteristics of object are made private we now need methods to modify and access them

• These methods will be made public

Creating Methodsclass KIETStudent{ private: int age; char *name; double cgpa; char *intake; public: void set_age(int newage); int get_age();};void KIETStudent::set_age(int new_age){ age = new_age; printf("\nAge successfully set to %d.", age);}

int KIETStudent::get_age(){ return(age);}

int main(){ // Create instane of class KIETStudent KIETStudent student1; // Fill out the data student1.set_age(20); // Display the data printf("\nStudent's Data"); printf("\nAge: %d", student1.get_age() ); getch(); }