defining class functions n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Defining Class Functions PowerPoint Presentation
Download Presentation
Defining Class Functions

Loading in 2 Seconds...

  share
play fullscreen
1 / 22
rhian

Defining Class Functions - PowerPoint PPT Presentation

95 Views
Download Presentation
Defining Class Functions
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. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

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

  1. Defining Class Functions

  2. Definition and Use //fraction.h #ifndef FRACTION_H #define FRACTION_H class Fraction { public: void readin(); void print(); Fraction reciprocal(); void unreduce(const int m); private: intm_Numerator; intm_Denominator; }; #endif #include “fraction.h” int main() { Fraction f, g; f.m_Numerator = 7; f.readin(); f.print(); f.unreduce(5); return 0; }

  3. Function Definitions //fraction.h #ifndef FRACTION_H #define FRACTION_H class Fraction { public: void readin(); void print(); Fraction reciprocal(); void unreduce(const int m); private: intm_Numerator; intm_Denominator; }; #endif

  4. Function Definitions //fraction.h #ifndef FRACTION_H #define FRACTION_H class Fraction { public: void readin(); void print(); Fraction reciprocal(); void unreduce(const int m); private: intm_Numerator; intm_Denominator; }; #endif //fraction.cpp #include “fraction.h” #include <iostream> using std::cout; using std::cin;

  5. Function Definitions //fraction.h #ifndef FRACTION_H #define FRACTION_H class Fraction { public: void readin(); void print(); Fraction reciprocal(); void unreduce(const int m); private: intm_Numerator; intm_Denominator; }; #endif //fraction.cpp #include “fraction.h” #include <iostream> using std::cout; using std::cin; void Fraction::readin() { }

  6. Function Definitions //fraction.h #ifndef FRACTION_H #define FRACTION_H class Fraction { public: void readin(); void print(); Fraction reciprocal(); void unreduce(const int m); private: intm_Numerator; intm_Denominator; }; #endif //fraction.cpp #include “fraction.h” #include <iostream> using std::cout; using std::cin; void Fraction::readin() { cout<<“enter numerator: ”; cin>>m_Numerator; cout<<“enter denominator: ”; cin>>m_Denominator; return; }

  7. Function Definitions //fraction.h #ifndef FRACTION_H #define FRACTION_H class Fraction { public: void readin(); void print(); Fraction reciprocal(); void unreduce(const int m); private: intm_Numerator; intm_Denominator; }; #endif //fraction.cpp continued...

  8. Function Definitions //fraction.h #ifndef FRACTION_H #define FRACTION_H class Fraction { public: void readin(); void print(); Fraction reciprocal(); void unreduce(const int m); private: intm_Numerator; intm_Denominator; }; #endif //fraction.cpp continued... • void Fraction::print() • { • cout<<“(”<<m_Numerator • <<“/”<<m_Denominator<<“)”; • return; • }

  9. Function Definitions //fraction.h #ifndef FRACTION_H #define FRACTION_H class Fraction { public: void readin(); void print(); Fraction reciprocal(); void unreduce(const int m); private: intm_Numerator; intm_Denominator; }; #endif //fraction.cpp continued... • void Fraction::print() • { • cout<<“(”<<m_Numerator • <<“/”<<m_Denominator<<“)”; • return; • } • Fraction Fraction::reciprocal() • { • Fraction returnable; • returnable.m_Numerator = m_Denominator; • returnable.m_Denominator = m_Numerator; •     return returnable; • }

  10. Function Definitions //fraction.h #ifndef FRACTION_H #define FRACTION_H class Fraction { public: void readin(); void print(); Fraction reciprocal(); void unreduce(const int m); private: intm_Numerator; intm_Denominator; }; #endif //fraction.cpp continued... • void Fraction::print() • { • cout<<“(”<<m_Numerator • <<“/”<<m_Denominator<<“)”; • return; • } • Fraction Fraction::reciprocal() • { • Fraction returnable; • returnable.m_Numerator = m_Denominator; • returnable.m_Denominator = m_Numerator; •     return returnable; • } • void Fraction::unreduce (const int m) • { • m_Numerator*=m; • m_Denominator*=m; •     return; • }

  11. Fraction in Use #include “fraction.h” int main() { Fraction f1, f2, f3; f1.readin(); f1.print(); f2.readin(); f2.print(); f2.unreduce(2); f2.print(); f3 = f1.reciprocal(); f3 = f1 + f2; ...

  12. Fraction in Use //fraction.h #ifndef FRACTION_H #define FRACTION_H class Fraction { public: void readin(); void print(); Fraction reciprocal(); void unreduce(const int m); private: intm_Numerator; intm_Denominator; }; #endif #include “fraction.h” int main() { Fraction f1, f2, f3; f1.readin(); f1.print(); f2.readin(); f2.print(); f2.unreduce(2); f2.print(); f3 = f1.reciprocal(); f3 = f1 + f2; ...

  13. Fraction in Use • void Fraction::readin() • { • cout<<“enter numerator: ”; • cin>>m_Numerator; • cout<<“enter denominator: ”; • cin>>m_Denominator; • return; • } #include “fraction.h” int main() { Fraction f1, f2, f3; f1.readin(); f1.print(); f2.readin(); f2.print(); f2.unreduce(2); f2.print(); f3 = f1.reciprocal(); f3 = f1 + f2; ...

  14. Fraction in Use • void Fraction::print() • { • cout<<“(”<<m_Numerator • <<“/”<<m_Denominator<<“)”; • return; • } #include “fraction.h” int main() { Fraction f1, f2, f3; f1.readin(); f1.print(); f2.readin(); f2.print(); f2.unreduce(2); f2.print(); f3 = f1.reciprocal(); f3 = f1 + f2; ...

  15. Fraction in Use • void Fraction::readin() • { • cout<<“enter numerator: ”; • cin>>m_Numerator; • cout<<“enter denominator: ”; • cin>>m_Denominator; • return; • } #include “fraction.h” int main() { Fraction f1, f2, f3; f1.readin(); f1.print(); f2.readin(); f2.print(); f2.unreduce(2); f2.print(); f3 = f1.reciprocal(); f3 = f1 + f2; ...

  16. Fraction in Use • void Fraction::print() • { • cout<<“(”<<m_Numerator • <<“/”<<m_Denominator<<“)”; • return; • } #include “fraction.h” int main() { Fraction f1, f2, f3; f1.readin(); f1.print(); f2.readin(); f2.print(); f2.unreduce(2); f2.print(); f3 = f1.reciprocal(); f3 = f1 + f2; ...

  17. Fraction in Use • void Fraction::unreduce (const int m) • { • m_Numerator*=m; • m_Denominator*=m; •     return; • } #include “fraction.h” int main() { Fraction f1, f2, f3; f1.readin(); f1.print(); f2.readin(); f2.print(); f2.unreduce(2); f2.print(); f3 = f1.reciprocal(); f3 = f1 + f2; ...

  18. Fraction in Use • void Fraction::print() • { • cout<<“(”<<m_Numerator • <<“/”<<m_Denominator<<“)”; • return; • } #include “fraction.h” int main() { Fraction f1, f2, f3; f1.readin(); f1.print(); f2.readin(); f2.print(); f2.unreduce(2); f2.print(); f3 = f1.reciprocal(); f3 = f1 + f2; ...

  19. Fraction in Use • Fraction Fraction::reciprocal() • { • Fraction returnable; • returnable.m_Numerator = • m_Denominator; • returnable.m_Denominator = • m_Numerator; •     return returnable; • } #include “fraction.h” int main() { Fraction f1, f2, f3; f1.readin(); f1.print(); f2.readin(); f2.print(); f2.unreduce(2); f2.print(); f3 = f1.reciprocal(); f3 = f1 + f2; ...

  20. Fraction in Use • ??? Fraction::operator+(???) • { • ??? • } #include “fraction.h” int main() { Fraction f1, f2, f3; f1.readin(); f1.print(); f2.readin(); f2.print(); f2.unreduce(2); f2.print(); f3 = f1.reciprocal(); f3 = f1 + f2; ...

  21. B-b-b-but! • ??? Fraction::operator=(???) • { • ??? • } #include “fraction.h” int main() { Fraction f1, f2, f3; f1.readin(); f1.print(); f2.readin(); f2.print(); f2.unreduce(2); f2.print(); f3 = f1.reciprocal(); f3 = f1 + f2; ...

  22. End of Session