logo Hurry, Grab up to 30% discount on the entire course
Order Now logo

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

expert
Aditya PanchalMathematics
(5/5)

879 Answers

Hire Me
expert
Lawrence BankheadMarketing
(5/5)

758 Answers

Hire Me
expert
StatAnalytica ExpertManagement
(5/5)

756 Answers

Hire Me
expert
Neil BissonnetteeCriminology
(5/5)

964 Answers

Hire Me
C++ Programming

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;

}

Related Questions

. The fundamental operations of create, read, update, and delete (CRUD) in either Python or Java

CS 340 Milestone One Guidelines and Rubric  Overview: For this assignment, you will implement the fundamental operations of create, read, update,

. 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

Retail Transaction Programming Project  Project Requirements:  Develop a program to emulate a purchase transaction at a retail store. This

. The following program contains five errors. Identify the errors and fix them

7COM1028   Secure Systems Programming   Referral Coursework: Secure

. Accepts the following from a user: Item Name Item Quantity Item Price Allows the user to create a file to store the sales receipt contents

Create a GUI program that:Accepts the following from a user:Item NameItem QuantityItem PriceAllows the user to create a file to store the sales receip

. The final project will encompass developing a web service using a software stack and implementing an industry-standard interface. Regardless of whether you choose to pursue application development goals as a pure developer or as a software engineer

CS 340 Final Project Guidelines and Rubric  Overview The final project will encompass developing a web service using a software stack and impleme