Assignment 10
Assignment 10
Assignment 10
Problem Statement:
Study of Constructors and Destructors in
C++
#include<iostream>
class Employee {
public:
int age;
// Default constructor.
Employee() {
default constructor.*/
age = 50;
};
int main() {
Employee e1;
return 0;
Output
Using the default constructor, it is impossible to initialize different objects with different initial
values. What if we need to pass arguments to constructors which are needed to initialize an
object? There is a different type of constructor called Parameterized Constructor to solve this
issue.
A Parameterized Constructor is a constructor that can accept one or more arguments. This helps
#include <iostream>
class Employee {
public:
int age;
// Parameterized constructor
Employee(int x) {
age = x;
};
int main() {
Employee c1(40);
Employee c2(30);
Employee c3(50);
return 0;
Output
Study of Copy Constructor
A Copy constructor is a type of constructor used to create a copy of an already existing object of
a class type. The compiler provides a default Copy Constructor to all the classes. A copy
constructor comes into the picture whenever there is a need for an object with the same values for
data members as an already existing object. A copy constructor is invoked when an existing
#include<iostream>
class Employee {
private:
// Data members
public:
// Parameterized constructor
salary = x1;
experience = y1;
// Copy constructor
Employee(Employee &new_employee) {
salary = new_employee.salary;
experience = new_employee.experience;
void display() {
};
// main function
int main() {
// Parameterized constructor
// Copy constructor
employee1.display();
employee2.display();
return 0;
Output
Implementation of Constructors and Destructors in C++
#include <iostream>
class Department {
public:
Department() {
// Constructor is defined.
~Department() {
// Destructor is defined.
};
class Employee {
public:
Employee() {
// Constructor is defined.
~Employee() {
// Destructor is defined.
};
int main(void) {
Department d1;
Employee e2;
return 0;
Output
C++ Constructor Overloading Example
#include <iostream>
class ABC
private:
int x,y;
public:
x = y = 0;
x = y = a;
x = a;
y = b;
void display()
cout << "x = " << x << " and " << "y = " << y << endl;
};
int main()
cc1.display();
cc2.display();
cc3.display();
return 0;
} //end of program
Output
#include <iostream>
class Rectangle {
private:
double length;
double breadth;
public:
// parameterized constructor
Rectangle(double l, double b) {
length = l;
breadth = b;
double calculateArea() {
};
int main() {
Rectangle obj1(10,6);
Rectangle obj2(13,8);
return 0;
}
Output
#include <iostream>
class Rectangle {
private:
double length;
double breadth;
public:
// parameterized constructor
Rectangle(double l, double b) {
length = l;
breadth = b;
}
//Copy Constructor
Rectangle(Rectangle &obj1)
length = obj1.length;
breadth = obj1.breadth; }
double calculateArea() {
};
int main() {
Rectangle obj1(10,6);
Rectangle obj2(13,8);
cout << "Area of Rectangle 1: " << obj1.calculateArea()<<endl; cout << "Area of Rectangle 2: "
return 0;
Output