You are on page 1of 6

EXPERIMENT NUMBER –Practical 1.4.

1
STUDENT’S NAME – Aviral Dwivedi

STUDENT’S UID – 20BCS9316

CLASS AND GROUP – 32A


SEMESTER – 2ND

TOPIC OF EXPERIMENT – LEARN HOW TO USE CONSTRUCTOR AND


DESTRUCTOR IN C++

AIM OF THE EXPERIMENT- WAP to find area of rectangle using constructor


overloading. Also define destructor to delete the memory allocated to
objects.

PROGRAME CODE:

#include <bits/stdc++.h>
using namespace std;

// question one
class area{
    int len, bre, ar;
    public:
    area(void);
    area(int, int);
    ~area();
    void calc(){
        ar=len*bre;
        cout<<"Area is:"<<ar<<endl;
    }
};

area::area(void){
    len = 3;
    bre = 5;
}

area::area(int x, int y){
    len = x;
    bre = y;
}

area::~area(){
    cout<<"object is bieng deleted"<<endl;
SUBJECT NAME- OBJECT ORIENTED PROGRAMMING
USING C++ LAB
SUBJECT CODE-CSP-152
}

int main(){
    int l,b;
    area o1;
    o1.calc();
    cout<<"enter the value of length and breadth: ";
    cin>>l>>b;
    area o2(l,b);
    o2.calc();

    return 0;
}

ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION


(Kindly jot down the compile time errors encountered):

NO ERROR ENCOUNTERD
PROGRAMS’ EXPLANATION (in brief):
In this program we are making constructor names area and we making one default
constructor and one parameterized constructor.
We are also making destructor and which is releasing the memory and finally we are
getting desired output.
OUTPUT:

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
EXPERIMENT NUMBER –Practical 1.4.2
STUDENT’S NAME – Aviral Dwivedi

STUDENT’S UID – 20BCS9316

CLASS AND GROUP – 32A

SEMESTER – 2ND

TOPIC OF THE EXPERIMENT: 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.           

PROGRAM CODE:

class details
{
    string name;
    int roll;
    int hi;
    int wei;

public:
    details();
    details(string, int, int, int);
    ~details()
    {
        cout << "distructor is called";
    }
    void display()
SUBJECT NAME- OBJECT ORIENTED PROGRAMMING
USING C++ LAB
SUBJECT CODE-CSP-152
    {
        cout << "name of the student is: " << name << endl
             << "Roll number is: " << roll << endl
             << "Hight: " << hi << endl
             << "weight: " << wei << endl;
    }
};

details::details(string a, int b, int c, int d)
{
    name = a;
    roll = b;
    hi = c;
    wei = d;
}

int main()
{
    details s("Kshitij Kumar", 4094, 178, 80);
    details s1 = s;
    s1.display();
    return 0;
}

ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION


(Kindly jot down the compile time errors encountered):

practical4.cpp: In function 'int main()':


practical4.cpp:78:17: error: expected ';' before 'return'
s1.display()
^
;
return 0;
~~~~~~
PROGRAMS’ EXPLANATION (in brief):

In this program se are printing the name, height, weight, and roll number of the
candidate using copy constructor and erasing the memory be destructor after having
output.

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
OUTPUT:

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
LEARNING OUTCOMES
 Identify situations where computational methods would be useful.
 Approach the programming tasks using techniques learnt and write pseudo-
code.
 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
1. Worksheet Completion including 10
writing learning objective/ Outcome
2. Post Lab Quiz Result 5

3. Student engagement in Simulation/ 5


Performance/ Pre Lab Questions
4. Total Marks 20

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152

You might also like