Question

In: Computer Science

Describe a scenario/design where a polymorphic behavior is utilized. In the lecture this week we studied...

Describe a scenario/design where a polymorphic behavior is utilized. In the lecture this week we studied an example in which we defined a GeometricObject as the base class and added features to this base class to define two specialized classes, namely the Circle and the Rectangle classes. Your design should have the design elements of the example we studied but it doesn’t have to be related to geometric shapes. Clearly list the functions in the base class you would declare as virtual, what is the expected behavior? And what happens if the functions are not made virtual? Do not code, describe your design and all features of a polymorphic design requirements.

Solutions

Expert Solution

Consider the example given in the problem as the base is the GeometricObject and the derived classes are Circle and Rectangle.

Therefore, the structure will may be as follows:

class GeometricObject{

};

class Circle: <access_specifier> GeometricObject

{

};

class Rectangle: <access_specifier> GeometricObject

{

};

  • For the polymorphism behaviour, assume that there are two functions that compute the area and the description of the class.
  • The name of the area function is Area(), and of the description is Description().
  • So, if we are declared these functions are virtual which is shown below, and call the functions with the pointer of the Base class, then the functions of the derived class will be called.

class GeometricObject{

public:

virtual void Area()

{

}

virtual void Description()

{

}

};

class Circle: <access_specifier> GeometricObject

{

public:

Circle(){}

void Area()

{

}

void Description()

{

}

};

class Rectangle: <access_specifier> GeometricObject

{

public:

Rectangle(){}

void Area()

{

}

void Description()

{

}

};

int main()

{

//Pointer of Base class creating the object

//of Derived classes Circle and Rectangle.

GeometricObject *object = new Circle();

GeometricObject *object1 = new Rectangle();

//Functions of the class Circle is called.

object.Area();

object.Description();

//Functions of the class Rectangle is called.

object1.Area();

object1.Description();

}

  • The reason is that, the virtual keyword is used with the functions in the base class.
  • The virtual keyword performs the dynamic binding and not the static binding.
  • In dynamic binding, the function which is being pointed is called and not the type of pointer it is.
  • Therefore, the function of Circle and Rectangle will be called.
  • Now, if there is no virtual keyword with the functions.
  • Then the functions of the Base class GeometricObject will be called.
  • The reason is that the compiler performs the static binding.
  • In static binding, the function as per the pointer in the object is being called.

Thus, for the dynamic binding or calling the functions of derived class with the pointer of the Base class, use the virtual keyword in the Base class functions.

Otherwise, compiler will always perform the static binding.


Related Solutions

When we studied Organizational Behavior Modification we learned about the four types of consequences that impact...
When we studied Organizational Behavior Modification we learned about the four types of consequences that impact motivation - Positive reinforcement, negative reinforcement, punishment and extinction.  Explain each of these consequences and provide an example for each of how they can motivate an employee in the workplace.
From our Week 2 lecture, we discussed the history of Health and Health Education. In 1850,...
From our Week 2 lecture, we discussed the history of Health and Health Education. In 1850, Lemuel Shattuck wrote a Report of the Sanitary Commission of Massachusetts and shared how public health problems should be approached. Provide examples of his recommendations and explain if those approaches do or do not apply to today’s public health problems. Answer in essay based
In our lecture this week we read about slotting fees, fees that manufacturers pay retailers to...
In our lecture this week we read about slotting fees, fees that manufacturers pay retailers to stock their product on their shelves. Is this legal? Ethical?
Describe a homeostatic imbalance of one of the systems we studied this semester. It can be...
Describe a homeostatic imbalance of one of the systems we studied this semester. It can be any disease or imbalance of your choice. Describe how it affects your selected body system.
In Chapter3 and also in the growth model where we studied about the long run, deficit...
In Chapter3 and also in the growth model where we studied about the long run, deficit spending( a higher government spending for a given tax rate) appears to have a negative effect on the economy but in the IS-LM model we can see that the opposite is true. A higher G increases the GDP by enhancing demand. Which one do you think is true? Can you reconcile these two ideas? What is your conclusion in terms of practical policy?
Describe how the “kidney” works in amphixous using the anatomical terms we discussed in lecture and...
Describe how the “kidney” works in amphixous using the anatomical terms we discussed in lecture and then a possible mechanism of how it may move materials from the blood
Reflect back to this week’s lecture and discussion where we learned about the consumer decision-making process....
Reflect back to this week’s lecture and discussion where we learned about the consumer decision-making process. In your own words, explain the process and how you would utilize it in creation of a marketing plan
Describe a scenario where selection occurs on a trait, but there is no evolutionary change.
Describe a scenario where selection occurs on a trait, but there is no evolutionary change.
This week, we will discuss the research design and sample for your nursing clinical issue. The...
This week, we will discuss the research design and sample for your nursing clinical issue. The research design flows from the research question and outlines the plan for the study that will answer the research question. The design identifies the major components of the study. It is important to remember that there is no one best design for a research study. Research question: Which techniques or strategies are most effective in promoting compliance to adhering to psych medication regimens? After...
This week, we will discuss the research design and sample for your nursing clinical issue. The...
This week, we will discuss the research design and sample for your nursing clinical issue. The research design flows from the research question and outlines the plan for the study that will answer the research question. The design identifies the major components of the study. It is important to remember that there is no one best design for a research study. The design identifies the major components of the study. Please note that there is no one best design for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT