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

University Institute of Engineering Department

of Mechatronics

Experiment: 3.2

Student Name: Vinay Kumar Gupta UID : 21BEM1014

Branch: Mechatronics Engineering Section/Group: B


Semester: 2 Date of Performance:2/05/2022
Subject Name: OBJECT ORIENTED PROGRAMMING USING C++
Subject Code: 21CSH-103

Aim of the practical: LEARN HOW TO USE DYNAMIC MEMORY ALLOCATION IN C++

PREREQUISITES: KNOWLEDGE OF OPERATORS USED TO ALLOCATE AND DEALLOCATE MEOMORY IN


C++

LIST OF SUB-PROGRAMS-

Practical 9.1: WAP to calculate sum of marks of n students of a class inputted via dynamic memory
allocation.

Solution:

#include <iostream>

#include <conio.h>

using namespace std;

{
University Institute of Engineering Department
of Mechatronics

int main( )
University Institute of Engineering Department
of Mechatronics

int sum=0, N;

//clrscr();

cout<<"Enter number of students in Class:\n";

cin>>N;

int *a = new int[N];

cout<<"\nEnter "<<N<<"Students Marks: "<<endl;

for(int i=0; i<N; i++)

cin>>a[i];

cout<<"Enter Marks are:"<<endl;

for(int i=0; i<N; i++)

cout<<a[i]<<endl;

sum = sum + a[i]; // sum += a[i];

cout<<"Total Sum: "<<sum;

delete(a);

getch();

Output:

{
University Institute of Engineering Department
of Mechatronics

Practical 9.2: WAP to allocate memory dynamically for an object of a given class using class’s constructor.

Solution:

#include <iostream>

using namespace std;

class stud {

public:

stud()

cout<< "Constructor Used" <<endl;

~stud()
University Institute of Engineering Department
of Mechatronics

cout<< "Destructor Used" <<endl;

};

int main()

stud* S = new stud[6];

delete[] S;

Output:

{
University Institute of Engineering Department
of Mechatronics

Learning outcomes (What I have learnt):

• Understand the concepts of object-oriented programming including programming process and


compilation process.

• Apply different techniques to decompose a problem and programmed a solution with its sub modules.

• Analyze and explain the behavior of simple programs involving the programming addressed in the
course.

• Implement and evaluate the programs using the syntax and semantics of objectoriented programming.

• Design the solution of real-world problems in order to determine that the program performs as expected.

Evaluation Grid (To be filled by Faculty):


Sr. No. Parameters Marks Obtained Maximum Marks
1. Worksheet completion including 10
writinglearning
objectives/Outcomes.(To besubmitted
at the end of the day)
2. Post Lab Quiz Result. 5
3. Student Engagement in 5
Simulation/Demonstration/Performance
and Controls/Pre-Lab Questions.
Signature of Faculty (with Date): Total Marks Obtained: 20

You might also like