Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Assignment 10

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

ASSIGNMENT -10

Problem Statement:
Study of Constructors and Destructors in
C++

Study of Default Constructor

#include<iostream>

using namespace std;

class Employee {

public:

int age;

// Default constructor.

Employee() {

/* Data member is defined with the help of the

default constructor.*/

age = 50;

};

int main() {

// Object of class Employee declared.

Employee e1;

// Prints value assigned by default constructor.


cout << e1.age;

return 0;

Output

STUDY OF PARAMETERIZED CONSTRUCTOR

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

programmers assign varied initial values to an object at the creation time.

#include <iostream>

using namespace std;

class Employee {

public:
int age;

// Parameterized constructor

Employee(int x) {

/* Age assigned to value passed as an argument

while object declaration.*/

age = x;

};

int main() {

/* Object c1 declared with argument 40, which

gets assigned to age.*/

Employee c1(40);

Employee c2(30);

Employee c3(50);

cout << c1.age << "\n";

cout << c2.age << "\n";

cout << c3.age << "\n";

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

object is passed as a parameter

#include<iostream>

using namespace std;

class Employee {

private:

// Data members

int salary, experience;

public:

// Parameterized constructor

Employee(int x1, int y1) {

salary = x1;
experience = y1;

// Copy constructor

Employee(Employee &new_employee) {

salary = new_employee.salary;

experience = new_employee.experience;

void display() {

cout << "Salary: " << salary << endl;

cout << "Years of experience: " << experience << endl;

};

// main function

int main() {

// Parameterized constructor

Employee employee1(34000, 2);

// Copy constructor

Employee employee2 = employee1;

cout << "Employee1 using parameterized constructor : \n";

employee1.display();

cout << "Employee2 using copy constructor : \n";

employee2.display();

return 0;

Output
Implementation of Constructors and Destructors in C++

#include <iostream>

using namespace std;

class Department {

public:

Department() {

// Constructor is defined.

cout << "Constructor Invoked for Department class" << endl;

~Department() {

// Destructor is defined.

cout << "Destructor Invoked for Department class" << endl;

};
class Employee {

public:

Employee() {

// Constructor is defined.

cout << "Constructor Invoked for Employee class" << endl;

~Employee() {

// Destructor is defined.

cout << "Destructor Invoked for Employee class" << endl;

};

int main(void) {

// Creating an object of Department.

Department d1;

// Creating an object of Employee.

Employee e2;

return 0;

Output
C++ Constructor Overloading Example

#include <iostream>

using namespace std;

class ABC

private:

int x,y;

public:

ABC () //constructor 1 with no arguments

x = y = 0;

ABC(int a) //constructor 2 with one argument

x = y = a;

ABC(int a,int b) //constructor 3 with two argument

x = a;
y = b;

void display()

cout << "x = " << x << " and " << "y = " << y << endl;

};

int main()

ABC cc1; //constructor 1

ABC cc2(10); //constructor 2

ABC cc3(10,20); //constructor 3

cc1.display();

cc2.display();

cc3.display();

return 0;

} //end of program

Output
#include <iostream>

using namespace std;

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

};

int main() {

// create objects to call constructors

Rectangle obj1(10,6);

Rectangle obj2(13,8);

cout << "Area of Rectangle 1: " << obj1.calculateArea()<<endl;

cout << "Area of Rectangle 2: " << obj2.calculateArea();

return 0;

}
Output

Modify the program with Copy Constructor

#include <iostream>

using namespace std;

// class name: Rectangle

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() {

return length * breadth;

};

int main() {

// create objects to call constructors

Rectangle obj1(10,6);

Rectangle obj2(13,8);

Rectangle obj3 = obj1;

cout << "Area of Rectangle 1: " << obj1.calculateArea()<<endl; cout << "Area of Rectangle 2: "

<< obj2.calculateArea()<<endl; cout << "Area of Rectangle 3: " << obj3.calculateArea();

return 0;

Output

You might also like