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

Expt 4 Oops

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

PROGRAMS:

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

Box(double l, double b, double h) : length(l), breadth(b), height(h) {}

Box(const Box &box) : length(box.length), breadth(box.breadth), height(box.height) {}

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 box2(3.0, 4.0, 5.0);


cout<<"Box2 (Parameterized Constructor):"<<endl;
box2.display();
cout<<"Surface Area: "<<box2.getSurfaceArea()<<endl;
cout<<"Volume: "<<box2.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

Box2 (Parameterized Constructor):


Length: 3, Breadth: 4, Height: 5
Surface Area: 94
Volume: 60

Box3 (Copy Constructor):


Length: 3, Breadth: 4, Height: 5
Surface Area: 94
Volume: 60
Program 2: Write a program to implement class ‘Date’ with data members(date,month,year).
Include different constructors to initialize data members and also include members
functions to display each date object in ‘dd-mm-yyyy’ format.
#include<iostream>
#include<iomanip>
using namespace std;
class Date
{
private:
int day;
int month;
int year;

public:
Date():day(1), month(1), year(2000) {}

Date(int d, int m, int y) : day(d), month(m), year(y) {}

Date(const Date &date) : day(date.day), month(date.month), year(date.year) {}

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 date2(28, 7, 2024);


cout<<"\nDate2 (Parameterized Constructor): ";
date2.display();

Date date3(date2);
cout<<"\nDate3 (Copy Constructor): ";
date3.display();

return 0;
}

OUTPUT:
Date1 (Default Constructor): 01-01-2000

Date2 (Parameterized Constructor): 28-07-2024

Date3 (Copy Constructor): 28-07-2024


Program 3: Write a program to implement a class ‘Solid’ with data members (radius,height).
Include different constructors to initialize data members and also include members
functions to compute volume of cylinder and cone. Include default argument for the
data member ‘radius’.
#include<iostream>
#include<cmath>
using namespace std;
class Solid
{
private:
double radius;
double height;
public:
Solid(double r=1.0, double h=1.0):radius(r), height(h) {}

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 solid2(3.0, 5.0);


cout<<"Solid2 (Parameterized Constructor):"<<endl;
solid2.display();
cout<<"Cylinder Volume: "<<solid2.volumeCylinder()<<endl;
cout<<"Cone Volume: "<<solid2.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

Solid2 (Parameterized Constructor):


Radius: 3, Height: 5
Cylinder Volume: 141.372
Cone Volume: 47.1239

Solid3 (Default Argument for Radius):


Radius: 2, Height: 1
Cylinder Volume: 12.5664
Cone Volume: 4.18879
Program 4: Write a program to print the details of students by creating a Student class. If no
data is passed while creating an object of the Student class, then default values should be
assigned. Include different constructors to initialize data members and also include
members functions to display student details
#include<iostream>
#include<string>
using namespace std;
class Student
{
private:
string name;
int age;
string grade;

public:
Student() : name("Unknown"), age(0), grade("Not Assigned") {}

Student(string n, int a, string g) : name(n), age(a), grade(g) {}

Student(const Student &student) : name(student.name), age(student.age),


grade(student.grade) {}

void display()
{
cout<<"Name: "<<name<<"\nAge: "<<age<<"\nGrade: "<<grade<<endl;
}
};
int main()
{
Student student1;
cout<<"Student1 (Default Constructor):"<<endl;
student1.display();
cout<<endl;

Student student2("Alice", 20, "A");


cout<<"Student2 (Parameterized Constructor):"<<endl;
student2.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

Student2 (Parameterized Constructor):


Name: Alice
Age: 20
Grade: A

Student3 (Copy Constructor):


Name: Alice
Age: 20
Grade: A
Program 5: Write a program to implement a class ‘Complex Nos’ with data
members(real,imag). Include parameterised and copy constructors to initialize data members
and also include members functions to compute and display the sum and difference of
complex nos.
#include<iostream>
using namespace std;
class ComplexNos
{
private:
double real;
double imag;

public:
ComplexNos(double r, double i) : real(r), imag(i) {}

ComplexNos(const ComplexNos &c) : real(c.real), imag(c.imag) {}

ComplexNos add(const ComplexNos &c)


{
return ComplexNos(real+c.real, imag+c.imag);
}

ComplexNos subtract(const ComplexNos &c)


{
return ComplexNos(real-c.real, imag-c.imag);
}

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

cout<<"Complex Number 1: ";


c1.display();
cout<<"Complex Number 2: ";
c2.display();

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

Sample* obj2 = new Sample(3);


obj2->display();
delete obj2;

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

You might also like