Download presentation

Presentation is loading. Please wait.

Published byMaribel Bushey Modified over 3 years ago

1
Inheritance Math 130 B Smith: Consider using the example in SAMS Teach Yourself C++ in 24 hours B Smith: Consider using the example in SAMS Teach Yourself C++ in 24 hours

2
2 Overview to cover sub title also cover this is a box a callout this is code this is also code

3
3 Motivation Want to reuse code Don’t want to destroy the perfection of the existing code that we’ll reuse Goal: Use C++ classes without breaking the existing code Solution: Create a new class as a type of an existing class inherit the features you’re interested in

4
4 Example 1 class Circle { protected: double radius; public: Circle(double = 1.0); // constructor double getArea(); };

5
5 // class implementation Circle::Circle(double r) // constructor { radius = r; } double Circle::getArea() // this calculates an area { return(PI * radius * radius); } Implementation

6
6 class Cylinder : public Circle // Cylinder is derived from Circle { protected: double length; // add one additional data member and public: // two additional function members Cylinder(double r = 1.0, double l = 1.0) : Circle(r), length(l) {} double getArea(); }; // class implementation double Cylinder::getArea() // this calculates a volume { return (length * Circle::getArea()); // note the base function call } class Circle { protected: double radius; public: Circle(double = 1.0); double getArea(); }; B Smith: Insert pictures here to show how one builds/inherits attributes from the other B Smith: Insert pictures here to show how one builds/inherits attributes from the other

7
7 Example 2 class one // the base class { protected: float a; public: one(float = 2); // constructor float f1(float); // a member function float f2(float); // another member function };

8
8 Implementation // class implementation one::one(float val) // constructor { a = val; } float one::f1(float num) // a member function { return(num/2); } float one::f2(float num) // another member function { return( pow(f1(num),2) ); // square the result of f1() }

9
9 class two : public one // the derived class { public: float f1(float); // this overrides class one's f1() }; // class implementation float two::f1(float num) { return(num/3); }

10
10 Driver Code int main() { one object_1; // object_1 is an object of the base class two object_2; // object_2 is an object of the derived class // call f2() using a base class object call cout << "The computed value using a base class object call is " << object_1.f2(12) << endl; // call f2() using a derived class object call cout << "The computed value using a derived class object call is " << object_2.f2(12) << endl; return 0; }

11
11 Example 3 class One // the base class { protected: float a; public: One(float = 2); // constructor virtual float f1(float); // a member function float f2(float); // another member function };

12
12 Implementation // class implementation One::One(float val) // constructor { a = val; } float One::f1(float num) // a member function { return(num/2); } float One::f2(float num) // another member function { return( pow(f1(num),2) ); // square the result of f1() }

13
13 class Two : public One // the derived class { public: virtual float f1(float); // this overrides class One's f1() }; // class implementation float Two::f1(float num) { return(num/3); }

14
14 int main() { One object_1; // object_1 is an object of the base class Two object_2; // object_2 is an object of the derived class // call f2() using a base class object call cout << "The computed value using a base class object call is " << object_1.f2(12) << endl; // call f2() using a derived class object call cout << "The computed value using a derived class object call is " << object_2.f2(12) << endl; return 0; }

15
15 Summary this is code

Similar presentations

OK

The Math class Java provides certain math functions for us. The Math class contains methods and constants that can be very useful. The Math class is like.

The Math class Java provides certain math functions for us. The Math class contains methods and constants that can be very useful. The Math class is like.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To ensure the functioning of the site, we use **cookies**. We share information about your activities on the site with our partners and Google partners: social networks and companies engaged in advertising and web analytics. For more information, see the Privacy Policy and Google Privacy & Terms.
Your consent to our cookies if you continue to use this website.

Ads by Google

Moral stories for kids ppt on batteries Ppt on the road not taken essay Ppt on carl friedrich gauss formula Ppt on go green Ppt on advances in automobile engineering download Ppt on water pollution remedies Ppt on political parties and electoral process in canada Ppt on bank lending policies Ppt on building construction in india Ppt on renewable energy in india