Expt 4 Oops
Expt 4 Oops
Expt 4 Oops
Program 1: Write a program to implement a class ‘Box’ with data members (Length, Breadth,
Height). Include different constructors to initialize data members and also include
members functions to compute surface area and volume of each box objects
#include<iostream>
using namespace std;
class Box
{
private:
double length;
double breadth;
double height;
public:
Box() : length(0), breadth(0), height(0) {}
double getSurfaceArea()
{
return 2*(length*breadth+breadth*height+height*length);
}
double getVolume()
{
return length * breadth * height;
}
void display()
{
cout<<"Length: "<<length<<", Breadth: "<<breadth<<", Height: "<<height<<std::endl;
}
};
int main()
{
Box box1;
cout<<"Box1 (Default Constructor):"<<endl;
box1.display();
cout<<"Surface Area: "<<box1.getSurfaceArea()<<endl;
cout<<"Volume: "<<box1.getVolume()<<endl;
cout<<endl;
Box box3(box2);
cout<<"Box3 (Copy Constructor):"<<endl;
box3.display();
cout<<"Surface Area: "<<box3.getSurfaceArea()<<endl;
cout<<"Volume: "<<box3.getVolume()<<endl;
return 0;
}
OUTPUT:
Box1 (Default Constructor):
Length: 0, Breadth: 0, Height: 0
Surface Area: 0
Volume: 0
public:
Date():day(1), month(1), year(2000) {}
void display()
{
cout<<setw(2)<<setfill('0')<<day<<"-"<<setw(2)<<setfill('0')<<month<<"-"<<year<<endl;
}
};
int main()
{
Date date1;
cout<<"Date1 (Default Constructor): ";
date1.display();
Date date3(date2);
cout<<"\nDate3 (Copy Constructor): ";
date3.display();
return 0;
}
OUTPUT:
Date1 (Default Constructor): 01-01-2000
double volumeCylinder()
{
return M_PI*radius*radius*height;
}
double volumeCone()
{
return (1.0/3.0)*M_PI*radius*radius*height;
}
void display()
{
cout<<"Radius: "<<radius<<", Height: "<<height<<endl;
}
};
int main()
{
Solid solid1;
cout<<"Solid1 (Default Constructor):"<<endl;
solid1.display();
cout<<"Cylinder Volume: "<<solid1.volumeCylinder()<<endl;
cout<<"Cone Volume: "<<solid1.volumeCone()<<endl;
cout<<endl;
Solid solid3(2.0);
cout<<"Solid3 (Default Argument for Radius):"<<endl;
solid3.display();
cout<<"Cylinder Volume: "<<solid3.volumeCylinder()<<endl;
cout<<"Cone Volume: "<<solid3.volumeCone()<<endl;
return 0;
}
OUTPUT:
Solid1 (Default Constructor):
Radius: 1, Height: 1
Cylinder Volume: 3.14159
Cone Volume: 1.0472
public:
Student() : name("Unknown"), age(0), grade("Not Assigned") {}
void display()
{
cout<<"Name: "<<name<<"\nAge: "<<age<<"\nGrade: "<<grade<<endl;
}
};
int main()
{
Student student1;
cout<<"Student1 (Default Constructor):"<<endl;
student1.display();
cout<<endl;
Student student3(student2);
cout<<"Student3 (Copy Constructor):"<<endl;
student3.display();
cout<<endl;
return 0;
}
OUTPUT:
Student1 (Default Constructor):
Name: Unknown
Age: 0
Grade: Not Assigned
public:
ComplexNos(double r, double i) : real(r), imag(i) {}
void display()
{
cout<<real;
if(imag>=0)
{
cout<<" + "<<imag<<"i"<<endl;
}
else
{
cout<<" - "<<-imag<<"i"<<endl;
}
}
};
int main()
{
ComplexNos c1(3.0, 4.0);
ComplexNos c2(1.5, 2.5);
ComplexNos c3(c1);
cout<<"Complex Number 3 (Copy of Complex Number 1): ";
c3.display();
ComplexNos sum=c1.add(c2);
cout<<"Sum: ";
sum.display();
ComplexNos diff=c1.subtract(c2);
cout<<"Difference: ";
diff.display();
return 0;
}
OUTPUT:
Complex Number 1: 3 + 4i
Complex Number 2: 1.5 + 2.5i
Complex Number 3 (Copy of Complex Number 1): 3 + 4i
Sum: 4.5 + 6.5i
Difference: 1.5 + 1.5i
Program 6: Write a program demonstrating use of destructors.
#include<iostream>
using namespace std;
class Sample
{
private:
int id;
public:
Sample(int i) : id(i)
{
cout<<"Constructor called for object with id: "<<id<<endl;
}
~Sample()
{
cout<<"Destructor called for object with id: "<<id<<endl;
}
void display()
{
cout<<"Object id: "<<id<<endl;
}
};
void createObject()
{
Sample obj(2);
obj.display();
}
int main()
{
Sample obj1(1);
obj1.display();
createObject();
return 0;
}
OUTPUT:
Constructor called for object with id: 1
Object id: 1
Constructor called for object with id: 2
Object id: 2
Destructor called for object with id: 2
Constructor called for object with id: 3
Object id: 3
Destructor called for object with id: 3
Destructor called for object with id: 1