• No results found

Inheritance, Composition CSCI 103L

N/A
N/A
Protected

Academic year: 2021

Share "Inheritance, Composition CSCI 103L"

Copied!
26
0
0

Loading.... (view fulltext now)

Full text

(1)

Inheritance, Composition

CSCI 103L

Jeffrey Miller, Ph.D.

LECTURE 16

HTTP://WWW-SCF.USC.EDU/~CSCI103B

USC CSCI 103L

(2)

Outline

USC CSCI 103L 2/26

▪ Inheritance

▪ Instantiating Objects with Inheritance

▪ Inheritance Example

▪ Program

(3)

Inheritance

▪ Inheritance is a form of software reuse in which you create a class that absorbs an existing class’s data and behaviors and enhances them with new

capabilities

› Functions and variables from a parent class that are not private will be inherited into the child class

▪ When creating a class, you can designate that the new class should inherit the members of an existing class

› The existing class is called the base class (or parent class)

› The new class is called the derived class (or child class)

USC CSCI 103L 3/26 Inheritance

(4)

Punnett Square

▪ In biology, we determine inheritance using a Punnett Square, looking at genotypes and alleles

› If one parent has the genotype BB and the other bb, what is the likelihood the child will have bb?

› If one parent has the genotype Bb and the other bb, what is the likelihood the child will have bb?

USC CSCI 103L 4/26 Inheritance

B B

b Bb Bb

b Bb Bb

B b

b Bb bb

b Bb bb

(5)

Single vs Multiple Inheritance

▪ Single inheritance allows a derived class to inherit from only one base class

› Java supports single inheritance

▪ Multiple inheritance allows a derived class to inherit from more than one base class

› C++ supports multiple inheritance

› There is a potential problem with multiple inheritance if more than one base class provides an implementation for the same function

USC CSCI 103L 5/26 Inheritance

(6)

Inheritance vs Composition

▪ is-a Relationship

› If an object has an “is-a” relationship with another object, inheritance will be used

› Vehicle, Car, Truck, Motorcycle

▪ has-a Relationship

› If an object has a “has-a” relationship with another object, composition will be used

› Car, Steering Wheel, Brake Pedal, Speedometer

USC CSCI 103L 6/26 Inheritance

(7)

Inheritance Hierarchy

▪ To show a child class and a parent class in a

diagram, we draw a line connecting the child class to the parent class where the parent class is above the child

USC CSCI 103L 7/26 Inheritance

Shape

2-D Shape 3-D Shape

Triangle Rectangle

Circle

Square Sphere

Cone Box

Cube

Ellipse Ellipsoid

(8)

Class Inheritance Types

▪ When inheriting from a class, you specify the type of inheritance you would like in addition to the name of the class from which to inherit

▪ The type of inheritance (public, protected, or private) will determine how you inherit variables and

functions from the parent class

class TwoDShape : public Shape

USC CSCI 103L 8/26 Inheritance

(9)

Class Inheritance Types (cont.)

▪ Public Inheritance

› With public inheritance, all of the base class members retain their original member access when they become members of the derived class

› This means private members are still private to the base class, so the derived class can NOT access them

▪ Protected Inheritance

› With protected inheritance, public and protected members of the base class become protected members of the derived class

› Private members are still private to the base class

▪ Private Inheritance

› With private inheritance, public and protected members of the base class become private members of the derived class

› Private members are still private to the base class

USC CSCI 103L 9/26 Inheritance

(10)

Outline

USC CSCI 103L 10/26

▪ Inheritance

▪ Instantiating Objects with Inheritance

▪ Inheritance Example

▪ Program

(11)

Parent and Child Instantiation

▪ When a child class is instantiated, the parent class must be instantiated first in the child class’s constructor

› This will happen automatically by the compiler calling the parent class’s default constructor unless we explicitly instantiate the parent

› Note that if there is no default constructor in the parent, we MUST explicitly call the parent class’s constructor from the child

▪ When we call the parent class’s constructor from the child, it must be placed immediately after the parameter list of the constructor in the child

USC CSCI 103L 11/26 Instantiating Objects with Inheritance

(12)

Default Constructor in Parent #1

1 #include <iostream>

2 using namespace std;

3

4 class Parent { 5 public:

6 Parent() {

7 cout << "in parent" << endl;

8 } 9 };

10

11 class Child : public Parent { 12 public:

13 Child() {

14 cout << "in child" << endl;

15 } 16 };

17

18 int main() { 19 Child c;

20 return 1;

21 }

USC CSCI 103L 12/26 Instantiating Objects with Inheritance

(13)

Default Constructor in Parent #2

1 #include <iostream>

2 using namespace std;

3

4 class Parent { 5 public:

6 Parent() {

7 cout << "in parent" << endl;

8 } 9 };

10

11 class Child : public Parent { 12 public:

13 Child(int num) {

14 cout << "in child" << endl;

15 } 16 };

17

18 int main() { 19 Child c(3);

20 return 1;

21 }

USC CSCI 103L 13/26 Instantiating Objects with Inheritance

(14)

Non-Default Constructor in Parent #1

1 #include <iostream>

2 using namespace std;

3

4 class Parent { 5 private:

6 int num;

7 public:

8 Parent(int num) {

9 cout << "in parent" << endl;

10 this->num = num;

11 } 12 };

13

14 class Child : public Parent { 15 public:

16 Child() {

17 cout << "in child" << endl;

18 } 19 };

20

21 int main() { 22 Child c;

23 return 1;

24 }

USC CSCI 103L 14/26 Instantiating Objects with Inheritance

(15)

Non-Default Constructor in Parent #2

1 #include <iostream>

2 using namespace std;

3

4 class Parent { 5 private:

6 int num;

7 public:

8 Parent(int num) {

9 cout << "in parent" << endl;

10 this->num = num;

11 } 12 };

13

14 class Child : public Parent { 15 public:

16 Child(int n) : Parent(n) { 17 cout << "in child" << endl;

18 } 19 };

20

21 int main() { 22 Child c(3);

23 return 1;

24 }

USC CSCI 103L 15/26 Instantiating Objects with Inheritance

(16)

Non-Default Constructor in Parent #3

1 #include <iostream>

2 using namespace std;

3

4 class Parent { 5 private:

6 int num;

7 public:

8 Parent(int num) {

9 cout << "in parent" << endl;

10 this->num = num;

11 } 12 };

13

14 class Child : public Parent { 15 public:

16 Child() : Parent(3) {

17 cout << "in child" << endl;

18 } 19 };

20

21 int main() { 22 Child c;

23 return 1;

24 }

USC CSCI 103L 16/26 Instantiating Objects with Inheritance

(17)

Outline

USC CSCI 103L 17/26

▪ Inheritance

▪ Instantiating Objects with Inheritance

▪ Inheritance Example

▪ Program

(18)

Single Class Example

1 #include <iostream>

2 using namespace std;

3

4 class Shape { 5 protected:

6 string name;

7 public:

8 Shape(string name) { 9 this->name = name;

10 }

11 void print_name() { 12 cout << this->name;

13 } 14 };

15

16 int main() {

17 Shape s("csci 103");

18 s.print_name();

19 return 1;

20 }

USC CSCI 103L 18/26 Inheritance Example

(19)

Two Class Example #1

1 #include <iostream>

2 using namespace std;

3

4 class Shape { 5 protected:

6 string name;

7 public:

8 Shape() { }

9 Shape(string name) { 10 this->name = name;

11 }

12 void print_name() { 13 cout << this->name;

14 } 15 };

16

17 class TwoDShape : public Shape { 18 public:

19 TwoDShape(string name) { 20 this->name = name;

21 } 22 };

23

24 int main() {

25 TwoDShape tds("csci 103");

26 tds.print_name();

27 return 1;

28 }

USC CSCI 103L 19/26 Inheritance Example

(20)

Two Class Example #2

1 #include <iostream>

2 using namespace std;

3

4 class Shape { 5 protected:

6 string name;

7 public:

8 Shape() { }

9 Shape(string name) { 10 this->name = name;

11 }

12 void print_name() { 13 cout << this->name;

14 } 15 };

16

17 class TwoDShape : public Shape { 18 public:

19 TwoDShape(string name) { 20 Shape::name = name;

21 } 22 };

23

24 int main() {

25 TwoDShape tds("csci 103");

26 tds.print_name();

27 return 1;

28 }

USC CSCI 103L 20/26 Inheritance Example

(21)

Three Class Example

1 #include <iostream>

2 using namespace std;

3

4 class Shape { 5 protected:

6 string name;

7 public:

8 Shape() { }

9 Shape(string name) { 10 this->name = name;

11 }

12 void print_name() { 13 cout << this->name;

14 } 15 };

16

17 class TwoDShape : public Shape { 18 public:

19 TwoDShape(string name) { 20 this->name = name;

21 } 22 };

USC CSCI 103L 21/26 Inheritance Example

23 class Triangle : public TwoDShape { 24 private:

25 double base, height;

26 public:

27 Triangle(string n, double b, double h) : TwoDShape(n) { 28 this->base = b;

29 this->height = h;

30 }

31 float get_area() {

32 return 0.5 * base * height;

33 } 34 };

35

36 int main() {

37 Triangle tri("triangle", 10.0, 5.0);

38 tri.print_name();

39 cout << " has area = " << tri.get_area() << endl;

40 return 1;

41 }

(22)

Four Class Example

1 #include <iostream>

2 using namespace std;

3

4 class Shape { 5 protected:

6 string name;

7 public:

8 Shape() { }

9 Shape(string name) { 10 this->name = name;

11 }

12 void print_name() { 13 cout << this->name;

14 } 15 };

16

17 class TwoDShape : public Shape { 18 public:

19 TwoDShape(string name) { 20 this->name = name;

21 } 22 };

USC CSCI 103L 22/26 Inheritance Example

23 class Triangle : public TwoDShape { 24 private:

25 double base, height;

26 public:

27 Triangle(string n, double b, double h) : TwoDShape(n) { 28 this->base = b;

29 this->height = h;

30 }

31 double get_area() {

32 return 0.5 * base * height;

33 } 34 };

35

36 class Rectangle : public TwoDShape { 37 private:

38 double height, width;

39 public:

40 Rectangle(string n, double h, double w) : TwoDShape(n) { 41 this->height = h;

42 this->width = w;

43 }

44 double get_area() { 45 return height * width;

46 } 47 };

48

49 int main() {

50 Triangle tri("triangle", 10.0, 5.0);

51 tri.print_name();

52 cout << "area = " << tri.get_area() << endl;

53 Rectangle rect("rectangle", 10.0, 5.0);

54 rect.print_name();

55 cout << " has area = " << rect.get_area() << endl;

56 return 1;

57 }

(23)

Five Class Example

1 #include <iostream>

2 using namespace std;

3

4 class Shape { 5 protected:

6 string name;

7 public:

8 Shape() { }

9 Shape(string name) { 10 this->name = name;

11 }

12 void print_name() { 13 cout << this->name;

14 } 15 };

16

17 class TwoDShape : public Shape { 18 public:

19 TwoDShape(string name) { 20 this->name = name;

21 } 22 };

23 class Triangle : public TwoDShape { 24 private:

25 double base, height;

26 public:

27 Triangle(string n, double b, double h) : TwoDShape(n) { 28 this->base = b;

29 this->height = h;

30 }

31 double get_area() {

32 return 0.5 * base * height;

33 } 34 };

35

USC CSCI 103L 23/26 Inheritance Example

36 class Rectangle : public TwoDShape { 37 private:

38 double height, width;

39 public:

40 Rectangle(string n, double h, double w) : TwoDShape(n) { 41 this->height = h;

42 this->width = w;

43 }

44 double get_area() { 45 return height * width;

46 } 47 };

48

49 class Square : public Rectangle { 50 public:

51 Square(string n, double s) : Rectangle(n, s, s) { } 52 };

53

54 int main() {

55 Triangle tri("triangle", 10.0, 5.0);

56 tri.print_name();

57 cout << " has area = " << tri.get_area() << endl;

58 Rectangle rect("rectangle", 10.0, 5.0);

59 rect.print_name();

60 cout << " has area = " << rect.get_area() << endl;

61 Square sq("square", 10.0);

62 sq.print_name();

63 cout << " area = " << sq.get_area() << endl;

64 return 1;

65 }

(24)

Passing Inherited Objects

1 #include <iostream>

2 using namespace std;

3

4 class Shape { 5 protected:

6 string name;

7 public:

8 Shape() { }

9 Shape(string name) { 10 this->name = name;

11 }

12 void print_name() { 13 cout << this->name;

14 } 15 };

16

17 class TwoDShape : public Shape { 18 public:

19 TwoDShape(string name) { 20 this->name = name;

21 } 22 };

23 class Triangle : public TwoDShape { 24 private:

25 double base, height;

26 public:

27 Triangle(string n, double b, double h) : TwoDShape(n) { 28 this->base = b;

29 this->height = h;

30 }

31 double get_area() {

32 return 0.5 * base * height;

33 } 34 };

35

USC CSCI 103L 24/26 Inheritance Example

36 class Rectangle : public TwoDShape { 37 private:

38 double height, width;

39 public:

40 Rectangle(string n, double h, double w) : TwoDShape(n) { 41 this->height = h;

42 this->width = w;

43 }

44 double get_area() { 45 return height * width;

46 } 47 };

48

49 class Square : public Rectangle { 50 public:

51 Square(string n, double s) : Rectangle(n, s, s) { } 52 };

53

54 void print_shape_name(Shape s) { 55 cout << "The shape ";

56 s.print_name();

57 } 58

59 int main() {

60 Triangle tri("triangle", 10.0, 5.0);

61 print_shape_name(tri);

62 cout << " has area = " << tri.get_area() << endl;

63 Rectangle rect("rectangle", 10.0, 5.0);

64 print_shape_name(rect);

65 cout << " has area = " << rect.get_area() << endl;

66 Square sq("square", 10.0);

67 print_shape_name(sq);

68 cout << " has area = " << sq.get_area() << endl;

69 return 1;

70 }

(25)

Outline

USC CSCI 103L 25/26

▪ Inheritance

▪ Instantiating Objects with Inheritance

▪ Inheritance Example

▪ Program

(26)

Program

▪ Mimicking the program in this lecture, create a program for finding the volume of three-dimensional shapes. Make sure to draw out the inheritance hierarchy you will use for cube, box, sphere, threeDShape, and Shape before writing any code.

USC CSCI 103L 26/26 Program

c:\>ThreeDShapes.exe

Enter the radius of the sphere: 3 Volume of sphere is 113.10

Enter the side of the cube: 5 Volume of cube is 125

Enter the width of the box: 3 Enter the length of the box: 4 Enter the height of the box: 5 Volume of box is 60

c:\>

References

Related documents

When the dataset size is larger, 40 blocks, the proposed scheme shows lower average access time than those of the random selection scheme and the SLF scheme.. Still, the SLF

Ongoing development of GCSS-Army Release 1.1 may require modification of the content contained in this

class, only the public and protected members of base class can be accessed by the member functions of derived class. This means no private member of the base class

Table 4.8 Pearson Correlation Statistics for the Variables 97 Table 4.9 Regression Result of transformational leadership and Crisis Management 99 Table 4.10 Regression Result

La estancia hospitalaria es relevante, ya que definiendo y (re)definiendo roles, asumiendo mientras más tiempo tenga el cuidador principal la enfermera un rol de

 Private methods of the base class are not accessible to a derived class (unless the derived class is a friend of the base class).  If the subclass is derived

 Private methods of the base class are not accessible to a derived class (unless the derived class is a friend of the base class).  If the subclass is derived

9.6 Overriding Base-Class Members in a Derived Class 9.7 Public, Protected and Private Inheritance.. 9.8 Direct Base Classes and Indirect