C++ STREAMS; INHERITANCE AS PUBLIC, PROTECTED, AND PRIVATE; AGGREGATION/COMPOSITION Pages 731 to 742 Anna Rakitianskaia, University of Pretoria
C++ STREAM CLASSES A stream is an abstraction that represents a device on which input and output operations are performed To output a variable to the screen, the cout stream object is used: cout << x << endl; To get input from keyboard, the cin stream object is used: cin >> x; To use cin and cout, <iostream> header has to be included, containing definitions of istream and ostream classes Similarly, ifstream and ofstream classes are used for file input/output, and are defined in the <fstream> header
C++ STREAM CLASSES: INHERITANCE IN PRACTICE istream is the parent of ifstream ostream is the parent of ofstream ios is the parent of istream and ostream
PROTECTED MEMBERS OF A CLASS public members are accessible outside of the class, and private members are not accessible outside of the class private members of the parent class are not directly accessible from the child class What if we want to keep certain parent class members inaccessible from the outside, but allow child class to access them directly? Use the protected access modifier! protected: double length; double width;
PROTECTED MEMBERS OF A CLASS Parent class access modifier Access from the outside Access from a child class public Accessible Accessible private Not accessible Not accessible protected Not accessible Accessible Protected members: public for child classes private for the rest of the world Inheritance can also be public, protected, and private (next slide)
INHERITANCE AS PUBLIC, PROTECTED, OR PRIVATE // Inherit from parent publicly class pubchild: public parentclass ; // Inherit from parent privately class prichild: private parentclass ; // Inherit from parent protectedly class prochild: protected parentclass ; // Default: private inheritance class defchild: parentclass ;
INHERITANCE AS PUBLIC, PROTECTED, OR PRIVATE Parent Access Modifier Public Inheritance Protected Inheritance Private Inheritance parent parent: public child parent: protected child parent: private child public: x public: x protected: x private: x protected: x protected: x protected: x private: x private: x Inaccessible Inaccessible Inaccessible
PUBLIC INHERITANCE class parent public: int x; protected: int y; private: int z; ; class child: public parent child() // inherit publicly x = 1; // ALLOWED: x is public y = 2; // ALLOWED: y is protected z = 3; // NO! COMPILE ERROR! z is private! ; int main() child obj; obj.x = 1; // ALLOWED: anybody can access public members obj.y = 2; // NOT OK: can not access protected members from outside obj.z = 3; // NOT OK: can not access private members from outside
PROTECTED INHERITANCE class parent public: int x; protected: int y; private: int z; ; class child: protected parent child() // inherit protectedly x = 1; // ALLOWED: x is protected y = 2; // ALLOWED: y is protected z = 3; // NO! COMPILE ERROR! z is private! ; int main() child obj; obj.x = 1; // NOT OK: can not access protected members from outside obj.y = 2; // NOT OK: can not access protected members from outside obj.z = 3; // NOT OK: can not access private members from outside
PRIVATE INHERITANCE class parent public: int x; protected: int y; private: int z; ; class child: private parent child() // inherit privately x = 1; // NO! COMPILE ERROR! x is private! y = 2; // NO! COMPILE ERROR! y is private! z = 3; // NO! COMPILE ERROR! z is private! ; int main() child obj; obj.x = 1; // NOT OK: can not access private members from outside obj.y = 2; // NOT OK: can not access private members from outside obj.z = 3; // NOT OK: can not access private members from outside
INHERITANCE VS COMPOSITION Inheritance: is-a relationship child class is derived from parent (base) class student is a person pigeon is a bird Aggregation (composition): has-a relationship one class contains an instance of another class person has a date of birth pigeon has a wing (two of them, in fact) Student Person Date of Birth
COMPOSITION/AGGREGATION Why is composition useful? Decouple for reusability Pigeon is not the only thing with wings Our models become more modular
CONSTRUCTOR INITIALIZATION LISTS What you re used to (explicit assignment): persontype::persontype(string first, string last) firstname = first; lastname = last; Another way to do it (implicit assignment): persontype::persontype(string first, string last) : firstname(first), lastname(last)
COMPOSITION: INITIALIZING INNER OBJECTS Many games and simulations have creatures or objects that move around a board, map, or screen The one thing that all of these creatures/objects have in common is a location (point in 2D space) Composition: creature has a location Creature -name: string -location: Point2D -Creature() +Creature(string n, int x, int y) +Creature(string n, Point2D p) +moveto(int x, int y): void +print() const: void -coordx: int -coordy: int Point2D +Point2D() +Point2D(int x, int y) +setpoint(int x, int y): void +getx() const: int +gety() const: int
COMPOSITION: POINT2D HEADER #ifndef POINT2D_H #define POINT2D_H class Point2D private: int coordx; int coordy; public: Point2D(int x = 0, int y = 0); void setpoint(int x, int y); int getx() const; int gety() const; ; #endif
COMPOSITION: POINT2D IMPLEMENTATION #include Point2D.h Point2D::Point2D(int x, int y) : coordx(x), coordy(y) // Constructor with default parameters // Notice the constructor initialisation list! void Point2D::setPoint(int x, int y) coordx = x; coordy = y; int Point2D::getX() const return coordx; int Point2D::getY() const return coordy;
COMPOSITION: CREATURE HEADER #ifndef CREATURE_H #define CREATURE_H #include <string> #include <iostream> #include "Point2D.h" // Include Point2D for composition class Creature private: string name; Point2D location; Creature(); public: ; #endif Creature(string n, int x, int y); void moveto(int x, int y); void print() const; // Composition: a Point2D object is used!
COMPOSITION: CREATURE IMPLEMENTATION #include Creature.h Creature::Creature() // Empty default constructor Creature::Creature(string n, int x, int y) : name(n), location(x, y) // Constructor with // initialisation list void Creature::moveTo(int x, int y) location.setpoint(x, y); void Creature::print() const cout << name << " is at " << location.getx() Passing parameters to the variable invokes Point2D constructor << "," << location.gety() << " location" << endl;
COMPOSITION: MAIN FUNCTION #include Creature.h using namespace std; int main() string cname; // declare input variables int x = 0, y = 0; cout << "Enter a name for your creature: "; cin >> cname; // get input from user cout << " Enter X position: "; cin >> x; cout << " Enter Y position: "; cin >> y; Creature beast(cname, x, y); // create the beast beast.print(); // print to screen return 0; // exit! Bobby is at 3,4 location
QUESTION TIME Next lecture: Object-Oriented Design Object-Oriented Programming A thorough example