Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Could you please help me with this lab? The main.cpp is attached at the bottom.

ID: 3753365 • Letter: C

Question

Could you please help me with this lab? The main.cpp is attached at the bottom. Thank you

In this lab you’ll be provided with a Visual Studio project containing definitions for a parent class called Shape and two childen: Rectangle and Triangle. You will adapt the classes to use polymorphism.

Lab 6a: Getting Started

Download the code from Canvas (FilesLabsLab6.zip (or Lab6.7z) and re-write main() to create pointers and dynamically allocate variables of each type, e.g. to create a Shape pointer to a dynamically allocated Triangle object, you would use the following command:

Shape* sPtr = new Triangle(10,12,5);

Try calling draw( ) on each pointer-object combination. Remember that you can call a function through a pointer to an object using the syntax:

sPtr->draw();

Play around with the code until you can fill out the following table for regular (non-virtual) functions.

Object Type

Shape

Rectangle

Triangle

Pointer Type

Shape

Rectangle

Triangle


Lab 6b: Virtual Functions

Modify the draw( ) function to make it a virtual function. Then play around with the pointers in main( ) to fill out a fresh copy of the table.

Object Type

Shape

Rectangle

Triangle

Pointer Type

Shape

Rectangle

Triangle

Lab 6c: Pointer Arrays

Modify Shape’s copy of the draw( ) function to make it a pure virtual function.

Re-write main( ) to create a statically-allocated 4 x 4 array of Shape pointers.

Dynamically allocate variables for each of the pointers in your array to point at; you can choose what types of objects to store at each location, but make sure to include both Rectangles and Triangles.

Adapt main( ) so that it will prompt the user 3 times for a set of co-ordinates and will then display the object stored at that location in your array. (For the purposes of the lab, you can assume the user doesn’t give you bad co-ordinates).

<main.cpp file>

#include <iostream>

#include "rectangle.h"

#include "triangle.h"

using namespace std;

int main()

{

    cout << "I need to be written by you! ";

    system("PAUSE");

    return 0;

}

Object Type

Shape

Rectangle

Triangle

Pointer Type

Shape

Rectangle

Triangle

Explanation / Answer

Polymorphism as the name derives 'having multiple forms' , is the characteristic of being to assign a different meaning or usage to something in different contexts.

Lab 6a: Answer as follows below:

Illustrate PolyMorphism Functionality using Regular (non-virtual) function

#include<iostream>

using namespace std;

class shape

{

public:

int a,b,c;

void draw()

{

return a*b*c;

}

};

class triangle:public shape

{

public:

int a1,b1,c1;

triangle(int a1,int b1,int c1) //constructor holding three params a1,b1,c1

{

cout<<"Values Assigned are"<<a1<<b1<<c1<<endl;

}

};

int main()

{

shape* sPtr = new triangle(10,12,5);

cout<<"Output is :"<<sPtr->draw()<<endl;

return 0;

}

Lab 6b: Virtual Function Menas Function will Same Name and Same Arguments can be Used in both Parent and Child Class , Implementation can be used differently depends on the End User.

Check the Example Below:

#include<iostream>

using namespace std;

class shape

{

public:

int a,b;

void set_values(int a1,int b1)

{

a=a1;

b=b1;

}

virtual void area()

{

return 0;

}

};

class rectangle:public shape

{

public:

void area()

{

return (a*b);

}

};

class triangle:public shape

{

public:

void area()

{

return ((a*b)/2);

}

};

int main()

{

triangle tri;

rectangle rect;

shape s;

shape* sPtr = &tri;

shape* sPtr1 = &rect;

shape* sptr2= &s;

sPtr->set_values(4,4);

sPtr1->set_values(4,4);

sptr2->set_values(4,4);

cout<<"Output 1 is :"<<sPtr->area()<<endl;

cout<<"Output 2 is :"<<sPtr1->area()<<endl;

cout<<"Output 3 is :"<<sptr2->area()<<endl;

return 0;

}

Lab 6c: Pure Virtual Function Means Function will be declared in Parent Class and Assigned to '0' (NULL) and the function definitation will be Child Class

Check the Example Below:

#include<iostream>

using namespace std;

class shape

{

public:

int a,b,c;

virtual void draw() = 0;

};

class triangle:public shape

{

public:

int a1,b1,c1;

triangle(int a1,int b1,int c1) //constructor holding three params a1,b1,c1

{

cout<<"Values Assigned are"<<a1<<b1<<c1<<endl;

}

void draw()

{

return a*b*c;

}

};

int main()

{

triangle tri(10,12,5);

shape* sPtr = &tri;

cout<<"Output is :"<<sPtr->draw()<<endl;

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote