Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Draw an inheritance hierarchy for classes ouadrilateral, TRapezoid, Parallelogram, Rectangle and Square. Use quadrilateral as the base class of the hierarchy. Make the hierarchy as deep as possible.

Short Answer

Expert verified
1. Quadrilateral 2. Trapezoid 3. Parallelogram 4. Rectangle 5. Square

Step by step solution

01

Understand the Base Class

The problem involves creating an inheritance hierarchy starting with the base class 'Quadrilateral'. A quadrilateral is a polygon with four sides, serving as the most general class in this hierarchy.
02

Recognize Special Characteristics

Understand the unique characteristics of each quadrilateral type: - Trapezoid: A quadrilateral with at least one pair of parallel sides. - Parallelogram: A quadrilateral with two pairs of parallel sides. - Rectangle: A parallelogram with four right angles. - Square: A rectangle with all sides of equal length.
03

Design the Hierarchical Diagram

Start with placing 'Quadrilateral' at the top. Below it, derive 'Trapezoid', as a more specific case, because it includes at least one pair of parallel sides. From 'Trapezoid', derive 'Parallelogram' since it specifies two pairs of parallel sides. Next, derive 'Rectangle' from 'Parallelogram' due to right angles, and finally place 'Square' under 'Rectangle' because all sides are equal.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

Object-Oriented Programming
Object-Oriented Programming (OOP) is a fundamental concept in software development, providing a framework where everything is an object. This approach allows for structuring code in a way that is both intuitive and efficient.

OOP simplifies complex systems by defining objects that represent real-world entities. Each object is an instance of a class and can have attributes (the object's characteristics) and methods (the object's behaviors). By organizing code in this way, developers can create models of real-world problems that are easier to manipulate and extend.

Key principles of OOP include encapsulation, inheritance, and polymorphism. Encapsulation allows objects to hide their internal state and require all interaction to be performed through an object's methods. This protects the object's integrity by preventing outside interference.
  • Encapsulation: Promotes data protection by combining data and code.
  • Inheritance: Facilitates code reusability and hierarchy creation.
  • Polymorphism: Enables the same method to perform differently based on the object it is called upon.
Understanding these concepts allows developers to write clear, reusable, and scalable code.
Class Hierarchy
In a class hierarchy, classes are organized in a parent-child structure based on shared characteristics and behaviors. This structure allows for efficient code organization and reuse.

A base class, also known as a parent or super class, generally embodies the most general attributes and methods. Derived classes, or subclasses, inherit these characteristics while introducing additional features or specificity.

In the exercise provided, the class hierarchy starts with 'Quadrilateral' as the base class. It features subclasses like 'Trapezoid', 'Parallelogram', 'Rectangle', and 'Square', each representing more specific types of quadrilaterals.
  • 'Quadrilateral': The base class with general attributes like side lengths.
  • 'Trapezoid': Inherits from Quadrilateral, adds the concept of parallel sides.
  • 'Parallelogram': A specialized Trapezoid with both pairs of sides parallel.
  • 'Rectangle' and 'Square': Special cases of Parallelograms with right angles and equal sides, respectively.
This hierarchy allows developers to utilize code reuse and extend existing codebases quickly.
Polymorphism
Polymorphism is a powerful feature of object-oriented programming that allows for methods to do different things based on the object they are operating on. This concept is essential for dynamic and flexible code.

In polymorphic behavior, a single function or method name can work differently depending on the object's class it is called on. Two main types of polymorphism in C++ are:
  • Compile-time polymorphism: Achieved via method overloading or operator overloading. The function's behavior is resolved at compile time.
  • Runtime polymorphism: Relies on inheritance and virtual functions. The behavior is determined during program execution.
For instance, if you have a function called `calculateArea()`, its implementation might differ for a 'Rectangle' and a 'Square' due to their distinct characteristics. This feature allows developers to simplify code management and enhance the program's scalability.

By employing polymorphism, C++ developers can manage different data types and objects more effectively, leveraging a singular interface.
C++ Programming Concepts
C++ is a versatile language, particularly known for its support of object-oriented programming features like inheritance and polymorphism.

The language provides classes and objects as its primary means to create modular programs. A class serves as a blueprint for objects—defining attributes and methods that describe behaviors. In C++, class declarations typically contain data members and member functions.

Here's a basic structure of a class declration in C++:
```cpp class ClassName { private: // private data members public: // public data members // member functions }; ```
  • Data Members: Variables within a class that hold data specific to an object.
  • Member Functions: Functions within a class that define the behavior of an object.
Central to C++ programming are its capabilities concerning memory management through pointers, and low-level manipulation. C++ allows precise control over system resources, which can enhance performance.

C++ syntax can be complex due to its extensive feature set, but mastering its concepts allows programmers to design efficient, powerful applications that can scale and evolve.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

The world of shapes is much richer than the shapes included in the inheritance hierarchy of Fig, \(12.3 .\) Write down all the shapes you can think ofboth two- dimensional and three-dimensionaland form them into a more complete Shape hierarchy with as many levels as possible. Your hierarchy should have base class shape from which class Two imensionalshape and class ThreeDimensionalshape are derived. [Note: You do not need to write any code for this exercise.] We will use this hierarchy in the exercises of Chapter 13 to process a set of distinct shapes as objects of base-class shape. (This technique, called polymorphism, is the subject of Chapter \(13 .\).

(Account Inheritance Hierarchy) Create an inheritance hierarchy that a bank might use to represent customers' bank accounts. All customers at this bank can deposit (i.e., credit) money into their accounts and withdraw (i.e., debit) money from their accounts. More specific types of accounts also exist. Savings accounts, for instance, earn interest on the money they hold. Checking accounts, on the other hand, charge a fee per transaction (i.e.. credit or debit). Create an inheritance hierarchy containing base class account and derived classes savingsAccount and checkingAccount that inherit from class Account. Base class Account should include one data member of type double to represent the account balance. The class should provide a constructor that receives an initial balance and uses it to initialize the data member. The constructor should validate the initial balance to ensure that it is greater than or equal to \(\theta . \theta .\) If not, the balance should be set to \(\theta . \theta\) and the constructor should display an error message, indicating that the initial balance was invalid. The class should provide three member functions. Member function credit should add an amount to the current balance. Member function debit should withdraw money from the account and ensure that the debit amount does not exceed the account's balance. If it does, the balance should be left unchanged and the function should print the message "Debit amount exceeded account balance." Member function getBalance should return the current balance. Derived class savingsAccount should inherit the functionality of an Account, but also include a data member of type double indicating the interest rate (percentage) assigned to the Account. SavingsAccount's constructor should receive the initial balance, as well as an initial value for the SavingsAccount's interest rate. SavingsAccount should provide a public member function calculateInterest that returns a double indicating the amount of interest earned by an account. Member function calculateInterest should determine this amount by multiplying the interest rate by the account balance. [Note: SavingsAccount should inherit member functions credit and debit as is without redefining them.] Derived class checkingAccount should inherit from base class Account and include an additional data member of type double that represents the fee charged per transaction. CheckingAccount's constructor should receive the initial balance, as well as a parameter indicating a fee amount. Class CheckingAccount should redefine member functions credit and debit so that they subtract the fee from the account balance whenever either transaction is performed successfully. CheckingAccount's versions of these functions should invoke the base-class account version to perform the updates to an account balance. CheckingAccount's debit function should charge a fee only if money is actually withdrawn (i.e., the debit amount does not exceed the account balance). [Hint: Define Account's debit function so that it returns a bool indicating whether money was withdrawn. Then use the return value to determine whether a fee should be charged.] After defining the classes in this hierarchy, write a program that creates objects of each class and tests their member functions. Add interest to the SavingsAccount object by first invoking its calculateInterest function, then passing the returned interest amount to the object's credit function.

( Package Inheritance Hierarchy) Package-delivery services, such as \(\mathrm{FedEx}^{\mathbb{Q}}\), \(\mathrm{DHL}^{@}\) and \(\mathrm{UPS}^{@}\), offer a number of different shipping options, each with specific costs associated. Create an inheritance hierarchy to represent various types of packages. Use Package as the base class of the hierarchy, then include classes TwoDayPackage and overnight Package that derive from Package. Base class Package should include data members representing the name, address, city, state and ZIP code for both the sender and the recipient of the package, in addition to data members that store the weight (in ounces) and cost per ounce to ship the package. Package's constructor should initialize these data members. Ensure that the weight and cost per ounce contain positive values. Package should provide a public member function calculatecost that returns a double indicating the cost associated with shipping the package. Package's calculatecost function should determine the cost by multiplying the weight by the cost per ounce. Derived class Two DayPackage should inherit the functionality of base class Package, but also include a data member that represents a flat fee that the shipping company charges for two-day-delivery service. TwoDayPackage's constructor should receive a value to initialize this data member. Two ouppackage should redefine member function calculatecost so that it computes the shipping cost by adding the flat fee to the weight-based cost calculated by base class Package's calculatecost function. Class overnightPackage should inherit directly from class Package and contain an additional data member representing an additional fee per ounce charged for overnight-delivery service. overnightPackage should redefine member function calculatecost so that it adds the additional fee per ounce to the standard cost per ounce before calculating the shipping cost. Write a test program that creates objects of each type of Package and tests member function calculatecost.

Fill in the blanks in each of the following statements: a. _______ is a form of software reuse in which new classes absorb the data and behaviors of existing classes and embellish these classes with new capabilities. b. A base class's _______ members can be accessed only in the base- class definition or in derived-class definitions. c. \(\ln a(n)\) _______ relationship, an object of a derived class also can be treated as an object of its base class. d. \(\ln a(n)\) _______relationship, a class object has one or more objects of other classes as members. e. In single inheritance, a class exists in a(n) _______ relationship with its derived classes. f. A base class's _______ members are accessible within that base class and anywhere that the program has a handle to an object of that base class or to an object of one of its derived classes. g. \(A\) base class's protected access members have a level of protection between those of public and _______ access. h. \(C++\) provides for _______ which allows a derived class to inherit from many base classes, even if these base classes are unrelated. i. When an object of a derived class is instantiated, the base class's _______ is called implicitly or explicitly to do any necessary initialization of the base-class data members in the derived-class object. J. When deriving a class from a base class with public inheritance, public members of the base class become _______ members of the derived class, and protected members of the base class become _______ members of the derived class. k. When deriving a class from a base class with protected inheritance, public members of the base class become _______ members of the derived class, and protected members of the base class become _______ members of the derived class.

Some programmers prefer not to use protected access because they believe it breaks the encapsulation of the base class. Discuss the relative merits of using protected access vs. using private access in base classes.

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free