"point class in c++" Code Answer's

You're definitely familiar with the best coding language C++ that developers use to develop their projects and they get all their queries like "point class in c++" answered properly. Developers are finding an appropriate answer about point class in c++ related to the C++ coding language. By visiting this online portal developers get answers concerning C++ codes question like point class in c++. Enter your desired code related query in the search bar and get every piece of information about C++ code related question on point class in c++. 

point class in c++

By mr.salehimr.salehi on Apr 27, 2021
/*
  Examples of classes, inheritance, overloading, overriding,   
  static binding, and dynamic binding
  with classes Point2D and Point3D.

  Not an example of polymorphism.
*/

#include <iostream>
#include <cmath>

using namespace std;

/*
  Base class Point2D represents a point in two dimensional space
*/
class Point2D {
  
public:
  
  /*
    Static data members have to be initialized when declared.
    Static data memebers are usually declared const (constant), 
    because the values should not be changed (Principle of Least Privilege)
  */
  static const double ZERO = 0.0;
  
  /*
    Overloaded constructor with zero parameters
  */
  Point2D(){
    //initialize the data members
    x = ZERO;
    y = ZERO;
    //cout<<*this<<endl;
  }
  
  /*
    Overloaded constructor with two parameters
  */
  Point2D(double x2, double y2){
    //initialize the data members
    x = x2;
    y = y2;
    //cout<<*this<<endl;
  }
   
  //set (mutator) functions
  void setX(double x2){
    x = x2;
  }

  void setY(double y2){
    y = y2;
  }

  //get (accessor) functions   
  double getX(){
    return x;

  }

  double getY(){
    return y;
  }

  /*
    Returns the distance from point (0,0).
    Can be the magnitude of a vector or length of a line.
  */
  double distance(){
    return sqrt(x*x + y*y);
  }

  //friend function for overloading output operator<<()
  friend ostream & operator<<(ostream & output, const Point2D & point){
    output<<"("<<point.x<<", "<<point.y<<")";
    return output;
  }                    

  //data members represent two points in space
protected:
  double x; 
  double y;      
};

/*
  Derived class Point3D represents a podouble in three dimensional space
*/
class Point3D:public Point2D {
  
public:
  /*
    Overloaded constructor with zero parameters
  */
  Point3D():Point2D(){
    //initialize data member
    z = ZERO;
    //cout<<*this<<endl;
  }
  
  /*
    Overloaded constructor with three parameters
  */
  Point3D(double x2, double y2, double z2):Point2D(x2,y2){
    //initialize data member
    z = z2;
    //cout<<*this<<endl;
  } 
  
  //set (mutator) functions
  void setZ(double z2){
    z = z2;
  }

  //get (accessor) functions       
  double getZ(){
    return z;
  }

  //overridden function, returns the distance from point (0,0,0)
  double distance(){
    return sqrt(x*x + y*y + z*z);
  }

  //friend function for overloading output operator<<()
  friend ostream & operator<<(ostream & output, const Point3D & point){
    output<<"("<<point.x<<", "<<point.y<<", "<<point.z<<")";
    return output;
  }          

  /*
    Data members represent three points in space.
    Points x and y are inherited from Point2D.
  */
protected:
  double z;                   
};

//driver function to test classes
int main(){
  Point3D point(3, 4, 5);
  cout<<"point = "<<point<<endl;
  //function distance() returns 7.07107
  cout<<point.distance()<<endl;
  
  //pointer2D points to point
  Point2D *pointer2D = &point; 
  cout<<"*pointer2D = "<<*pointer2D<<endl;
  //function distance() incorrectly returns 5.0
  cout<<pointer2D->distance()<<endl;
  
  //pointer3D points to point
  Point3D *pointer3D = &point; 
  cout<<"*pointer3D = "<<*pointer3D<<endl;
  //function distance() returns 7.07107
  cout<<pointer3D->distance()<<endl;
   
  return 0;
}

/*
point = (3, 4, 5)
7.07107
*pointer2D = (3, 4)
5
*pointer3D = (3, 4, 5)
7.07107

*/



Add Comment

0

All those coders who are working on the C++ based application and are stuck on point class in c++ can get a collection of related answers to their query. Programmers need to enter their query on point class in c++ related to C++ code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about point class in c++ for the programmers working on C++ code while coding their module. Coders are also allowed to rectify already present answers of point class in c++ while working on the C++ language code. Developers can add up suggestions if they deem fit any other answer relating to "point class in c++". Visit this developer's friendly online web community, CodeProZone, and get your queries like point class in c++ resolved professionally and stay updated to the latest C++ updates. 

C++ answers related to "point class in c++"

View All C++ queries

C++ queries related to "point class in c++"

2D point class in c++ point class in c++ how to get the player view point location and rotation in ue4 c++ Runtime Error: Runtime ErrorFloating-point exception (SIGFPE check if point is left or right of vector distance from point to line print numbers after decimal point c++ passing the value to base class constructor from derived class c++ calling base class function from derived class object Write a C++ program using class and objects. You have to define multiple-member functions outside class and all those functions will be the same name class friend to another class syntax is not a nonstatic data member or base class of class c++ struct vs class how to fix class friendship errors in c++ arguments to a class instance c++ binary search tree in cpp using class class is replace by structure c++ logger class example c++ call method in same class matrix class in c++ c++ class member initialization declaring instance of class c++ c++ class method example defining class in other file in c++ c++ class constructor static in class c++ c++ thread incide class c++ remove class from vector c++ class member initializer list nested class in c++ cpp class constructor vector remove class c++ extend class static class in C++ cpp how to create an object of template class how to write a class in c++ what is abstract class in c++ abstract class in c++ worker class c++ Using functions in Class c++ class template create class instance c++ c++ class inheritance cpp make class abstract of c++ bind class member function C++ pointer to base class Turn the bank details struct into a class cpp nested class in c, is class uppercase or lowercase linked list in c++ using class insert delete display in array how initilaize deffult value to c++ class volume of shapes using class and operator overload TIME CLASS cpp class access array member by different name c++ argument list for class template is missing c++ final class class cpp student class in c++ c++ public class declaration C++ pointer to incomplete class type is not allowed unreal compare two functions in a class c++ c++ how to inherit from a template class inline in class in C++ new class * [] c++ C++ class linked list class c++ basic implementation vector in c++ class Can you add a constructor to an abstract class c++ How many functions (methods) can a class have? constructor derived class c++ can derived class access private members friend class c++ how to shorten code using using c++ in class with typename

Browse Other Code Languages

CodeProZone