0% found this document useful (0 votes)
27 views5 pages

Parameterized Constructor

The document contains C++ programs demonstrating the use of parameterized constructors, constructor overloading, and copy constructors through classes such as Employee, Box, and Person. Each program includes the definition of the class, constructors, and member functions to display details or calculate volume. The main function creates objects and showcases the functionality of the constructors implemented.

Uploaded by

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

Parameterized Constructor

The document contains C++ programs demonstrating the use of parameterized constructors, constructor overloading, and copy constructors through classes such as Employee, Box, and Person. Each program includes the definition of the class, constructors, and member functions to display details or calculate volume. The main function creates objects and showcases the functionality of the constructors implemented.

Uploaded by

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

 Parameterized Constructor:

Question: Write a C++ program that defines a class Employee with attributes name, id, and salary.
Implement a parameterized constructor to initialize these attributes. Create an object of the class
and display the details of the employee using a member function.

Code:-

#include <iostream>

#include <string>

using namespace std;

class Employee {

private:

string name;

int id;

float salary;

public:

// Parameterized constructor

Employee(string empName, int empId, float empSalary) {

name = empName;

id = empId;

salary = empSalary;

// Member function to display employee details

void displayDetails() {

cout << "Employee Details:" << endl;

cout << "Name: " << name << endl;

cout << "ID: " << id << endl;

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

};
int main() {

// Creating an object of the Employee class using the parameterized constructor

Employee emp("John Doe", 101, 50000.50);

// Displaying employee details

emp.displayDetails();

return 0;

 Constructor Overloading:

o Question: Create a C++ program to demonstrate constructor overloading using a


class box. Implement three constructors:

 A default constructor that sets dimensions to 1.

 A parameterized constructor that accepts length, width, and height.

 A copy constructor that creates a new Box object from an existing one.
Display the volume of the box for each constructor call.

Code:-

#include <iostream>

using namespace std;

class Box {

private:

double length, width, height;

public:

// Default constructor

Box() {

length = width = height = 1;

cout << "Default constructor called. ";

// Parameterized constructor
Box(double l, double w, double h) {

length = l;

width = w;

height = h;

cout << "Parameterized constructor called. ";

// Copy constructor

Box(const Box &other) {

length = other.length;

width = other.width;

height = other.height;

cout << "Copy constructor called. ";

// Member function to calculate and display volume

void displayVolume() {

double volume = length * width * height;

cout << "Box dimensions: " << length << " x " << width << " x " << height

<< ", Volume: " << volume << endl;

};

int main() {

// Default constructor

Box box1;

box1.displayVolume();

// Parameterized constructor

Box box2(3, 4, 5);

box2.displayVolume();
// Copy constructor

Box box3 = box2; // or Box box3(box2);

box3.displayVolume();

return 0;

 Copy Constructor:

Question: Implement a C++ program with a class Person that has attributes name and age. Use a
copy constructor to copy the data from one object to another. Demonstrate the use of the copy
constructor by creating a new object from an existing object and displaying the details.

#include <iostream>

#include <string>

using namespace std;

class Person {

private:

string name;

int age;

public:

// Parameterized constructor

Person(string personName, int personAge) {

name = personName;

age = personAge;

// Copy constructor

Person(const Person &other) {

name = other.name;

age = other.age;

}
// Member function to display person details

void displayDetails() {

cout << "Name: " << name << ", Age: " << age << endl;

};

int main() {

// Create an object using the parameterized constructor

Person person1("Alice", 30);

// Create another object using the copy constructor

Person person2 = person1;

// Display the details of both objects

cout << "Person 1 details:" << endl;

person1.displayDetails();

cout << "Person 2 details (copied from Person 1):" << endl;

person2.displayDetails();

return 0;

You might also like