Presentation is loading. Please wait.

Presentation is loading. Please wait.

Andy Wang Object Oriented Programming in C++ COP 3330

Similar presentations


Presentation on theme: "Andy Wang Object Oriented Programming in C++ COP 3330"— Presentation transcript:

1 Andy Wang Object Oriented Programming in C++ COP 3330
Inheritance Andy Wang Object Oriented Programming in C++ COP 3330

2 Hierarchical Relationships
Many types of classes share similarities Similar private member data Similar member functions Inheritance Factoring out common features into a single base class Build derived classes that will share or inherit all of the data and functionality of the base class Except constructors, destructors, and assignment operators

3 Inheritance The base class/derived class relationship is an “is a” relationship Derived object is an instance of the base object Usually a subcategory Vs. “has a” relationship An object of one class is embedded inside another class

4 Examples Base class Derived class GeometricObject Sport BankAccount
Vehicle Car Circle, Square, Line Football, Baseball Savings, Checking Car, Train, Bus Honda, Toyota, Ford

5 Declaring Derived Class
Format class derivedClassName : public baseClassName public The base class must be declared somewhere The derived class has the same level as the base class Other protection levels can be ignored for now

6 Examples class Sport {…}; class Football : public Sport {…}; class Baseball : public Sport {…}; class BankAccount {…}; class Checking : public BankAccount {…}; class Vehicle {…}; class Car : public Vehicle {…}; class Honda : public Car {…};

7 Note Car inherits everything in the Vehicle class
Honda inherits everything in the Car class Thus, Honda inherits everything from the Vehicle class

8 Example: Drawing Program
nher/geombuild/geom1.h

9 geom1.h class GeometricObject { public: void Draw(); void Erase(); void Move(int h, int v); int top, left, bottom, right; };

10 geom1.h class Two_D_Object : public GeometricObject { public: int fillPattern; }; class Rectangle : public Two_D_Object { int length, width; }

11 Protection Levels Public Private Protected
Can be accessed by name from anywhere Private Can be accessed directly only by the class in which it is declared Protected Can be accessed by directly by the class in which is declared and by any classes derived from that class

12 Example: Drawing class GeometricObject { public: GeometricObject(); void Draw(); void Erase(); void Move(int h, int v); protected: // accessible by this and derived classes int top, left, bottom, right private: int x; // inaccessible from outside };

13 Example: Drawing class Two_D_Object : public GeometricObject { public: Two_D_Object(); private: int fill Pattern; };

14 Example: Drawing class Rectangle : public Two_D_Object { public: Rectangle(); Rectangle(int t, int l, int b, int r, int f, int len, int wid); void FindCenter(int &, int &); void Draw(); double Area(); int Perimeter(); private: int length, width; };

15 Example: Drawing class Circle : public Two_D_Object { public: Circle(); Circle(int t, int l, int b, int r, int f, int cx, int cy, int rad); void FindCenter(int &, int &); void Draw(); double Area(); double Circumference(); private: int center_x, center_y, radius; };

16 Constructors in Derived Classes
A derived object “is an” instance of the base class Thus, when a derived object is created, the constructors from the base and derived classes will run The base class will run first, then the derived

17 Examples Vehicle obj1; Car obj2; Honda obj3;
Only the Vehicle() constructor runs Car obj2; Vehicle constructor runs, followed by the Car() constructor Honda obj3; Vehicle(), Car(), Honda()

18 Note Destructors will be invoked in the reverse order
~Honda(), ~Car(), ~Vehicle()

19 Examples nher/sample.cpp

20 Inheritance #include <iostream> using namespace std; class A { public: A() { cout << “A()” << endl; } ~A() { cout << “~A” << endl; } };

21 Inheritance class B : public A { public: B() { cout << “B()” << endl; } ~B() { cout << “~B()” << endl; } }; class C : public B { C() { cout << “C()” << endl; } ~C() { cout << “~C()” << endl; }

22 Inheritance int main() { cout << “Declaring object of type A” << endl; A aobject; { cout << “Declaring object of type B” << endl; B bobject; cout << Declaring object of type C” << endl; C cobject;

23 Inheritance cout << “Object C now going out of scope” << endl; } cout << “Object B now going out of scope” << endl; cout << “Object A now going out of scope” << endl; return 0;

24 Constructors with Parameters
Without parameters, the default constructors are called With a derived class, we might want to declare Honda h(2, 3, 4, “green”, “Accord”); 2 and 3 might be for variables like vehicleCategory and idNumber in the Vehicle Class 4 and “green” might be for variables like numDoors and carColor in the Car Class “Accord” might be for a variable model in the Honda class

25 How to Distribute the Parameters
Use an initialization list function prototype : initialization list { function body } Use the initialization list to call the next higher constructor explicitly, in order to send the parameters to the parent constructor

26 Example Vehicle::Vehicle(int c, int id) { vehicleCategory = c; idNumber = id; } Car::Car(int c, int id, int nd, char *cc) : Vehicle(c, id) { numDoors = nd; strcpy(carColor, cc); Honda::Honda(int c, int id, int nd, char *cc, char *mod) : Car(c, id, nd, cc) { strcpy(model, mod);

27 Drawing Program Example
r1.html

28 geom.h class GeometriObject { public: GeometricObject(); GeometricObject(int t, int l, int b, int r); void Draw(); … protected: int top, left, bottom, right; };

29 geom.h class Two_D_Object : public GeometricObject { public: Two_D_Object(); Two_D_Object(int t, int l, int b, int r, int fill); … protected: int fillpattern; };

30 geom.h class Rectangle : public Two_D_Object { public: Rectangle(); Rectangle(int t, int l, int b, int r, int f, int len, int wid); … private: int length, width; };

31 geom.h class Circle : public Two_D_Object { public: Circle(); Circle(int , int l, int b, int r, int f, int cx, int cy, int rad); … private: int center_x, center_y, radius; };

32 geom.cpp #include <iostream> #include “geom.h” using namespace std; GeometricObject::GeometricObject() { cout << “Running GeometricObject default constructor\n”; top = left = bottom = right = 0; }

33 geom.cpp GeometricObject::GeometricObject(int t, int l, int b, int r) { cout << “Running GeometricObject constructor with parameters\n”; top = t; left = l; bottom = b; right = r; }

34 geom.cpp Two_D_Object::Two_D_Object() { cout << “Running Two_D_Object default constructor\n”; fillPattern = 0; } Two_D_Object::Two_D_Object(int t, int l, int b, int r, int fill) : GeometricObject(t, l, b, r) { cout << “Running Two_D_Object constructor with parameters\n”; fillPattern = fill;

35 geom.cpp Rectangle::Rectangle() { cout << “Running Rectangle default constructor\n”; length = width = 1; }

36 geom.cpp Rectangle::Rectangle(int , int l, int b, int r, int f, int len, int wid) : Two_D_Object(t, l, b, r, f) { cout << “Running Rectangle constructor with parameters\n”; length = len; width = wid; }

37 geom.cpp Circle::Circle() { radius = center_x = center_y = 1; } Circle::Circle(int t, int l, int b, int r, int f, int cx, int cy, int rad) : Two_D_Object(t, l, b, r, f) { center_x = cx; center_y = cy; radius = rad;

38 main.cpp #include <iostream> #include “geom.h” using namespace std; int main() { cout << “Rectangle r\n”; Rectangle r; cout << “Rectangle r1(1, 2, 3, 4, 5, 10, 20)\n”; Rectangle r1(1, 2, 3, 4, 5, 10, 20); return 0; }

39 Function Overriding Suppose we have the following base class
class Student { public: void GradeReport(); }; We also have the following derived classes class Grad : public Student class undergrad : public Student

40 Function Overriding Since Grad and Undergrad are derived from Student, they inherit everything from Student But suppose grade reports look different for undergrads and grads These classes need to have their own functions But using the exact same prototype

41 Function Overriding class Grad : public Student { public: void GradeReport(); … }; class Undergrad : public Student {

42 To Call the Parent Version
void Student::GradeReport() { // processing done by parent function } void Grade::GradeReport() { // explicit call to parent function Student::GradeReport(); // other processing specific to Grad’s version

43 Drawing Example nher/geom2/geom.h

44 geom.h class GeometricObject { public: … void Draw(); };

45 geom.h class Two_D_Object : public GeometricObject { public: … void Draw(); }

46 geom.h class Rectangle : public Two_D_Object { public: … void Draw(); };

47 geom.h class Circle: public Two_D_Object { public: … void Draw(); };

48 geom.cpp … void GeometricObject::Draw() { cout << “Running GeometricObject::Draw()\n” } void Two_D_Object::Draw() { cout << “Running Two_D_Object::Draw()\n”; // can call upon the base class version of Draw GeometricObject::Draw(); // and do any processing specific to this class cout << “Finishing Two_D_Object::Draw()\n”;

49 geom.cpp void Rectangle::Draw() { cout << “Running Rectangle::Draw()\n”; Two_D_Object::Draw(); // do pre-processing // draw rectangle cout << “Finishing Rectangle::Draw()\n”; } …

50 main.cpp #include <iostream> #include “geom.h” using namespace std; int main() { Rectangle r; … r.Draw(); return 0; }


Download ppt "Andy Wang Object Oriented Programming in C++ COP 3330"

Similar presentations


Ads by Google