You are on page 1of 6

1

EXPERIMENT NUMBER - 4
STUDENT’S NAME - KAVITA NAINWAL
STUDENT’S UID – 22BAI70573
CLASS AND GROUP – 22-AML-102- A
SEMESTER - SECOND

AIMOFTHEEXPERIMENT–
4.1 : WAP to find area of rectangle using constructor overloading. Also define
destructor to delete the memory allocated to objects.
4.2 : WAP to create database of the following items: Name of the student (String),
Roll number of the student (int), Height of the student (cm), Weight of the
student (kg/gms)
1) Create a Constructor to initialize values
2) Create display () function to display the details
3) Illustrate the use of copy constructor 4) Also
implement the concept of destructor.

Algorithm of program 4.1:


 Define a class "area" with private member variables "l" and "b" representing length and
breadth, and "a" representing the area.
 Define a default constructor which initializes length and breadth to 5 and 6 respectively,
and print their values.
 Define a parameterized constructor which takes length and breadth as input arguments
and initializes the member variables accordingly.
 Define two member functions "calc()" and "print()" to calculate and print the area
respectively.
 In the main function, create an object "a1" of the class "area" using the default constructor,
and call the "calc()" and "print()" member functions on it. Then create another object "a2"
using the parameterized constructor by taking user input for length and breadth, and call
the "calc()" and "print()" member functions on it. Finally, destroy the objects using the
destructor.
2

Algorithm of program 4.2 :


 Define a class "student" with private member variables "name", "rollNo", "height", and
"weight" representing a student's details.
 Define a parameterized constructor which takes student's details as input arguments and
initializes the member variables accordingly.
 Define a member function "display()" to print the student's details.
 Define a destructor to print a message when the object is destroyed.
 In the main function, create an object "std" of the class "student" using the parameterized
constructor, and call the "display()" member function on it. Then create another object
"std1" of the class "student" using copy constructor by passing the "std" object, and call the
"display()" member function on it. Finally, destroy the objects using the destructor.

PROGRAM CODE –
// CODE OF PROGRAM 4.1
#include<iostream>
using namespace std;
class area
{ int a,l,b; public: area() // simple
constructor definition.
{ l=5;
b=6;
cout<<"Simple constructor called\n";
cout<<"length="<<l<<"\nbreadth="<<b<<endl;
} area(int x,int y) // parameterised
constructor
{ l=x; b=y; }
void calc();
void
print();
~area();
};
void area::calc()
3

{ a=l*b;
}
void area::print()
{ cout<<"Area is : "<<a<<endl;
}
area::~area()
{
cout << "Object is being deleted" << endl;
} int
main()
{ int l,b; area a1; // simple constructor is
called.
a1.calc();
a1.print();
cout<<"Enter length and breadth for
parameterised constructor:\n"; cin>>l>>b; area a2(l,b);
// parameterised constructor is called.
a2.calc();
a2.print();
return 0;
}

// CODE OF PROGRAM 4.2


#include <iostream>
using namespace std;
class student
{ private:
string name;
int rollNo;
int height;
int weight;
public:
student(string n,int r,int h,int w){
name=n;
4

rollNo=r;
height=h;
weight=w;}
//member function to print student's details
void display(void);
~student();};
void student::display(void){
cout << "Student details:\n";
cout << "Name:"<< name << ",Roll Number:" << rollNo <<
",Height:" << height << ",Weight:" << weight;} student
:: ~student()
{cout<<"destructor is called\n";}
int main(){
student std("mohit",1234,6,78); //object creation
student std1=std;
std1.display ();
return 0;
}
ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION NA
(Kindly note down the compile time errors encountered)

PROGRAMS’ EXPLANATION (in brief) –


4.1: This program defines a class "student" with member variables representing a student's
details, and creates objects of the class using parameterized and copy constructors to display
and print the student's details.
4.2: This program defines a class "area" with a default and parameterized constructor, member
functions to calculate and print the area, and a destructor; and creates objects of the class using
both constructors to display the area of a rectangle.

OUTPUT–
Program 4.1:-
5

Program 4.2:-

LEARNING OUTCOMES -
 Identify situations where computational methods would be useful.

 Approach the programming tasks using techniques learnt and write


pseudocode.
 Choose the right data representation formats based on the requirements of the
problem.
 Use the comparisons and limitations of the various programming constructs
and choose the right one for the task.

EVALUATION COLUMN (To be filled by concerned faculty


only)-
Sr. No. Parameters Maximum Marks Marks Obtained
6

1. Worksheet Completion 10

2. Viva 8

3. Conduct 12

Total Marks 30

You might also like