Question

In: Computer Science

Question B Considering how objects list in their own memory space and can behave almost like...

Question B Considering how objects list in their own memory space and can behave almost like independent entities, think about some possible programs that you could create with this concept. (for example, a simulated worlds). Be specific in your description as to how OOP concepts would be employed.

Solutions

Expert Solution

As programming language was not mentioned so provided the example in commonly asked language (C++).

Base class BaseShape.h===

#include <iostream>

// Base class

class BaseShape{

public: float area; //area Of the Shape

public: BaseShape(); //Constructor

public: float CalculateArea(); //Method to Calculate Area of the Shape

};

BaseShape.cpp=====

#include "BaseShape.h"

#include <iostream>

//default implementaion of Base class

BaseShape::BaseShape(){

  area = 0;

}

float BaseShape::CalculateArea(){

  return area;

};

Derived class Square.h===========

//dervied class This shows inheritance

class Square: public BaseShape {

public:

Square(float side);

float CalculateArea();

//new method used by dervied class which is not in base class

float CalculatePerimeter();

private:

float side; /*new variables this shows data hiding/abstraction as variable is private and can't be accessed outside*/

};

Square.cpp=====

#include "BaseShape.h"

#include "Square.h"

#include <iostream>

using namespace std;


Square::Square(float sideOfSquare){

  side = sideOfSquare;   

}

/*redefining the functionality of base class function in dervied class which shows Method Overridding of OOP */

float Square::CalculateArea(){

  return side*side;

}

//Adding new functionality derived class

float Square::CalculatePerimeter(){

  return 4*(side);

}

main program:======

#include "BaseShape.h"

#include "Square.h"

#include <iostream>

using namespace std;

int main() {

Square testSquare1(5);

Square testSquare2(12);

cout << "The area of the Square1 is " << testSquare1.CalculateArea() << '\n';

cout << "The perimeter of the Square1 is " << testSquare1.CalculatePerimeter() << '\n';

//This shows the data encapsulation with in the object, both the object have their own data

cout << "The area of the Square2 is " << testSquare2.CalculateArea() << '\n';

cout << "The perimeter of the Square2 is " << testSquare2.CalculatePerimeter() << '\n';

return 0;

}

output:

Please rate your answer.


Related Solutions

IN PYTHON Question B Considering how objects list in their own memory space and can behave...
IN PYTHON Question B Considering how objects list in their own memory space and can behave almost like independent entities, think about some possible programs that you could create with this concept. (for example, a simulated worlds). Be specific in your description as to how OOP concepts would be employed.
If you have various objects in space, what does space look like? How does the universe...
If you have various objects in space, what does space look like? How does the universe evolve? Please explain your answer.
Two objects, A and B, are released from rest in free space (no other masses are...
Two objects, A and B, are released from rest in free space (no other masses are near). Object A has a greater mass than object B. They accelerate toward each other under the action of their mutual gravitational attraction. a. On which object is the greater work done? b. At the instant just before they hit, which object will have the momentum with the greatest magnitude? c. At the instant just before they hit, which object will have the greater...
a) Explain how performance can be controlled in virtual memory. (b) How does the OS implement...
a) Explain how performance can be controlled in virtual memory. (b) How does the OS implement dynamic allocation of frames? c) Explain the main difficulty in using and/or managing virtual memory.
A. What are Objects? B. How do Objects differ from Classes? C. Where are Objects stored...
A. What are Objects? B. How do Objects differ from Classes? C. Where are Objects stored in Memory? D. Why do you not need to declare when you are finished using an Object in Java? E. Can you edits the contents of a String? View keyboard shortcuts EditViewInsertFormatToolsTable 12pt Paragraph
The majority of people frequently experience the effects of memory distortions like this. Considering the prevalence...
The majority of people frequently experience the effects of memory distortions like this. Considering the prevalence these difficulties, please respond to the following; What memory improvement techniques have you found to be helpful in your everyday life? What new strategies do you plan to try? As an eyewitness to a crime, how could you use information from to improve your memory for specific details?
List the different ways the government can force monopolists to behave and price more competitively and...
List the different ways the government can force monopolists to behave and price more competitively and discuss the difference between Marginal Cost and Average Cost pricing between a natural monopolist and a regular monopolist.
How many ways to group 75 objects into 3 groups if any number of objects can...
How many ways to group 75 objects into 3 groups if any number of objects can be in the 3 groups if each group must have 25 objects?
1.how can i improve my memory? 2. how is memory measured? 3. how does the brain...
1.how can i improve my memory? 2. how is memory measured? 3. how does the brain form and store memorise?
As production volume increases, describe how variable costs "behave”: (a) on a total basis and (b)...
As production volume increases, describe how variable costs "behave”: (a) on a total basis and (b) on a per unit basis. Explain. (2pts each) As production volume increases, describe how fixed costs "behave"”: (a) on a total basis and (b) on a per unit basis. Explain. (2pts each) Lickety-Split makes pints of ice cream sold by major retailers. Identify each of the following costs as variable (V) or fixed (F), relative to the number of pints of ice cream produced....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT