Programming Paradigms - PowerPoint PPT Presentation

programming paradigms n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Programming Paradigms PowerPoint Presentation
Download Presentation
Programming Paradigms

play fullscreen
1 / 13
Programming Paradigms
155 Views
Download Presentation
jamuna
Download Presentation

Programming Paradigms

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Programming Paradigms LecturerHamza Azeem

  2. Lecture 11 Operator Overloading

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

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

  5. 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;

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

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

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

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

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

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

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

  13. Operator Overloading • Now we have two methods for same functionality • S1 < S2 • S1. Compare_cgpa(s2) • What is the difference between both ?