1 / 9

Exploring Inheritance and Overloading in C++: Lecture 10 Overview

In Lecture 10 of the Computational Methods of Scientific Programming course, we explore key concepts in C++ programming, focusing on inheritance and operator overloading. The lecture covers how to extend classes in C++ to create derived classes, such as uString, which capitalizes strings by inheriting from the String class. We also examine operator overloading, allowing custom functionality for operators like + and = within classes. By providing practical examples, we illustrate how these features enhance code reusability and encapsulation in object-oriented programming.

harvey
Télécharger la présentation

Exploring Inheritance and Overloading in C++: Lecture 10 Overview

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. 12.010 Computational Methods of Scientific ProgrammingLecture 10 Today’s lecture Inheritance and overloading in C++ Web page http://www-gpsg.mit.edu/~tah/12.010

  2. Previous Lecture • Basic features of C++ • Adds formal class concept to C, making it object-oriented • Class is like a derived type except • It is better encapsulated • It has “methods” • Invoking a method is like sending a message to the object, object contains its own logic saying what to do. • E.g the String class String s1; s1.set(“Hello”); printf(“%s\n”,s1.s()); 12.010

  3. Inheritance Want new class uString. Like String except that the strings will be converted and stored in upper case. e.g. String uString set() s() set() s() uString s; s.set(“Hello”); printf(“%s\n”,s.s()); HELLO String s; s.set(“Hello”); printf(“%s\n”,s.s()); Hello 12.010

  4. uString extends String • No need to write uString from scratch. • Inherit most code from String. • Extend String::set to capitalise. • A uString is a String with some extra feature. String Base class set() s() uString Derived class 12.010

  5. C++ InheritanceExample • New interface for uString /* Extend String class to uString */ /* uString stores strings as upper case */ class uString : public String { public: void set( char *); /* Set a uString */ }; 12.010

  6. uString set method /* Set str to point to a private copy of s */ void uString::set(char *s) { int i; String::set(s); for (i=0;i<strlen(s);++i) { if ( str[i] >= 'a' && str[i] <= 'z' ) { str[i] = toupper(str[i]); } } } Base class method “protected” (not “private”) 12.010

  7. uString in action! main() { String s1; uString s2; printf("Executable code starting\n"); s1.set("Hello"); printf("%s\n",s1.s()); s2.set("Hello"); printf("%s\n",s2.s()); printf("Executable code ending\n"); } 12.010

  8. Overloading Can redefine operators e.g. + to operate on classes e.g. coord =() +() coord p1, p2, p3; p3 = p1 + p2 This would then do if p1=p2=(1,1,1) p3 = (2,2,2) 12.010

  9. Overloading Have to define the meaning of + and = for a coord class object. Language defines meaning for integer, float, double etc but now we can define extra meanings. coord coord::operator+ (coord c2) { coord temp; temp.cx = cx + c2.cx; temp.cy = cy + c2.cy; temp.cz = cz + c2.cz; return(temp); } class coord{ public: coord operator+(coord ); private: int cx; int cy; int cz; }; 12.010

More Related