Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

a) Create three classes shape, rectangle and triangle b) Class triangle and rectangle inherits class shape c) Class shape has protected variables width and height

image text in transcribed
a) Create three classes shape, rectangle and triangle b) Class triangle and rectangle inherits class shape c) Class shape has protected variables width and height and public function shape which has two parameters which accepts input from user which will to width and height variables d) There is a function in class shape called area which displays "Parent Class area" e) Class rectangle has public access modifier and a function called has two parameters and accepts user inputs f) Class rectangle has function called area which daploys fwidth "height) g) Class triangle has public access modifier and a function called triangle which has two parameters each one accepts user input. h) Class triangle has function called area which displiys (width *height)/2 9) Finally create pointer object of class shape which points to address of rectangle and triangle and display value of area of rectangle and area of triangle Description: The word polymorphism means having many forms C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function. Consider the following example where a base class has been derived by other two classes: The reason for the incorrect output is that the call of the function area[D is being set once by the compiler as the version defined in the base class This is called static resolution of the function call, or static linkge - the function call is fued before the program is executed. This is also sometimes called early binding because the area0 function is set during the compilation of the program, But now, let's make a slight modification in our program and precede the declaration of area[ in the Shape class with the keyword virtual so that it looks like this: After this slight modification, when the previous example code is compiled and executed, it produces the following result: fectangle class area Triangle class area This time, the compiler looks at the contents of the pointer irstead of it's type. Hence, since addresses of objects of tri and rec classes are stored in shape the respectivearea() function is called. As you can see, each of the child classes has a separate implementation for the function area0. This is how polymorphism is generally used You have different classes with a function of the same name, and even the same parameters, but with different implementations

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions