C++ program with constructor and constant function

Today let us write a program with class, member methods and constructor.

Q:
Write a class point with two data members x and y. Write getters and setters for these. Now create an array of 10 points and print the farthest point from the origin.

Point.h
class point
{
    float x;
    float y;
public:
    point(float m=0,float n=0);
    float  getx() const;
    float  gety() const;
    void setx(const float & a);
    void sety(const float & b);
};

Point.cpp
#include"Point.h"
point::point(float a,float b):x(a),y(b)
{
}
float   point::getx() const
{
    return x;
}
float   point::gety() const
{
    return y;
}
void point::setx(const float &a)  
{
   x = a;
}
void point::sety(const float &b)  
{
   y = b;
}

main.cpp
#include<iostream>
#include"Point.h"
#include<cmath>
using std::cin;
using std::cout;
 
int main()
{
    point pobj[4];
    for(int i=0;i<4;i++)
    {
        cout<<"Enter x and y of point"<<i<<":";
        float x1,y1;
        cin>>x1>>y1;
        pobj[i].setx(x1);
        pobj[i].sety(y1);
    }
    /*find farthest point*/
    float largest_dist = -1;
    int index = -1;
    for(int i=0;i<4;i++)
     {
         float squared_dist = pobj[i].getx()*pobj[i].getx() + pobj[i].gety()*pobj[i].gety();
         float dist_from_origin = sqrt(squared_dist);
         if(dist_from_origin>largest_dist)
         {
               largest_dist = dist_from_origin;
               index = i;
         }
     }
     cout<<"The farthest point is ("<<pobj[index].getx()<<","<<pobj[index].gety()<<")";
     return 0;
}
               


Now let us try to understand the code.

Point class has x and y as private data members. Which means main() function or any other outside function can not directly access the code. So the getters and setters. getter returns the data member. And setter changes it.

Let us look at getx() and gety(). They return data members x and y respectively. x and y of which Point. The point from where the function is called. this point. If you want you can add this pointer to the code.

float   point::getx() const
{
    return x;//or return this->x;
}
float   point::gety() const
{
    return y;
}

But why are these functions constants? Because getx and gety need not and should not change x and y co-ordinate. So making the function const ensures this. In const function, if state of an object is changed, there will be a compiler error. 
 
But setx() and sety() are not const functions. Why? Because they have to change x and y.

Look at a modified main function. We have created a constant point p1 and called getx and gety on this constant object. No error. But when we call setx() on p1, there will be compiler error. Because a constant object can call only constant functions.
int main()
{
   const Point p1(10,3);
   cout<<p1.getx()<<","<<p1.gety();//no error
   float m;
   cin>>m;
   p1.setx(m);//error
   .....
}

Now let us look at the constructor

point::point(float a,float b):x(a),y(b)
{
}

The constructor is very simple. It takes two parameters a and b. And  assigns x to a and y to b using member initializer list.x(a) , y(b) is equivalent to saying x=a; and y=b; inside the constructor

Member initializer list is faster than assignments and in case of constant data members, you should use member initializer. If x is const float, the x=a; in constructor throws a compilation error.

In main function, we are creating an array of 10 points and reading their x an y using a for loop. No, we are reading a temporary variable x1 and use this in setx function.

A simple

cin>>pobj[i].x 

will throw compilation error. Because x is private. But won't it be simpler to make x and y as public and directly read and write them. NO. Because making data public violates the principle of Encapsulation and any future change in implementation affects other code which use this class.  Always make data members as private




    point pobj[4];
    for(int i=0;i<4;i++)
    {
        cout<<"Enter x and y of point"<<i<<":";
        float x1,y1;
        cin>>x1>>y1;
        pobj[i].setx(x1);

Which constructor do array elements of pobj use here? We know that if a class has any user defined constructor, then compiler does not provide default constructor.

If we look at Point.h
 

class point
{
    float x;
    float y;
public:
    point(float m=0,float n=0);

we see that parameters m and n have default values 0 in the constructor. So the 2 parameter constructor behaves like default constructor (constructor with 0 parameters) and is called when initialing array elements.

The rest of the code is simple finding a largest element in an array kind of stuff.

Comments

Popular Posts