Lab Report Oop 4
Lab Report Oop 4
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;
}
void displayDistance() {
cout << "Distance : " << feet << " feet " << inches << " inches" << endl;
}
};
int main() {
Distance d1;
d1.displayDistance();
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;
}
int main() {
Number original(30);
Number copy = original;
cout << "Original Number: " << original.num << endl;
cout << "Copied Number: " << copy.num << endl;
return 0;
}
Output:-