Classes— the full enchilada Chapter 20 Agenda Review Constructor functions Accessor functions Modifier functions input/output functions Summary Review--Modeling a Balloon in C++ class Balloon { public: void initialize(int initRad); void inflate(int howMuch); void pop( ); Member Functions void display(); private: int radius; }; Member Data Review--You can use the public functions defined for the class Balloon bal; bal.initialize(5); bal.inflate(15); bal.pop(); Review--Implement the initialize and inflate functions void Balloon::initialize(int initRad) { This says it is a radius = initRad; member function of Class Balloon } void Balloon::inflate(int howMuch) { radius = radius + howMuch; Notice how the } parameter modifies the member data A ‘client’ program is one that uses a class int main() { Balloon myBalloon; myBalloon.initialize(3); cout<<"myBalloon currently has Radius "; myBalloon.display(); cout<<"\nInflating myBalloon by 8 \n"; myBalloon.inflate(8); cout<<"Now myBalloon has Radius "; myBalloon.display(); cout<<"\nPopping myBalloon \n"; myBalloon.pop(); cout<<"myBalloon currently has Radius "; myBalloon.display(); } Agenda Review Constructor functions Accessor functions Modifier functions input/output functions Summary Fully defined Balloon class in C++ class Balloon { public: // constructor functions Balloon( ); // Balloon(int initRad); // accessor functions float volume( ) const; change radius int getRadius( ) const; default constructor // explicit constructor // const means function can’t // modifier functions void inflate(int howMuch); void pop( ); void setRadius(int r); …continued Fully defined Balloon class in C++ …continued // input/output functions void display(ostream &out); void input(istream &in); private: int radius; }; // overloaded operators ostream& operator<<( ostream& out, Balloon b); istream& operator>>( istream& in, Balloon &b); bool operator==( Balloon b1, Balloon b2);
© Copyright 2025 Paperzz