defining class functions
play

Defining Class Functions http://cs.mst.edu Definition and Use - PowerPoint PPT Presentation

Defining Class Functions http://cs.mst.edu Definition and Use //fraction.h #include fraction.h #ifndef FRACTION_H #define FRACTION_H int main() { class Fraction Fraction f, g; { f.m_Numerator = 7; public: f.readin(); void


  1. Defining Class Functions http://cs.mst.edu

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

  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: int m_Numerator; int m_Denominator; }; #endif http://cs.mst.edu

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

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

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

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

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

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

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

  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; ... http://cs.mst.edu

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

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

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

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

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

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

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

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

  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; ... http://cs.mst.edu

  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; ... http://cs.mst.edu

  22. End of Session http://cs.mst.edu

Recommend


More recommend