1 / 17

Programming Paradigms

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.

dino
Download Presentation

Programming Paradigms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Paradigms LecturerHamza Azeem

  2. Lecture 9 Objects and Classes

  3. 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

  4. Classes • A class is like a cookie-cutter; it defines the shape of object • Objects are like cookies; they are instances of the class

  5. 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 instanceof a class

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

  7. person data: Khalid, Clifton, 24 person data: Rashid, Tariq Road, 25 person data: Ali, N.Nazimabad, 28 Class and its Objects person data: name, address, age methods: getage() Class

  8. Class Example • This class example shows how we can encapsulate (gather) a circle information into one package (class) No need for others classes to access and retrieve its value directly. The class methods are responsible for that only. class Circle { private: double radius; public: void setRadius(double r); double getDiameter(); double getArea(); double getCircumference(); }; They are accessible from outside the class, and they can access the member (radius)

  9. 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. • circlesmallcircle; • circlebigcircle;

  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.

  11. Creating Class • Here we will create a class “KIETStudent” which has following characteristics and behaviors • Name, Age, InTake, CGPA • getAge, getName, getInTake, getCGPA

  12. Creating Class • class KIETStudent • { • public: • int age; • char *name; • double cgpa; • char* intake; • };

  13. Creating Instance // 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(); } class 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";

  14. 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

  15. Creating Class • class KIETStudent • { • private: • int age; • char *name; • double cgpa; • char* intake; • };

  16. 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

  17. Creating Methods 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(); } class KIETStudent { private: int age; char *name; double cgpa; char *intake; public: void set_age(intnewage); intget_age(); }; void KIETStudent::set_age(intnew_age) { age = new_age; printf("\nAge successfully set to %d.", age); } intKIETStudent::get_age() { return(age); }

More Related