Programm to demonstrate constructor with default arguments

Introduction 

In this code example, we have a class called "Polar" that represents a point in polar coordinates. The class has two data members: radius and angle, which represent the distance from the origin and the angle in radians, respectively. The code demonstrates the use of constructors, including a parameterized constructor with default arguments and a copy constructor.

CPP

Key Concepts

Before diving into the code, let's understand some key concepts:
  • Constructor: A constructor is a special member function of a class that is used to initialize objects of that class. It has the same name as the class and is automatically called when an object is created. Constructors can be used to set initial values to the data members of the class.
  • Parameterized Constructor: A parameterized constructor is a constructor that takes one or more parameters. It allows us to initialize the object with specific values at the time of creation.
  • Default Arguments: Default arguments are values assigned to the parameters of a function or constructor. If no argument is provided for a parameter with a default value, the default value is used.
  • Copy Constructor: A copy constructor is a special constructor that creates a new object by copying the values of another object of the same class. It is used to create a copy of an existing object.

Code Structure

The code consists of a class definition for Polar and a main function that demonstrates
#include<iostream>
using namespace std;
class Polar
{ 
    float radius;
    float angle;
    public:
    Polar(float radius=0,float angle=0) //parameterized constructor with defalut arguments
{ 
    //formal argument is same as data member
    this->radius = radius; 
    this->angle = angle; //this poiter is used to avoid conflict
    cout<<"Parameterized constructor invoked "<<endl;
}
Polar(Polar &p) //copy constructor
{ 
    radius = p.radius;
    angle = p.angle;
    cout<<"Copy constructor invoked "<<endl;
}
void display()
    { 
        cout<<"Radius : "<<radius<<endl;
        cout<<"Angle : "<<angle<<endl; 
    
    }
};
int main()
{ 
    Polar p1; //default argument parameterized constructor invoked
    p1.display();
    Polar p2(4); //default argument Parameterized constructor invoked
    p2.display();
    Polar p3(2,3); //Parameterized constructor invoked
    p3.display();
    Polar p4 = p3; //copy constructor invoked
    p3.display();
    return 0;
}
  1. The code begins with the inclusion of the iostream library, which allows us to use input/output operations.
  2. Next, we define the Polar class. It has two private data members: radius and angle.
  3. The class has two constructors: a parameterized constructor with default arguments and a copy constructor.
  4. The parameterized constructor initializes the radius and angle data members with the provided values. If no arguments are provided, the default values of 0 are used. The constructor also outputs a message to indicate that it has been invoked.
  5. The copy constructor takes a reference to another Polar object as a parameter. It copies the values of the radius and angle data members from the source object to the newly created object. Again, a message is displayed to indicate that the copy constructor has been invoked.
  6. The display function is a member function of the Polar class. It simply outputs the values of the radius and angle data members.
  7. In the main function, we create four Polar objects: p1, p2, p3, and p4.
  8. p1 is created using the default constructor, which initializes both radius and angle with the default values of 0. The display function is then called to print the values.
  9. p2 is created using the parameterized constructor with a single argument. The radius is set to 4, and the angle is set to the default value of 0. The display function is called to print the values.
  10. p3 is created using the parameterized constructor with two arguments. The radius is set to 2, and the angle is set to 3. The display function is called to print the values.
  11. p4 is created by using the copy constructor and passing p3 as the argument. The radius and angle values of p3 are copied to p4. The display function is called to print the values.
  12. Finally, the main function returns 0, indicating successful execution.

Conclusion

In this code example, we explored the concept of constructors in C++ and how they can be used to initialize objects. We learned about parameterized constructors with default arguments, which allow us to provide initial values to the object at the time of creation. We also saw the use of a copy constructor to create a new object by copying the values of an existing object. Understanding constructors is essential in object-oriented programming, as they play a crucial role in object initialization and memory allocation.
Next Post Previous Post
No Comment
Add Comment
comment url