0% found this document useful (0 votes)
12 views

C++ Constructor

Uploaded by

ketankumar2614
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

C++ Constructor

Uploaded by

ketankumar2614
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

C++ Constructor

Raj Kumar
C++ Constructor
1. In C++, constructor is a special method which is
invoked automatically at the time of object creation.
2. It is used to initialize the data members of new object
generally.
3. The constructor in C++ has the same name as class or
structure.
4. In brief, A particular procedure called a constructor is
called automatically when an object is created in C++.
5. In C++, the class or structure name also serves as the
constructor name.
6. When an object is completed, the constructor is
called.
Characteristics of constructor functions
1. Constructors are invoked automatically when
we create objects.
2. Constructors should be declared in the public
section of the Class.
3. Constructors cannot return values to the calling
program because they do not have return types.
4. Constructors cannot be inherited because they
are called when objects of the class are created.
The Constructors prototype looks like this:

• <class-name> (list-of-parameters);

Example :-
class Wall
{
public:
Wall() // create a constructor
{ // code
}
};
C++ Constructor
1. Default Constructor
• A constructor with no arguments (or
parameters) in the definition is a default
constructor. Usually, these constructors use to
initialize data members (variables) with real
values.
• Note: If no constructor is explicitly declared, the
compiler automatically creates a default
constructor with no data member (variables) or
initialization.
1. Default Constructor
class CLASS_NAME
{
………
public :
CLASS_NAME() //Default constructor
{
......
}

//other member functions


};
2. Parameterized Constructor
 Parameters (or arguments) in the constructor
definition and declaration.
 More than one argument can also pass
through a parameterized constructor.
2. Parameterized Constructor
class class_Name
{
public:
class_Name(datatype variable) //Parameterized
constructor
{
………………..
}
};
#include <iostream>
Program
// class name: Rectangle
class Rectangle {
private:
double length;
double breadth;
public:
// parameterized constructor
Rectangle(double l, double b) {
length = l;
breadth = b;
}
double calculateArea() {
return length * breadth;
}
};
void main() {
// create objects to call constructors
Rectangle obj1(10,6);
Rectangle obj2(13,8);
cout << "Area of Rectangle 1: " << obj1.calculateArea();
cout << "Area of Rectangle 2: " << obj2.calculateArea();
}
3. Copy Constructor
 A copy constructor is a member function that
initializes an object using another object of the
same class.
 In simple terms, a constructor which creates an
object by initializing it with an object of the same
class, which has been created previously is known
as a copy constructor.
 Copy constructor is used to initialize the members
of a newly created object by copying the members
of an already existing object.
3. Syntax

Sample(Sample &t)
{
id=t.id;
}
#include<iostream>
#include<string.h>
class student
{
int rno;
char name[50];
double fee;
public:
student(int,char[],double);
student(student &t) //copy constructor (member wise initialization)
{
rno=t.rno;
strcpy(name,t.name);

}
void display();
void disp()
{
cout<<endl<<rno<<"\t"<<name;
}

};
student::student(int no, char n[],double f)
{
rno=no;
strcpy(name,n);
fee=f;
}

void student::display()
{
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
}

int main()
{
student s(1001,"Manjeet",10000);
s.display();

student manjeet(s); //copy constructor called


manjeet.disp();

return 0;
}

You might also like