1 / 35

FRIEND FUNCTION

FRIEND FUNCTION. #include <iostream> using namespace std; class Rectangle { int width, height ; public: Rectangle() {} Rectangle (int x, int y) : width(x), height(y) {} int area() {return width * height;} friend Rectangle duplicate (const Rectangle&); };

aluz
Download Presentation

FRIEND FUNCTION

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. FRIEND FUNCTION #include <iostream> using namespace std; class Rectangle { int width, height; public: Rectangle() {} Rectangle (int x, int y) : width(x), height(y) {} int area() {return width * height;} friend Rectangle duplicate (const Rectangle&); }; Rectangle duplicate (const Rectangle& param) // This is a non-member function allowed to use private members { Rectangle res; res.width = param.width*2; // using a private variable inside a Rectangle type object res.height = param.height*2; return res; } int main () { Rectangle foo; Rectangle bar (2, 3); foo = duplicate (bar); cout << foo.area() << '\n'; } Try removing the key-word friend! 24 http://www.cplusplus.com/doc/tutorial/inheritance/

  2. FRIENDCLASS #include <iostream> using namespace std; class Square; // to satisfy compiler class Rectangle { int width, height; public: int area () {return (width * height);} void convert (Square a); }; class Square { friend class Rectangle; // Rectangle class can now use Square’s private members private: int side; public: Square (int a) : side(a) {} }; void Rectangle::convert (Square a) { width = a.side; height = a.side; } int main () { Rectangle rect; Square sqr (4); rect.convert(sqr); cout << rect.area(); } Try removing the key word friend! • Friend of a friend is not automatically a friend • Friend is not automatically reversible 16 http://www.cplusplus.com/doc/tutorial/inheritance/

  3. OBJECT ORIENTATION IN C++ http://www.cplusplus.com/doc/tutorial/inheritance/

  4. INHERITANCE BETWEEN CLASSES #include <iostream> using namespace std; class Polygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b;} }; class Rectangle :public Polygon{// inherits members from Polygon class public: int area () { return width*height; } }; http://www.cplusplus.com/doc/tutorial/inheritance/

  5. INHERITANCE BETWEEN CLASSES:Like re-writing the base class’ members inside derived class #include <iostream> using namespace std; class Polygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b;} }; class Rectangle :public Polygon{ // inherits members from Polygon class protected: int width, height; public: void set_values (int a, int b) { width=a; height=b;} public: int area () { return width*height; } }; http://www.cplusplus.com/doc/tutorial/inheritance/

  6. Inheritance: Derived class inherits all protectedand public members of the Base class Derived class Base class http://www.cplusplus.com/doc/tutorial/polymorphism/

  7. Inheritance: Derived class inherits all protected and public members of the Base class Derived class Base class private: protected: public: http://www.cplusplus.com/doc/tutorial/polymorphism/

  8. class Polygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b;} }; class Rectangle:public Polygon{ // inherits members from Polygon class protected: int width, height; public: void set_values (int a, int b) { width=a; height=b;} public: int area () { return width*height; } }; class Triangle:public Polygon{ // inherits members from Polygon class protected: int width, height; public: void set_values (int a, int b) { width=a; height=b;} public: int area () { return (width*height)/2; } }; INHERITANCE BETWEEN CLASSES:Like re-writing the inherited classes members inside derived ones http://www.cplusplus.com/doc/tutorial/inheritance/

  9. INHERITANCE BETWEEN CLASSES #include <iostream> using namespace std; // derived classes class Polygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b;} }; class Rectangle:public Polygon{ // inherits members from Polygon class public: int area () { return width*height; } }; class Triangle:publicPolygon{ public: int area () { return (width*height)/2; } }; int main () { Rectangle rect; Triangle trgl; rect.set_values (4, 5); trgl.set_values (4, 5); cout << rect.area() << '\n'; cout << trgl.area() << '\n'; } 20 10 http://www.cplusplus.com/doc/tutorial/inheritance/

  10. ACCESSIBILITY WITH INHERITANCE #include <iostream> using namespace std; // derived classes class Polygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b;} }; class Rectangle:public Polygon { // public: inherits protected & public as is public: int area () { return width * height; } }; class Triangle:public Polygon { public: int area () { return width * height / 2; } }; int main () { Rectangle rect; Triangle trgl; rect.set_values (4, 5); trgl.set_values (4, 5); cout << rect.area() << '\n'; cout << trgl.area() << '\n'; } http://www.cplusplus.com/doc/tutorial/inheritance/

  11. TYPES OF INHERITANCE

  12. class A { public: aa, ab // local members protected: ac, ad // local members private: ae, af // local members } Inheritance Qualifier: public: class B: public A { public: ba, bb, // local members aa, ab // inherited protected: bc, bd, // local members ac, ad // inherited private: be, bf // local members }

  13. class A { public: aa, ab // local members protected: ac, ad // local members private: ae, af // local members } Inheritance Qualifier: public: protected: class C: protected A { public: ca, cb // local members protected: cc, cd, // local members aa, ab, ac, ad // inherited private: ce, cf // local members } class B: public A { public: ba, bb, // local members aa, ab // inherited protected: bc, bd, // local members ac, ad // inherited private: be, bf // local members }

  14. Inheritance Qualifier: class C: protected A { public: ca, cb // local members protected: cc, cd, // local members aa, ab, ac, ad // inherited private: ce, cf // local members } private: class D: private C { public: da, db // local member protected: dc, dd // local members private: de, df, // local members ca, cb, cc, cd, aa, ab, ac, ad // inherited }

  15. class A { public: aa, ab // local members protected: ac, ad // local members private: ae, af // local members } Inheritance Qualifier: public: protected: class C: protected A { public: ca, cb // local members protected: cc, cd, // local members aa, ab, ac, ad // inherited private: ce, cf // local members } class B: public A { public: ba, bb, // local members aa, ab // inherited protected: bc, bd, // local members ac, ad // inherited private: be, bf // local members } private: class D: private C { public: da, db // local member protected: dc, dd // local members private: de, df, // local members ca, cb, cc, cd, aa, ab, ac, ad // inherited } public: class E: public D { public: ea, eb, // local members da, db // inherited protected: ec, ed, // local members dc, dd // inherited private: ee, ef // local members }

  16. Construction of inherited object constructs base object #include <iostream> using namespace std; // constructors and derived classes class Mother { public: Mother () { cout << "Mother: no parameters\n"; } Mother (int a) { cout << "Mother: int parameter\n"; } }; class Daughter : public Mother { public: Daughter (int a) { cout << "Daughter: int parameter\n\n"; } }; class Son : public Mother { public: Son (int a) : Mother (a) { cout << "Son: int parameter\n\n"; } }; int main () { Daughter kelly(0); Son bud(0); } Mother: no parameters //daughter creates a mother object first, by default constructor Daughter: int parameter Mother: int parameter Son: int parameter http://www.cplusplus.com/doc/tutorial/inheritance/

  17. What is NOT inherited from the base class? • Base class’ constructors and its destructor • Base class’ assignment operator members (operator=) • Base class’ friends • Base class’ private members http://www.cplusplus.com/doc/tutorial/inheritance/

  18. MULTIPLE INHERITANCE #include <iostream> using namespace std; class Polygon { protected: int width, height; public: Polygon (int a, int b) : width(a), height(b) {} }; class Output { public: static void print (int i); }; void Output::print (int i) { cout << i << '\n'; } class Rectangle: public Polygon, public Output { //members of both classes are accessible public: Rectangle (int a, int b) : Polygon(a,b) {} int area () { return width*height; } }; class Triangle: public Polygon, public Output { public: Triangle (int a, int b) : Polygon(a,b) {} int area () { return width*height/2; } }; int main () { Rectangle rect (4,5); Triangle trgl (4,5); rect.print (rect.area()); Triangle::print (trgl.area()); } 20 10 Avoid multiple inheritance, even though C++ allows it!

  19. INHERITANCE AND POINTERS #include <iostream> using namespace std; class Polygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b; } }; class Rectangle: public Polygon { public: int area() { return width*height; } }; int main () { Rectangle rect; Polygon * ppoly1 = &rect; ppoly1->set_values (4,5); // cout << ppoly1->area() << '\n'; cout << rect.area() << '\n'; return 0; } ppoly1->set_values (4,5); rect.set_values (4,5); are same, but ppoly1->area(); rect.area(); are not Because ppoly1 is a pointer as Polygon and not a pointer as Rectangle Play with using ppoly1->area() vs. rect.area() 20 http://www.cplusplus.com/doc/tutorial/polymorphism/

  20. Inherited pointer may not point to base #include <iostream> using namespace std; class Polygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b; } }; class Rectangle: public Polygon { public: int area() { return width*height; } }; int main () { Polygon ppoly1; Rectangle * rect = &ppoly1; // This does not work, inherited pointer may not point to base } http://www.cplusplus.com/doc/tutorial/polymorphism/

  21. DUAL IDENTITIES OF AN OBJECT: INHERITANCE AND POINTERS #include <iostream> using namespace std; class Polygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b; } }; class Rectangle: public Polygon { public: int area() { return width*height; } }; class Triangle: public Polygon { public: int area() { return width*height/2; } }; int main () { Rectangle rect; Triangle trgl; Polygon * ppoly1 = &rect; Polygon * ppoly2 = &trgl; ppoly1->set_values (4,5); ppoly2->set_values (4,5); cout << rect.area() << '\n'; cout << trgl.area() << '\n'; } 20 10 http://www.cplusplus.com/doc/tutorial/polymorphism/

  22. DUAL IDENTITIES: SAME OBJECT MULTIPLE TYPES Remember Union? Derived object Base pointer http://www.cplusplus.com/doc/tutorial/polymorphism/

  23. #include <iostream> using namespace std; class Polygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b; } virtual int area () { return 0; } // means it will be defined in derived class }; class Rectangle: public Polygon { public: int area () { return width * height; } }; class Triangle: public Polygon { public: int area () { return (width * height / 2); } }; POLYMORPHISM VIRTUAL MEMBER: allows resolving object’s closest type Try to remove the key word virtual : all objects’ areas will be 0 int main () { Rectangle rect; Triangle trgl; Polygon poly; Polygon * ppoly1 = &rect; Polygon * ppoly2 = &trgl; Polygon * ppoly3 = &poly; ppoly1->set_values (4,5); ppoly2->set_values (4,5); ppoly3->set_values (4,5); cout << ppoly1->area() << '\n'; cout << ppoly2->area() << '\n'; cout << ppoly3->area() << '\n'; return 0; } 20 10 0 http://www.cplusplus.com/doc/tutorial/polymorphism/

  24. #include <iostream> using namespace std; class Polygon { protected: int width, height; public: Polygon (int a, int b) : width(a), height(b) {} virtual int area() { return 5;} }; class Rectangle { public: Rectangle() {} int area() { return 10; } }; class Triangle: public Polygon, public Rectangle { public: Triangle(int a,int b) : Polygon(a,b) {} int area() { return width*height/2; } }; Polymorphism and Multiple inheritance int main () { Polygon * ppoly= new Triangle (4,10); cout << ppoly->area() << '\n'; delete ppoly; } 20 Play with this code!

  25. #include <iostream> using namespace std; class Polygon { // abstract class: there may be noobject of this type protected: int width, height; public: void set_values (int a, int b) { width=a; height=b; } virtual int area (void) =0; // =0 makes it a pure virtual member }; class Rectangle: public Polygon { public: int area (void) { return (width * height); } }; class Triangle: public Polygon { public: int area (void) { return (width * height / 2); } }; ABSTRACT BASE CLASS http://www.cplusplus.com/doc/tutorial/polymorphism/

  26. #include <iostream> using namespace std; class Polygon { // abstract class: there may be no object of this type, but pointer is ok protected: int width, height; public: void set_values (int a, int b) { width=a; height=b; } virtual int area (void) =0; // =0 makes it pure virtual member }; class Rectangle: public Polygon { public: int area (void) { return (width * height); } }; class Triangle: public Polygon { public: int area (void) { return (width * height / 2); } }; ABSTRACT BASE CLASS Try declaring non-pointer object of Polygon type Try remove the key word virtual Try using =1 on virtual function int main () { Rectangle rect; Triangle trgl; Polygon * ppoly1 = &rect; Polygon * ppoly2 = &trgl; ppoly1->set_values (4,5); ppoly2->set_values (4,5); cout << ppoly1->area() << '\n'; cout << ppoly2->area() << '\n'; return 0; } 20 10 http://www.cplusplus.com/doc/tutorial/polymorphism/

  27. #include <iostream> using namespace std; class Polygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b; } virtual int area() =0; // pure virtual member, not supposed to have any object, but void printarea() { cout << this->area() << '\n'; } // note: accesses the object }; class Rectangle: public Polygon { public: int area (void) { return (width * height); } }; class Triangle: public Polygon { public: int area (void) { return (width * height / 2); } }; ABSTRACT BASE CLASS: object’s pure virtual member may be accessed using “this->” Try declaring non-pointer object of Polygon type Try removing the key word virtual int main () { Rectangle rect; Triangle trgl; Polygon * ppoly1 = &rect; Polygon * ppoly2 = &trgl; ppoly1->set_values (4,5); ppoly2->set_values (4,5); ppoly1->printarea(); ppoly2->printarea(); return 0; } 20 10 http://www.cplusplus.com/doc/tutorial/polymorphism/

  28. #include <iostream> using namespace std; class Polygon { protected: int width, height; public: Polygon (int a, int b) : width(a), height(b) {} virtual int area (void) =0; void printarea() { cout << this->area() << '\n'; } }; class Rectangle: public Polygon { public: Rectangle(int a, int b) : Polygon(a,b) {} int area() { return width*height; } }; class Triangle: public Polygon { public: Triangle(int a, int b) : Polygon(a,b) {} int area() { return width*height/2; } }; POLYMORPHISM WITH DYNAMIC ALLOCATION int main () { Polygon * ppoly1 = newRectangle (4,5); Polygon * ppoly2 = new Triangle (4,5); ppoly1->printarea(); ppoly2->printarea(); delete ppoly1; delete ppoly2; } 20 10 http://www.cplusplus.com/doc/tutorial/polymorphism/

  29. Need for a pure virtual function or an abstract base class? * A Dog (base class) is always of some type (inherited class), e.g., Collies, Beagles, etc. * An object for Dog class of undefined type may not be created, does not make sense, or Dog should be an abstract base class * A dog always barks() , each type overrides this method with actual barks() audio-clip * barks() is a pure virtual method https://stackoverflow.com/questions/14189438/why-do-we-need-abstract-classes-in-c

  30. Function Overriding may occur when one class is inherited from another class. Overloaded functions must differ in either number of parameters or type of parameters should be different.In Overridden function parameters must be same.

  31. #include <iostream>class base{ public: //this needs to be virtual to be overridden in derived class virtual void show(){std::cout<<"I am base";} //this is overloaded function of the previous one void show(int x){std::cout<<"\nI am overloaded";} };class derived:public base{ public: //the base version of this function is being overriddenvoid show(){std::cout<<"I am derived (overridden)";}};int main(){ base* b; derived d; b=&d; b->show(); //this will call the derived overriden version b->show(6); // this will call the base overloaded function} I am derived (overridden) I am overloaded https://stackoverflow.com/questions/11912022/differentiate-between-function-overloading-and-function-overriding

  32. Sample codes next

  33. // abstract base class, a strange code that works #include <iostream> using namespace std; class Polygon { protected: int width, height; public: //Polygon() {} Polygon(int a, int b) { width=a; height=b; cout << "here\n";} virtual int area (void) =0; }; class Rectangle: public Polygon { public: Rectangle(int a, int b) : Polygon (a, b) {} int area (void) { return (width * height); } }; int main () { Rectangle rect (4, 5); Polygon * ppoly1 = &rect; // cout << rect.area() << '\n'; cout << ppoly1->area() << '\n'; } Output: here 20 Play with this!

  34. #include <iostream> struct Car { public: Car() : price(20000) {} Car(double b) : price(b*1.1) {} double price; }; struct Toyota : public virtual Car { Toyota(double b) : Car(b) {} }; struct Prius : public Toyota { Prius(double b) : Toyota(b) {} }; int main() { Prius s(30000); std::cout << s.price << std::endl; Car p(30000); std::cout << p.price << std::endl; return 0; } 20000 33000

  35. #include <iostream> struct mybase { int x; template <int RANGE> virtual void print() // illegal { std::cout << RANGE + x + 1 << std::endl; } }; struct myderived : public mybase { template <int RANGE> void print() { std::cout << RANGE + x + 2 << std::endl; } }; int main(int argc, char** argv) { mybase* b = new myderived; b->x = 1; b->print<5>(); return 0; } #include <iostream> structmybase { int x; template <int RANGE> void print() { std::cout << RANGE + x + 1 << std::endl; } }; structmyderived : public mybase { template <int RANGE> void print() { std::cout << RANGE + x + 2 << std::endl; } }; int main(intargc, char** argv) { mybase* b = new myderived; b->x = 1; b->print<5>(); // prints 7 from mybase return 0; }

More Related