Practical 2 Constructor
Practical 2 Constructor
Aim: Write a C++ program to calculate multiplication of two numbers; use parameterized and
default constructor.
Compiler used: We used the g++ compiler to compile the C++ program.
Operating System used: Ubuntu
Theory:
In C++, constructor is a special method which is invoked automatically at the time of object
creation. It is used to initialize the data members of new object generally. The constructor in C++
has the same name as class or structureThere can be two types of constructors in C++.
● Default constructor
● Parameterized constructor
A class constructor is a special member function of a class that is executed whenever we create
new objects of that class. A constructor will have exact same name as the class and it does not have
any return type at all, not even void. Constructors can be very useful for setting initial values for
certain member variables.
A constructor which has no argument is known as default constructor. It is invoked at the time of
creating object
For e.g.
class Line {
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor
private:
double length;
};
Pseudo Code:
1. Start.
2. Declare the class Multiplication
3. Define the constructors Multiplication() as a default constructor and Multiplication(int a,int
Conclusion:
-------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------
Sign of Teacher