Presentation is loading. Please wait.

Presentation is loading. Please wait.

Friday, January 26, 2018 Announcements… For Today… For Next Time…

Similar presentations


Presentation on theme: "Friday, January 26, 2018 Announcements… For Today… For Next Time…"— Presentation transcript:

1 Friday, January 26, 2018 Announcements… For Today… For Next Time…
Inheritance Announcements… Lab 01: Grades due Today Zipped file of ALL .cpp and .h files NO LATE WORK ACCEPTED! Quiz Retries Retry as many times as you like, but only 1 submission. Questions? For Today… 3.1-2, pgs For Next Time… 3.3-4, pgs

2 Attendance Quiz #7 Inheritance

3 Retrieves full path of command line arguments
Tip #8: Directory Path Inheritance #include <iostream> #include <fstream> #include <stdlib.h> using namespace std; int main(int argc, char* argv[]) { char full[_MAX_PATH]; _fullpath(full, ".\\", _MAX_PATH); cout << "Place input/output files in the following directory:"; cout<< endl << full << endl; } ifstream in(argv[1]); // try to open argv[1] if (in) cout << "Successfully found \"" << argv[1] << " \""; in.close(); else cout << "Unable to find file \"" << argv[1] << " \""; return 0; Retrieves full path of command line arguments Place input/output files here: C:\Users\proper\Dropbox\BYU\CS 235\Visual Studio Examples\PrintFullPath\ Successfully found "fullPath.cpp "

4 Follow up… Software Design class MyClass { private: string name; public: MyClass(const string name) { this->name = name; } ~MyClass() = default; friend std::ostream& operator<< (ostream& lhs, const MyClass& rhs) lhs << "MyClass(" << name << ")"; return lhs; } }; class MyClass { private: string name; public: MyClass(const string name) { this->name = name; } ~MyClass() = default; string toString() const ostringstream out; out << "MyClass(" << name << ")"; return out.str(); } }; std::ostream& operator<< (ostream& lhs, const MyClass& rhs) lhs << rhs.toString(); return lhs; A friend function of a class has the same access privileges to private and protected data as the class member functions. A friend declaration is not needed if using the public toString member function.

5 3.1 Introduction to Inheritance and Class Hierarchies
Is-a Versus Has-a Relationships A Base Class and a Derived Class Initializing Data Fields in a Derived Class The No-Parameter Constructor Protected Visibility for Base-Class Data Fields 3.1, pgs

6 Inheritance and Class Hierarchies
Object-Oriented Programming (OOP) enables programmers to reuse previously written code saved as classes' Code reuse reduces the time required to code new applications'. Previously written tested and debugged code enables new applications to be more reliable. In OOP, a programmer can create a similar class by extending an existing class, rather than by writing an entirely new class. The new class (called the derived class or subclass) can have additional data fields and member functions. The derived class inherits the data fields and member functions of the original class (called the base class or superclass).multiple parents. A class hierarchy represents a set of hierarchically organized concepts. Base classes act typically as interfaces. Implementation inheritance Interface inheritance. If a base class is used as an interface, make it a pure abstract class.

7 Is-a Versus Has-a Relationships
Inheritance The is-a relationship between classes means that every instance of one class is also an instance of the other class (but not the other way around). A jet airplane is an airplane, but not all airplanes are jet airplanes. The jet airplane class is derived from an airplane class. The is-a relationship is represented in object oriented programming by extending a class. The has-a relationship between classes means that every instance of one class is or may be associated with one or more instances of the other . For example, a jet plane has-a jet engine. The has-a relationship is represented by declaring in one class a data field whose type is another class.

8 Is-a Versus Has-a Relationships
Inheritance We can combine is-a and has-a relationships A jet plane is an airplane, and it has a jet engine The jet airplane inherits all the properties of an airplane. An airplane has a tail, so a jet plane does too because it is an airplane. C++ allows you to capture both the inheritance (is-a) relationship and the has-a relationship: class Jet_Plane : public Airplane { private: int num_engines; Jet_Engine jets[4]; // Jet planes may have 4 engines // ... }; The part of the class heading following the colon specifies that Jet_Plane is a derived class of Airplane The Jet_Engine data field stores information for up to 4 jet engines for a Jet_Plane object.

9 A Base Class and a Derived Class
C++ Primer A computer has a: manufacturer processor RAM disk A laptop computer is a kind of computer, so it has all the properties of a computer plus some additional features: screen size weight Computer string manufacturer string processor int ramSize int diskSize int getRamSize() const int getDiskSize() const toString() const LapTop int screenSize int weight toString() const We can define class LapTop as a derived class of class Computer. "A LapTop is-a Computer"

10 Class Computer Computer.cpp
Inheritance Computer.h Computer.cpp #ifndef COMPUTER_H_ #define COMPUTER_H_ #include <string> class Computer { private: std::string manufacturer; std::string processor; int ramSize; int diskSize; public: Computer(const std::string& man, const std::string& proc, int ram, int disk) : manufacturer(man), processor(proc), ramSize(ram), diskSize(disk) {} int getRamSize() const { return ramSize; } int getDiskSize() const { return diskSize; } std::string toString() const; }; #endif #include "computer.h" #include <sstream> using std::ostringstream; using std::string; using std::endl; string Computer::toString() const { ostringstream sb; sb << "Manufacturer: " << manufacturer << endl << "CPU: " << processor << endl << "RAM: " << ramSize << " Mbs" << endl << "Disk: " << diskSize << " Gbs"; return sb.str(); } There are 4 private data elements common to all computers. Small in-line member functions are included in the .h file. Larger class implementations are often found in a corresponding .cpp file.

11 Class LapTop Inheritance LapTop.h #ifndef LAP_TOP_H #define LAP_TOP_H #include <string> #include <sstream> #include "computer.h" using std::string; using std::ostringstream; class LapTop : public Computer { private: int screenSize; double weight; public: LapTop(const string& m, const string& p, int r, int d, int s, double w) : Computer(m, p, r, d), screenSize(s), weight(w) {} string toString() const; }; #endif Class LapTop is derived from class Computer and publicly inherits Computer's data members and member functions. The constructor for class Lap_Top must begin by initializing the four data fields inherited from class Computer. Because those data fields are private to the base class, C++ requires that they be initialized by a base class constructor. In the definition of a constructor of a class, member initializer list specifies the initializers for direct and virtual base subobjects and non-static data members.

12 Protected Data Fields LapTop.h
Inheritance Computer.h LapTop.h class Computer { private: std::string manufacturer; std::string processor; int ramSize; int diskSize; public: // ... }; class LapTop : public Computer { private: // ... public: void setManufacturer(std::string m) manufacturer = m; } }; C++ provides a less restrictive form of visibility called protected visibility to allow a derived class to directly access data fields declared in its base class Illegal!! The data fields inherited from class Computer have private visibility—they can be accessed only within class Computer. class Computer { protected: std::string manufacturer; private: std::string processor; int ramSize; int diskSize; public: // ... }; class LapTop : public Computer void setManufacturer(std::string m) manufacturer = m; }

13 Public, Protected, Private Inheritance
C++ Primer class A { public: int x; protected: int y; private: int z; }; class B : public A { // x is public // y is protected // z is not accessible from B }; class C : protected A // x is protected // z is not accessible from C class D : private A // x is private // y is private // z is not accessible from D Classes B, C and D all contain the variables x, y and z. It is just question of access. 'private' is default for classes

14 The No-Parameter Constructor
Inheritance If the execution of any constructor in a derived class does not invoke a base class constructor, C++ automatically invokes the no-parameter constructor for the base class. C++ does this to initialize the part of the object inherited from the base class before the derived class starts to initialize its part of the object.

15 3.2 Member Function Overriding, Member Function Overloading, and
Polymorphism Member Function Overriding Member Function Overloading Virtual Functions and Polymorphism 3.2, pgs

16 Member Function Overriding
Inheritance #include <iostream> #include <string> #include "lapTop.h" using namespace std; int main(int argc, char* argv[]) { Computer my_computer("HP", "I5", 16, 500); LapTop your_computer("Dell", "I7", 32, 1000, 15, 5); cout << endl << "My computer:" << endl << my_computer.toString() << endl; cout << endl << "Your computer:" << endl << your_computer.toString() << endl; return 0; } My computer: Manufacturer: HP CPU: I5 RAM: 16 Mbs Disk: 500 Gbs Your computer: Manufacturer: Dell CPU: I7 RAM: 32 Mbs Disk: 1000 Gbs Even though your_computer is of type LapTop, the LapTop fields are not displayed; the call to toString() calls the toString() method inherited from Computer

17 Member Function Overriding
Inheritance LapTop.h LapTop.cpp #ifndef LAP_TOP_H #define LAP_TOP_H #include <string> #include <sstream> #include "computer.h" using std::string; using std::ostringstream; class LapTop : public Computer { private: int screenSize; double weight; public: LapTop(const string& m, const string& p, int r, int d, int s, double w) : Computer(m, p, r, d), screenSize(s), weight(w) {} string toString() const; }; #endif #include "lapTop.h" #include <sstream> using std::ostringstream; using std::string; using std::endl; string LapTop::toString() const { ostringstream sb; sb << Computer::toString() << endl << "Screen: " << screenSize << " inches" << endl << "Weight: " << weight << " lbs"; return sb.str(); } If class LapTop has its own toString member function, it will override the inherited member function and will be invoked by the member function call to toString(). We still call Computer's toString member function to output its contents.

18 Member Function Overriding
Inheritance #include <iostream> #include <string> #include "lapTop.h" using namespace std; int main(int argc, char* argv[]) { Computer my_computer("HP", "I5", 16, 500); LapTop your_computer("Dell", "I7", 32, 1000, 15, 5); cout << endl << "My computer:" << endl << my_computer.toString() << endl; cout << endl << "Your computer:" << endl << your_computer.toString() << endl; return 0; } My computer: Manufacturer: HP CPU: I5 RAM: 16 Mbs Disk: 500 Gbs Your computer: Manufacturer: Dell CPU: I7 RAM: 32 Mbs Disk: 1000 Gbs Screen: 15 inches Weight: 5 lbs Now the state of a laptop computer, complete with screen size and weight, is output when calling toString member function for class LapTop which overrides the inherited member function of computer.

19 Virtual Functions and Polymorphism
Inheritance #include <iostream> #include <vector> #include <string> #include "lapTop.h" using namespace std; int main(int argc, char* argv[]) { std::vector<Computer*> computers; computers.push_back(new Computer("Acer", "I3", 8, 256)); computers.push_back(new LapTop("HP", "I5", 16, 500, 13, 6.5)); for (unsigned int i = 0; i < computers.size(); i++) cout << endl << computers[i]->toString() << endl; return 0; } Manufacturer: Acer CPU: I3 RAM: 8 Mbs Disk: 256 Gbs Manufacturer: HP CPU: I5 RAM: 16 Mbs Disk: 500 Gbs In C++, a pointer variable of a baseclass type (general) can point to an object of a derived-class type (specific): Lap_Top objects are Computer objects with more features But, where are the additional data members??

20 Virtual Functions and Polymorphism
Inheritance #include <iostream> #include <vector> #include <string> #include "lapTop.h" using namespace std; int main(int argc, char* argv[]) { std::vector<Computer*> computers; computers.push_back(new Computer("Acer", "I3", 8, 256)); computers.push_back(new LapTop("HP", "I5", 16, 500, 13, 6.5)); for (unsigned int i = 0; i < computers.size(); i++) cout << endl << computers[i]->toString() << endl; return 0; } class Computer { // ... public: virtual std::string toString() const; }; Manufacturer: Acer CPU: I3 RAM: 8 Mbs Disk: 256 Gbs Manufacturer: HP CPU: I5 RAM: 16 Mbs Disk: 500 Gbs Screen: 13 inches Weight: 6.5 lbs By changing the declaration of the function toString in the class Computer (in Computer.h) to a virtual function, when it is called through a pointer (or reference) variable the actual member function will be determined at run time and based on the type of the object pointed to (or referenced).

21 Virtual Functions and Polymorphism
Inheritance The feature we just illustrated is an important concept in OOP called polymorphism. Polymorphism is the quality of having many forms or many shapes Polymorphism enables the program to determine which member function to invoke at run time In our example, at compile time, the C++ compiler cannot determine what type of object the_computer will point to (type Computer or its derived type LapTop) At run time the program knows the type of the object that receives the to_string message and calls the appropriate to_string function To use polymorphsim you have to use pointers and virtual functions, so to solve your problem you have to do these things: Use pointers to your objects. Declare the toString() function in base class virtual.

22 Virtual Functions and Polymorphism
Inheritance

23 Virtual Functions and Polymorphism
Inheritance

24 Virtual Functions and Polymorphism
Inheritance

25


Download ppt "Friday, January 26, 2018 Announcements… For Today… For Next Time…"

Similar presentations


Ads by Google