Defining Class Functions

1 / 22

# Defining Class Functions

## Defining Class Functions

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