0% found this document useful (0 votes)
17 views3 pages

Practical 2 Constructor

Uploaded by

027HARSHA PATIL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

Practical 2 Constructor

Uploaded by

027HARSHA PATIL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CO207U Object Oriented Programming Lab

Government College of Engineering, Jalgaon


(An Autonomous Institute of Government of Maharashtra)

PRN: ____________________ Name of the Student:________________________________

Class :_________ Semester :__________ Session:________________________

Subject Teacher: Shrutika Mahajan Subject Coordinator:

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:

The Class Constructor

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.

C++ Default Constructor

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;
};

Prepared By: Mrs. Shrutika S.Mahajan


CO207U Object Oriented Programming Lab

C++ Parameterized Constructor

A constructor which has parameters is called parameterized constructor. It is used to provide


different values to distinct objects. Let's see the simple example of C++ Parameterized Constructor.
C++ Copy Constructor
A Copy constructor is an ​overloaded​ constructor used to declare and initialize an object from
another object.

Copy Constructor is of two types:


o Default Copy constructor:​ The compiler defines the default copy constructor. If the user
defines no copy constructor, compiler supplies its constructor.
o User Defined constructor:​ The programmer defines the user-defined constructor.

Syntax Of User-defined Copy Constructor:


Class_name(​const​ class_name &old_object);
For e.g.
class​ A
{
A(A &x) // copy constructor.
{
// copyconstructor.
}
}
If we have to use multiple constructors in one program,we can use it in by overloading constructors
Fore.g
Consider a class Student
class student
{
…….
……….
student() // default constructor
student(int a, int b) //parameterized constructor
student(Student &s) //copy constructor

Pseudo Code:
1. Start.
2. Declare the class Multiplication
3. Define the constructors Multiplication() as a default constructor and Multiplication(int a,int

Prepared By: Mrs. Shrutika S.Mahajan


CO207U Object Oriented Programming Lab

b) as a parameterised constructor in class with body.


4. define a common method display() to show result of multiplication in both constructors
5. In main function create an objects of a class Multiplication class
a. Multiplication m1 for default constructor.
b. Multiplication m2(12.10) for parameterised constructor.
c. Call a display method to show results
6. Stop

Conclusion:

-------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------

Sign of Teacher

Prepared By: Mrs. Shrutika S.Mahajan

You might also like