0% found this document useful (0 votes)
5 views13 pages

UNIT-IV

The document explains constructors in C++, which are special member functions that initialize class objects and are automatically called upon object creation. It describes three types of constructors: default, parameterized, and copy constructors, providing examples for each. Additionally, it includes code snippets demonstrating their usage and functionality.

Uploaded by

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

UNIT-IV

The document explains constructors in C++, which are special member functions that initialize class objects and are automatically called upon object creation. It describes three types of constructors: default, parameterized, and copy constructors, providing examples for each. Additionally, it includes code snippets demonstrating their usage and functionality.

Uploaded by

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

BLOCK-1

UNIT-4
CONSTRUCTOR

A constructor is a special type of member function of a


class which initializes objects of a class. In C++,
Constructor is automatically called when object(instance
of class) create. It is special member function of the class
because it does not have any return type.

~ tilde
E.g
#include<iostream.h>
class c
{ c()
{
public:
n=10;
cout<<n;
}
};
void main()
{
c obj;
}
TYPES OF CONSTRUCTOR
1) Default Constructor: that has no parameters, or if it has a
parameters, all the parameters have default values.
2) Parameterized Constructor: can take the arguments .
3) Copy Constructor
Default Constructor
1) Default Constructor: that has no parameters, or if it has a parameters, all the
parameters have default values.
Class student
{
int roll_no;
Public:
student(); //no parameters
Student::student()
{
-------
--------
};
Student s;
Parameterized Constructor

Parametrized constructor is a special type of


constructor where an object is created, and
further parameters are passed to distinct
objects.
Syntax of Parameterized Constructor
class class_name
{
Access Specifier: Member - Variables
Member - Functions
public:
class_name(variables)
{
// Constructor code
}
//... other Variables & Functions }
#include <iostream>
using namespace std;
class ParamA
{
private:
int b, c;
public:
ParamA (int b1, int c1) //passing parameters
{
b = b1;
c = c1;
}
int getX ()
{
return b;
}
int getY ()
{
return c;
}
};
int main ()
{
ParamA p1(10, 15);
cout << "p1.b = " << p1. getX() << ", p1.c = " << p1.getY();
return 0;
}
The copy constructor in C++ is used to copy data of one object to another.
// declare a class
class Wall
{
private:
double length;
double height;
public:
// initialize variables with parameterized constructor
Wall(double len, double hgt)
{
length = len;
height = hgt; }
// copy constructor with a Wall object as parameter
// copies data of the obj parameter
Wall(Wall &obj)
{
length = obj.length;
height = obj.height;
}
double calculateArea() {
return length * height; }
};
int main() {
// create an object of Wall class
Wall wall1(10.5, 8.6);
// copy contents of wall1 to wall2
Wall wall2 = wall1;
// print areas of wall1 and wall2
cout << "Area of Wall 1: " << wall1.calculateArea() << endl;
cout << "Area of Wall 2: " << wall2.calculateArea();
return 0;
}
Class abc
{
public:
abc(); //constructor
{
cout<<“hello”;
}
};
Main()
{
abc ob; //call the costructor
}
Class car
{
public:
string brand;
string model;
int year;
car(string x, string y, int z)
{
brand =x;
model=y;
year=z;
}
};
Int main()
{
car ob1(“FORD”, “AA”, 2009)
car ob2(“SANTRO”, “XO”, 2022)
cout<<ob1.brand;
cout<<ob1.model;
cout<<ob1.year;
cout<<ob2.brand;
cout<<ob2.model;
cout<<ob2.year;
Return 0;
}
Class abc
Main()
{
{
int x,y;
abc ob1(11,23);
public:
abc ob1=ob2;
abc(int x1, int y1)
Cout<<“First Constructor”;
{
Ob1.disp();
x=x1;
Cout<<“Copy Constructor”
y=y1;
Ob2.disp();
}
}
Copy(abc &H)
{
Syntax
x=H.x;
Copy(constructor_name &ob)
y=H.y;
}
Void disp()
Class A
{
{
cout<<x<<y;
public:
}
~A()
};
{
cout<<“CONSTRUCTOR DESTROYED”;
}
};

You might also like