Question

In: Computer Science

Describe the methods for converting inherited types, or 8) superclass/subclasses instances. Include the four options for...

Describe the methods for converting inherited types, or 8) superclass/subclasses instances. Include the four options for conversion and describe the tradeoffs of using each one in terms of total/partial participation and support for overlapping/disjoint subtypes. Then, describe how to represent 9) union types.

Solutions

Expert Solution

Inheritance is one of the core concepts of object-oriented programming (OOP) languages. It is a mechanism where you can to derive a class from another class for a hierarchy of classes that share a set of attributes and methods. C++ allows the conversion Derived* Base*, since a Derived**Base** is flagged as an error. Although this error may not be obvious, it is nonetheless a good thing. For example, if you could convert Car**Vehicle**, and if you could similarly convert NuclearSubmarine**Vehicle**, you could assign those two pointers and end up making a Car* point at a NuclearSubmarine:

class Vehicle

{

public:

virtual ~Vehicle() {}

virtual void startEngine() = 0;

};

class Car : public Vehicle

{

public:

virtual void startEngine();

virtual void openGasCap();

};

class nuclearSubmarine : public Vehicle

{

public:

virtual void startEngine();

virtual void fireNuclerMissile();

};

int main()

{

Car car;

Car* carPtr = &car;

Car** carPtrPtr = &carPtr;

Vehicle** vehiclePtrPtr = carPtrPtr; //This is an error

NuclearSubmarine sub;

NuclearSubmarine* subPtr = ⊂

*vehiclePtrPtr = subPtr; /* This line would have caused carPtr to point to sub*/

carPtr->openGasCap(); //This might call fireNuclearMissile()

}

Instance of superclass and subclass:

An important concept associated with the superclass/subclass is the concept of attribute inheritance. One of the definitions of inheritance is the derivation of a quality or characteristic from a predecessor of progenitor. Simple put, the child or subclass contains qualities or characteristics of he superclass (parent or grandparent). Because an entity in a sublcass represents membership in the superclass as well, it should inherit all of the properties and the attibutes of the superclass entity. The subclass entity will also inherit all relationship instances that the superclass participates in.

How to represent Union:

A union is a special data type availale in C that allows to store different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time. Union provide an efficient way of using the same memory location for multiple-purpose.

Defining a Union:

To define a union, you must use the union statement in the same way as you did while defining a structure. The union statement define a new data type with more than one member for your program. The formate of the union statement is as follows:

union [union tag]

{

member defination;

member defination;

...

member defnation;

} [one or more union variables];

The union tag is optional and each member defination is a normmal variable defination, such as int i; or float f; or any other valid variable defination. At the end of the union's defination, before the final semicolon, you can specify one or more union variables but it is optional. Here is the way you would define a union typenamed Data having three members i,f and str-

union Data

{

int i;

floar f;

char str[20];

}data;

Now, a variable of Data type can store an integer, a floating-point number, or a string of characters. It means a single variables, i.e. same memory location, can be used to store multiple types of data. You can use any built-in or user defined data types inside a union based on your requirments.

The memory occupied by a union will be large enough to hold the largest member of the union. For example, in the above example, Data type will occupy 20 bytes of memory space because this is the maximum space which can be occupied by a charcter string. The following example displays the total memory size occupied by the above union-

#include<stdio.h>

#include<string.h>

union Data

{

int i;

float f;

char str[20];

};

int main()

{

union Data data;

printf("Memory size occupied by data: %d", sizeof(data));

return 0;

}


Related Solutions

1. Describe the different methods of scale construction. Include specific examples of the different types.
1. Describe the different methods of scale construction. Include specific examples of the different types.
Question 3 (20 Marks) 3.1. Describe four (4) types of appraisal methods to justify IT investment...
Question 3 3.1. Describe four (4) types of appraisal methods to justify IT investment evaluation and its specified evaluation methods. 3.2. Discuss the Information Systems department and how it is utilized to manage End-User relationships. Include in your discussion an outline of the FOUR (4) ISD approaches that could be applied by an organisation.
In the context of post-merger reorganisation: Describe four types of post-merger integration. (8 marks
In the context of post-merger reorganisation: Describe four types of post-merger integration. (8 marks
identify and describe the four inventory valuation methods
identify and describe the four inventory valuation methods
Identify and describe two types of non-probability sampling methods and two types of probability sampling methods
Identify and describe two types of non-probability sampling methods and two types of probability sampling methods
PART B: 3.4 Name and briefly describe the four (4) different types of discrimination. (8) 3.5...
PART B: 3.4 Name and briefly describe the four (4) different types of discrimination. (8) 3.5 Define the term “insider trading” and why is it considered unethical and illegal. (6) 3.6 Give an example of insider trading. (1) 3.7 Should an employee find himself in a situation where a potential conflict of interest arise, how can he / she avoid or eliminate that conflict risk? (3)
Describe the 8 types of capital budgeting projects.
Describe the 8 types of capital budgeting projects.
Describe four methods of sampling, and provide examples of each. How could each of these methods...
Describe four methods of sampling, and provide examples of each. How could each of these methods be useful in some area of business? Do you think any more method would be more valuable than others? Why or why not?
List and briefly describe the four different methods of forecasting available.
List and briefly describe the four different methods of forecasting available.
Describe briefly the four (4 ) control methods of the contaminants (pollutants)
Describe briefly the four (4 ) control methods of the contaminants (pollutants)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT