Student'S Name Student'S Uid Section and Group Subject:-Semester

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

EXPERIMENT NO:

STUDENT’S NAME:- SUBJECT:- OOPS


SEMESTER:- 2nd
STUDENT’S UID:-

SECTION AND GROUP:- 109-B

AIM OF THE EXPERIMENT–

1. WAP to input a matrix of dimension m*n. If base address is 1000. Find the address of (m-1, n-1)
element of the matrix.

PROGRAM CODE:

#include<iostream>
using namespace std;

int main() {
int b, i, j, w, lr = 0, lc = 0, n, m;
int a[10][10];
cout << "Enter the number of rows in matrix: ";
cin >> m;
cout << "Enter the number of columns in matrix: ";
cin >> n;
cout << "Enter the elements in the matrix:\n";
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
cin >> a[i][j];
}
}
cout << "Enter the base address: ";
cin >> b;
cout << "Enter the storage size of one element stored in array: ";
cin >> w;
i = m-1;
j = n-1;
cout << "Address of A[" << i << "][" << j << "]: " << b + w * (n*(i-lr) + (j-lc)) << endl;
return 0;
}

SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-


21CSH103
PROGRAM’S EXPLANATION(in brief):

The program first declares several integer variables for the base address (b), row and column
numbers (i, j), storage size of one element (w), and the number of rows and columns (m, n).
A two-dimensional integer array a is declared to hold the matrix elements. The program prompts the
user to input the number of rows and columns of the matrix, and then asks for the elements in the
matrix using nested for loops. The user is then prompted to input the base address and storage size
of one element. The program calculates the memory address of the element located at the last row
and last column of the matrix. This is done by using the formula b + w * (n*(i-lr) + (j-lc)).
Finally, the program outputs the memory address of the specified element using the cout statement.
Note that the using namespace std; statement is used to avoid having to prefix standard library
functions with std::. This is a common practice in C++ programs.

OUTPUT:

2. Create a class called employee with the following details as variables within it.

1. Name of the employee (string)

2. Age (int)

3. Designation (string)

SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-


21CSH103
4. Salary (double)

Write a program to create array of objects for the same to access these. Also, make use of
member functions to accept values and print the name, age, designation and salary.

PROGRAM CODE:

#include<iostream>
using namespace std;

class Employee {
char Name[25];
int Age;
char Desg[8];
long Salary;
public:
void GetData();
void PutData();
};

void Employee::GetData() {
cout << "\n\tEnter Employee Name : ";
cin >> Name;
cout << "\n\tEnter Employee Age : ";
cin >> Age;
cout << "\n\tEnter Employee Designation:";
cin >> Desg;
cout << "\n\tEnter Employee Salary : ";
cin >> Salary;
}

void Employee::PutData() {
cout << "\nEmployee Name : " << Name;
cout << "\nEmployee Age : " << Age;
cout << "\nEmployee Designation:" << Desg;
cout << "\nEmployee Salary : " << Salary;
}

int main() {
Employee E[5]; // an array of 5 Employee objects
int i;
for(i=0; i<5; i++) {
E[i].GetData(); // prompt user to enter data for each Employee object
}
for(i=0; i<5; i++) {
E[i].PutData(); // display the entered data for each Employee object
}
SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-
21CSH103
return 0;
}

PROGRAM’S EXPLANATION(in brief):

The Employee class has 4 private data members: Name, Age, Desg, and Salary. It also has 2 public
member functions: GetData() and PutData().
GetData() prompts the user to enter the data for an Employee object and stores it in the private data
members.
PutData() displays the data stored in the private data members of an Employee object.
In the main() function, an array of 5 Employee objects is created.
A for loop is used to prompt the user to enter the data for each Employee object by calling the
GetData() member function for each object.
Another for loop is used to display the entered data for each Employee object by calling the
PutData() member function for each object.
Finally, the main() function returns 0 to indicate successful execution of the program

OUTPUT:

SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-


21CSH103
LEARNING OUTCOMES

• Remember the concepts related to fundamentals of C language, draw flowcharts and


write algorithm/pseudocode.

• Understand the way of execution and debug programs in C language.

• Apply various constructs, loops, functions to solve mathematical and scientific problem.

• Analyze the dynamic behavior of memory by the use of pointers.

• Design and develop modular programs for real world problems using control structure
and selection structure.

EVALUATION COLUMN (To be filled by concerned faculty only)


Sr. No. Parameters Maximum Marks
Marks Obtaine
d
1. Worksheet Completion including writing learning 10
objective/ Outcome
2. Viva 8

3. Conduct 12

4. Total Marks 30

SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-


21CSH103

You might also like