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

Lab Report Oop 4

The document discusses three tasks related to constructors and classes in C++. Task 1 demonstrates use of constructors and destructors. Task 2 creates a Distance class with constructors to initialize feet and inches to zero or fixed values and a method to display distance. Task 3 copies the value of one Number object to another using a copy constructor.

Uploaded by

sahi.uqba433
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Lab Report Oop 4

The document discusses three tasks related to constructors and classes in C++. Task 1 demonstrates use of constructors and destructors. Task 2 creates a Distance class with constructors to initialize feet and inches to zero or fixed values and a method to display distance. Task 3 copies the value of one Number object to another using a copy constructor.

Uploaded by

sahi.uqba433
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Task No .

1:-
Write a C++ program to demonstrate the use of constructor and destructor.
Code:-
#include <iostream>
using namespace std;

class MyClass {
public:

MyClass() {
cout << "Constructor called:" << endl;
}

~MyClass() {
cout << "Destructor called:" << endl;
}
};

int main() {
MyClass obj;
return 0;
}

Output:-
Task No. 2:-
Create a class called distance that has a separate integer member data
for feet and inches. One constructor should initialize this data to zero
and another should initialize it to fixed values. A member function
should display it in feet inches format.

Code:-

#include <iostream>
using namespace std;

class Distance {
private:
int feet;
int inches;

public:

Distance() {
feet = 0;
inches = 0;
}

Distance(int ft, int in) {


feet = ft;
inches = in;
}

void displayDistance() {
cout << "Distance : " << feet << " feet " << inches << " inches" << endl;
}
};

int main() {

Distance d1;
d1.displayDistance();

Distance d2(10, 6);


d2.displayDistance();
return 0;
}

Output:-

Task No.3:-
Write a C++ program to copy the value of one object to another
object using copy constructor.
Code:-

#include <iostream>
using namespace std;

class Number {
public:
int num;

Number(int n) {
num = n;
}

Number(const Number& obj) {


num = obj.num;
}
};

int main() {
Number original(30);
Number copy = original;
cout << "Original Number: " << original.num << endl;
cout << "Copied Number: " << copy.num << endl;

return 0;
}

Output:-

You might also like