Problem 1. 1. Create a new C++ project, named hw3. 2. Design a class Shape that
ID: 3803115 • Letter: P
Question
Problem 1.
1. Create a new C++ project, named hw3.
2. Design a class Shape that stores x and y coordinates of a shape. Write the classdeclaration in the header file (.h) and the class definition in the implementation
file (.cpp). Your class must have the following public methods: • constructor with two arguments for x and y• draw – prints statement, such as “drawing a shape with coordinates (2.3)”• print – prints statement, such as “this is a shape with coordinates (2.3)”
3. Create a main.cpp source file with int main() function.
4. Write test code to create three objects of type Shape. Call both draw and print
method on each object.
5. Create pointers for each of your three objects.
6. Call both draw and print function on each object using these pointers with the ->operator.Your program should produce an output similar to the following:
calling the draw() function:
drawing a shape with coordinates (10, 24)
drawing a shape with coordinates (4, 2)
drawing a shape with coordinates (-5, 8)
calling the print() function:
printing a shape with coordinates (10, 24)
printing a shape with coordinates (4, 2)
printing a shape with coordinates (-5, 8)
USING POINTERS:calling the draw() function:
drawing a shape with coordinates (10, 24)
drawing a shape with coordinates (4, 2)
drawing a shape with coordinates (-5, 8)
calling the print() function:
printing a shape with coordinates (10, 24)
printing a shape with coordinates (4, 2)
printing a shape with coordinates (-5, 8)
Explanation / Answer
Design a class Shape that stores x and y coordinates of a shape.
#include<iostream.h>
Class Shape
{
private:
int x;
int y;
public:
Shape(int x1, int y1) // Here is the initialization of Constructor
{
X=x1; y=y1;
}
void draw(int x1, int y1)
{
cout<<” drawing a shape with coordinates (“<<x1+”,”<<y1+”)”;
}
Void print(int x1, int y1)
{
cout<<” printing a shape with coordinates (“<<x1+”,”<<y1+”)”;
}
}
int main()
{
Shape s1(10,24);
Shape s2(4,2);
Shape s3(-5,8);
Shape* s1ptr(10,24);
Shape* s2ptr(4,2);
Shape* s3ptr(-5,8);
cout<<” calling the draw() function:”<<endl;
S1.draw(); s2.draw(); s3.draw();
cout<<” calling the print() function:”<<endl;
s1.print(); s2.print(); s3.print();
cout<<”USING POINTERS:calling the draw() function:”<<endl;
s1ptr->draw(); s2ptr->draw(); s2ptr->draw();
cout<<”USING POINTERS:calling the print() function:”<<endl;
s1ptr-> print (); s2ptr-> print (); s2ptr-> print ();
return();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.