Question

15. Define a class called Point. It has two private data members: x and y with int type; and three public member functions: a
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Source Code in C++:

#include <iostream>
using namespace std;

class Point
{
private:
int x,y; //instance variables
public:
Point() //default constructor to instance variables to 0
{
this->x=0;
this->y=0;
}
void setPoint(int x,int y) //setter method to set instance variables
{
this->x=(x<0?0:x);
this->y=(y<0?0:y);
}
void printPoint() //method to print instance variables
{
cout << "(" << this->x << "," << this->y << ")" << endl;
}
};

int main()
{
int n;
cout << "Enter the number of points: "; //input prompt
cin >> n; //input
Point *p=new Point[n]; //dynamically declaring an array of Points
for(int i=0;i<n;i++)
{
int x,y;
cout << "Enter two integers: "; //input prompt
cin >> x >> y; //input
(p+i)->setPoint(x,y); //setting the Point
}
cout << "The points you entered are: " << endl;
for(int i=0;i<n;i++)
{
cout << "Point " << i+1 << ": ";
(p+i)->printPoint(); //printing the Point
}
return 0;
}

Output:

CAUsers Debjit GDocuments pointcpp.exe Enter the nunber of points: 3 nter two integers: 3 2 nter tvo integers: 2-2 nter tuo i

Add a comment
Know the answer?
Add Answer to:
15. Define a class called Point. It has two private data members: x and y with int type; and three public member functions: a constructor, setPoint(int, int) and printPoint0. The constructor init...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Define a class named COMPLEX for complex numbers, which has two private data members of type...

    Define a class named COMPLEX for complex numbers, which has two private data members of type double (named real and imaginary) and the following public methods: 1- A default constructor which initializes the data members real and imaginary to zeros. 2- A constructor which takes two parameters of type double for initializing the data members real and imaginary. 3- A function "set" which takes two parameters of type double for changing the values of the data members real and imaginary....

  • In C++, A point class has the following definition: class Point{ private: int x; int y;...

    In C++, A point class has the following definition: class Point{ private: int x; int y; public: Point(int, int); A vector has two points as a start point (x,y) and end point (x,y). Write the header file and implementation file of the Vector class containing constructor, other functions, and also a function returning length of Vector determined from start point and end point coordinates. Write also the client code.

  • Write a class called Taxicab that has three int fields (data members) to store its current...

    Write a class called Taxicab that has three int fields (data members) to store its current x- and y-coordinates and the total distance it has driven so far (the actual distance driven by the Taxicab, not the Euclidean distance from it's starting point). The class should have a constructor that takes two parameters and uses them to initialize the coordinates, and also initializes the distance traveled to zero. The class should have a default constructor that sets all three fields...

  • skeleton: import java.util.*; public class Point3D{ private int x,y,z; Point3D(int a, int b, int c){ x = a; y = b; z = c; } public int x(){ return x; } public int y(){ return y; } public in...

    skeleton: import java.util.*; public class Point3D{ private int x,y,z; Point3D(int a, int b, int c){ x = a; y = b; z = c; } public int x(){ return x; } public int y(){ return y; } public int z(){ return z; } public String toString(){ return "("+x+","+y+","+z+")"; } } Question1Test.java import java.util.*; public class Question1Test { public static void main(String[] args){ MyHashList<Point3D> lst = new MyHashList<Point3D>(1000); } } a) A class Point3D is given. Re-write this class so that...

  • Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the c...

    C++ Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the class point that should have the following Private data members x and y (of type int), with default values of 0 A constant ID of type int A private static integer data member named numOfPoints This data member should be o Incremented whenever a new point object is created. o Decremented whenever a point object is destructed. A default constructor An...

  • public class File_In_36 {    private String filename;    public int[][] matrix()    {       //...

    public class File_In_36 {    private String filename;    public int[][] matrix()    {       // 1. matrix will call scan_File to determine whether or not the file       // name entered by the user actually exists       // 2. matrix will call size_matrix to determine the number of integers       // in the input file and set the instance variable matrix_size to that       // number       // 3. matrix will call check_square to determine whether or not matrix_size...

  • Write a class named RowSort which has the following members. Private: (1) double matrix 0O (a...

    Write a class named RowSort which has the following members. Private: (1) double matrix 0O (a 2D array): (2) int columns; (3) int rows Public: (1) A default constructor that creates a 2D array, 8 6 4 (2) A constructor that takes three values for the three private members, and creates a 2D array accordingly. (3) The "get" and "set" methods for the three private members. (4) A method that displays a 2D array, stored in the private member matrix,...

  • What is wrong with the following code: class Point { private : int x, y; public...

    What is wrong with the following code: class Point { private : int x, y; public : Point (int u, int v) : x(u), y(v) {} int getX () { return x; } int getY () { return y; } void setX (int newX ) const { x = newX ; } }; int main () { Point p(5, 3); p.setX (9001) ; cout << p. getX () << ’ ’ << p. getY (); return 0; }

  • C++ PROGRAMMING: Define a class called Date that has 3 private members that represent the month,...

    C++ PROGRAMMING: Define a class called Date that has 3 private members that represent the month, day, and year respectively. They will be represented as integers. Include a constructor and a public void function called print that prints out the date. (For example, if the month is 2, the day is 25, and the year is 1946, print provides as output 2/25/1946.) Include the class in a program that prints out the date 2/25/1946. Also, create another function that will...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT