logo Use CA10RAM to get 10%* Discount.
Order Nowlogo
(5/5)

Inheritance is a form of software reuse in which you create a class that inherits an existing class’s data members and member functions

INSTRUCTIONS TO CANDIDATES
ANSWER ALL QUESTIONS

CS2400 Computer Science II

Inheritance

Basic concepts:

  • Inheritance is a form of software reuse in which you create a class that inherits an existing class’s data members and member functions and enhances them with new capabilities by adding data members and member functions in the derived class or classes.
  • The existing class is called the base (or super) class and the new class is referred to as the derived (or sub) class.
  • A derived class represents a more specialized group of objects.
  • A derived class can also modify behaviors inherited from the base class.
  • In the case of single inheritance, a class is derived from one base class.
  • C++ also supports multiple inheritance, in which a derived class may inherit from multiple base classes.
  • C++ offers public, protected and private For most practical applications, public inheritance is used most frequently.
  • With public inheritance, every object of a derived class is also an object of that derived class’s base class. However, base-class objects are not objects of their derived classes.
  • The is-a or ISA relationship exists between base and its derived classes.
  • In an is-a relationship, an object of a derived class also can be treated as an object of its base class.
  • Derived-class member functions might require access to base-class data members and member functions.
  • A derived class can directly access the non-private members of its base class.
  • Base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class.
  • A derived class can change the values of private base-class members, but only through non-private member functions provided in the base class and inherited into the derived class.
  • Often, an object of one class is an object of another class, as well.
    • For example, in geometry, a rectangle is a quadrilateral (as are squares, parallelograms and trapezoids).
    • Thus, in C++, class Rectangle can be said to inherit from class Quadrilateral.
    • In this context, class Quadrilateral is a base class, and class Rectangle is a derived class.
    • A rectangle is a specific type of quadrilateral, but it’s incorrect to claim that a quadrilateral is a rectangle—the quadrilateral could be a parallelogram or some other shape.
  • Using protected access offers an intermediate level of protection between public and private access.
  • A base class’s protected members can be accessed within the body of that base class, by members and friends of that base class, and by members and friends of any classes derived from that base class.
  • Derived-class member functions can refer to public and protected members of the base class simply by using the member names.
  • When a derived-class member function redefines a base-class member function, the base-class member can be accessed from the derived class by preceding the base-class member name with the base-class name and the binary scope resolution operator (::).

 

Inheritance examples:

 

Inheritance hierarch for university community members:

Inheritance hierarchy of shapes:

 

Inheritance diagram for the below demos: A simplified version of the above shape hierarchy

// Demo 1: shows 1) inheritance of base class members by derived classes, 2) the implication of protected access specifier,

#include <iostream>

using namespace std;

 

class polygon

{

   friend ostream& operator<<(ostream &out, const polygon &);

public:

   void set_values(double a, double b);

protected:

   double width;

   double height;

};

void polygon::set_values (double a, double b)

{

   width = a;       // directly accessible regardless of "private" or "protected" for two data members

   height = b;            

}

ostream& operator<<(ostream &out, const polygon &pobj)

{

   out << "Width = " << pobj.width << ", Height = " << pobj.height;

   return out;

}

class rectangle : public polygon // pulbic inheritance

{

public:

   double area();  

};

double rectangle::area()

{

   return width * height; // directly accessible because width and height are in "protected" section

}

class triangle : public polygon

{

public:

   double area();

};

double triangle::area()

{

   return width * height / 2.0;

}

int main ()

{

  r.set_values(4.0, 5.0);

  cout << r << endl;

  cout << "Area of rectangle = " << r.area() << "\n\n";

 

  t.set_values(6.0, 7.0);

  cout << t << endl;

  cout << "Area of triagle = " << t.area() << "\n\n";

 

  return 0;

}

Output:

// Demo 2: basic class constructor is not inherited but is called automatically whenever a derived class object is instantiated

//         Also shows destructors (for both base and derived classes) are called automatically when a derived class goes out of scope

#include <iostream>

using namespace std;

 

class polygon

{

   friend ostream& operator<<(ostream &out, const polygon &);

public:

   polygon();

   ~polygon();

   void set_values(double a, double b);

protected:

   double width;

   double height;

};

polygon::polygon()

{

   cout << "Base polygon class constructor called!" << endl;

}

polygon::~polygon()

{

   cout << "Base polygon class destructor called!" << endl;

}

void polygon::set_values (double a, double b)

{

   width = a;       // directly accessible regardless of "private" or "protected" for two data members

   height = b;            

}

ostream& operator<<(ostream &out, const polygon &pobj)

{

   out << "Width = " << pobj.width << ", Height = " << pobj.height;

   return out;

}

(5/5)
Attachments:

Related Questions

. Introgramming & Unix Fall 2018, CRN 44882, Oakland University Homework Assignment 6 - Using Arrays and Functions in C

DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma

. The standard path finding involves finding the (shortest) path from an origin to a destination, typically on a map. This is an

Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t

. Develop a program to emulate a purchase transaction at a retail store. This program will have two classes, a LineItem class and a Transaction class. The LineItem class will represent an individual

Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th

. SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

. Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

Ask This Question To Be Solved By Our ExpertsGet A+ Grade Solution Guaranteed

expert
Atharva PatilComputer science

580 Answers

Hire Me
expert
Chrisantus MakokhaComputer science

941 Answers

Hire Me
expert
AyooluwaEducation

570 Answers

Hire Me
expert
RIZWANAMathematics

510 Answers

Hire Me

Get Free Quote!

413 Experts Online