Programming Paradigms

17
Programming Paradigms Lecturer Hamza Azeem

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 of Programming Paradigms

Page 1: Programming Paradigms

Programming Paradigms

LecturerHamza Azeem

Page 2: Programming Paradigms

Lecture 9

Objects and Classes

Page 3: Programming Paradigms

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

Page 4: Programming Paradigms

Classes

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

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

Page 5: Programming Paradigms

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

Page 6: Programming Paradigms

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); }

}

Page 7: Programming Paradigms

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

Page 8: Programming Paradigms

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)

Page 9: Programming Paradigms

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;

Page 10: Programming Paradigms

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.

Page 11: Programming Paradigms

Creating Class

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

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

Page 12: Programming Paradigms

Creating Class

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

Page 13: Programming Paradigms

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(); }

Page 14: Programming Paradigms

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

Page 15: Programming Paradigms

Creating Class

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

Page 16: Programming Paradigms

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

Page 17: Programming Paradigms

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(); }