170 likes | 256 Views
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.
E N D
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 instanceof 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); } }
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
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)
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;
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 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";
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 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); }