130 likes | 332 Views
Programming Paradigms. Lecturer Hamza Azeem. Lecture 11. Operator Overloading. Operator Overloading. Operator overloading used for customised behaviour of different operators Make operators sensitive to context The original meaning of the operator is lost
E N D
Programming Paradigms LecturerHamza Azeem
Lecture 11 Operator Overloading
Operator Overloading • Operator overloading used for customised behaviour of different operators • Make operators sensitive to context • The original meaning of the operator is lost • Gives new meaning to the operators in context with the class
Operator Overloading • There are mainly two types of operator overloading • Unary Operators • They only have one operator for e.g. x++, ++x, --y • Binary Operators • They consist of two operators for e.g. a < b, a != b
Unary Operator Overloading • Add a new variable in class KIETStudent with the name courses • Create get_courses() method • Initialize the courses variable in constructor • We will create Unary operator overloading method • Operator used ++ • It will increment the number of courses of student • Usage • KIETStudent s1("Ali”, 27, 2.93, "FALL11", 4); • ++s1;
Unary Operator Overloading class KIETStudent { private: int age; char *name; double cgpa; char *intake; int courses; public: KIETStudent() : age(0), name(" "), cgpa(0), intake(" "), courses(0) { } KIETStudent (char *new_name, intnew_age, double new_cgpa, char *new_intake, intnew_courses) : name(new_name), intake(new_intake), age(new_age), cgpa(new_cgpa), courses(new_courses) { } double get_cgpa(); intget_courses(); char* get_name(); void compare_cgpa(KIETStudent); void operator ++(); };
Unary Operator Overloading intKIETStudent :: get_courses () { return (courses); } void KIETStudent :: operator ++ () { courses = courses + 1; } double KIETStudent :: get_cgpa () { return (cgpa); } char* KIETStudent :: get_name () { return (name); }
Unary Operator Overloading int main() { KIETStudent s1("Hussain Ahmed",25,3.01,"FALL11",5); KIETStudent s2("Ali Shahid",27,2.93,"FALL11",4); printf("No. of Courses: %d",s1.get_courses()); ++s1; printf("\nNo. of Courses: %d",s1.get_courses()); getch(); }
Binary Operator Overloading • We will create Binary operator overloading method • Operator used < • It will compare cgpa of objects KIETStudent • Usage • KIETStudent s1("Hussain Ahmed",25,3.01,"FALL11",5); • KIETStudent s2("Ali Shahid",27,2.93,"FALL11",4); • s1<s2
Binary Operator Overloading class KIETStudent { private: int age; char *name; double cgpa; char *intake; int courses; public: KIETStudent() : age(0), name(" "), cgpa(0), intake(" "), courses(0) { } KIETStudent (char *new_name, intnew_age, double new_cgpa, char *new_intake, intnew_courses) : name(new_name), intake(new_intake), age(new_age), cgpa(new_cgpa), courses(new_courses) { } double get_cgpa(); intget_courses(); char* get_name(); void compare_cgpa(KIETStudent); void operator ++(); bool operator < (KIETStudent); };
Binary Operator Overloading boolKIETStudent :: operator < (KIETStudentotherStudent) { if ( cgpa < otherStudent.cgpa ) return (true); else return (false); } void KIETStudent :: operator ++ () { courses = courses + 1; } intKIETStudent :: get_courses () { return (courses); } char* KIETStudent :: get_name () { return (name); }
Binary Operator Overloading int main() { KIETStudent s1("Hussain Ahmed",25,3.01,"FALL11",5); KIETStudent s2("Ali Shahid",27,2.93,"FALL11",4); if (s1 < s2) printf("%s is smarter than %s", s2.get_name(),s1.get_name()); else printf("%s is smarter than %s", s1.get_name(),s2.get_name()); getch(); }
Operator Overloading • Now we have two methods for same functionality • S1 < S2 • S1. Compare_cgpa(s2) • What is the difference between both ?