0% found this document useful (0 votes)
34 views

BSEE21036 Lab 10 Oop

The objective of this lab is to observe programming knowledge in C++. Students are required to individually perform experiments. Structures in C++ provide a convenient way to group related variables into a single entity. The tasks involve implementing nested structures and returning structures. For task 1, a Rectangle structure with member variables for length, width, and area is created. Member functions to set dimensions, calculate area, and display details are defined. For task 2, a Person structure with member variables for name, age, and address is created. Member functions to create a person object and display details are defined. The code is implemented and outputs are displayed.

Uploaded by

Fazool Farigh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

BSEE21036 Lab 10 Oop

The objective of this lab is to observe programming knowledge in C++. Students are required to individually perform experiments. Structures in C++ provide a convenient way to group related variables into a single entity. The tasks involve implementing nested structures and returning structures. For task 1, a Rectangle structure with member variables for length, width, and area is created. Member functions to set dimensions, calculate area, and display details are defined. For task 2, a Person structure with member variables for name, age, and address is created. Member functions to create a person object and display details are defined. The code is implemented and outputs are displayed.

Uploaded by

Fazool Farigh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Electrical Engineering Department - ITU

CS152L: Object Oriented Programming Lab

Course Instructor: Nadir Abbas Dated:

Lab Engineer: Usama Riaz Semester: Summer 2023

Session: Batch:

Lab 10. Solving Problems by Utilization of Structs

Obtained Marks/100
Name Roll number

Muhammad Sami Ullah BSEE21036

Checked on: ____________________________

Signature: ____________________________
Objective
The objective of this lab is to observe the basic knowledge of programming in C++.
Equipment and Component
Component Description Value Quantity
Computer Available in lab 1
Conduct of Lab
• Students are required to perform this experiment individually.
• In case the lab experiment is not understood, the students are advised to seek help from
the course instructor, lab engineers, assigned teaching assistants (TA) and lab
attendants.

Theory and Background


In C++, a 1D array is declared by specifying the data type of the elements followed by the array
name and the size of the array in square brackets. For example, int numbers[5]; declares a 1D
integer array named numbers with a size of 5 elements. Elements in a 1D array can be accessed
using the array name and the index, starting from 0. For instance, numbers[0] refers to the first
element in the array.

A 2D array in C++ is essentially an array of arrays. It is declared by specifying the data type of
the elements, followed by the array name and the size of the rows and columns in square
brackets. For example, int matrix[3][4]; declares a 2D integer array named matrix with 3 rows
and 4 columns. Elements in a 2D array can be accessed using two indices, one for the row and
one for the column. For instance, matrix[1][2] refers to the element in the second row and third
column of the array.

In C++, a char array is used to represent a sequence of characters, typically used for strings. It
is declared by specifying the data type char, followed by the array name and the size of the array
in square brackets. For example, char name[10]; declares a char array named name with a size of
10 characters. Char arrays are terminated by a null character ('\0') placed at the end, indicating
the end of the string. Functions from the <cstring> library, such as strcpy, strlen, can be used to
manipulate char arrays.

In C++, a pointer is declared by specifying the data type followed by an asterisk () and the
pointer name. Pointers hold the memory address of another variable. For example, int* ptr;
declares a pointer named ptr that can hold the memory address of an integer variable. Pointers
are used to indirectly access variables and their values using the dereference operator (). Pointers
are particularly useful in dynamic memory allocation, accessing elements in arrays, and passing
arguments to functions by reference.

Structures in C++ provide a convenient way to group related variables together into a single
entity. By defining a structure, you can create a custom data type that represents a specific
concept or object in your program. The member variables within a structure can have different
data types, allowing you to store and organize diverse information. Structures are commonly
used to represent real-world entities such as people, products, or coordinates.

Lab Task
Part A [Marks: 5]

Please follow the following steps before starting below tasks:

• Create function.h file for declaration of all functions


• Create function.cpp file to define define all declared functions.
• Create main.cpp file for driving code/call functions.

Note: Make a menu driven program (compulsory).

Part B: Implementation of Nested Structures & Returning a Structure [Marks: 35]

• Create a structure called "Rectangle" that represents a rectangle shape.

The structure should have the following member variables:

length: The length of the rectangle (an integer).


width: The width of the rectangle (an integer).
area: The area of the rectangle (an integer).

Implement the following functions for the Rectangle structure:

SetRectangleDimensions: Set the dimensions of the rectangle (length and width).


CalculateArea: Calculate and update the area of the rectangle.
DisplayRectangleDetails: Display the length, width, and area of the rectangle.

Solve the problem using nested structures and member functions.

• Create a structure called "Person" that represents a person's information.

The structure should have the following member variables:

name: The name of the person (a string).


age: The age of the person (an integer).
address: The address of the person (a string).

Implement the following functions for the Person structure:

CreatePerson: Create and initialize a Person object with the provided name, age, and
address. Return the created Person object.
DisplayPersonDetails: Display the name, age, and address of the person.

Solve the problem by defining the structure, implementing the functions, and
demonstrating their usage in the main function.

// Paste your code here


//Functions.h
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include<iostream>
#include"Functions.h"
using namespace std;
template <typename T>
struct Rectangle {
struct Dimensions {
T Length;
T Width;
};
Dimensions dimensions;
T Area;

void SetRectangleDimensions(T Length, T Width) {


dimensions.Length=Length;
dimensions.Width=Width;
// dimensions.Area;
}

void CalculateArea() {
Area = dimensions.Length * dimensions.Width;

void DisplayRectangleDetails() {
cout << "Length of the Rectangle is\n" << dimensions.Length << endl;
cout << "Width of the Rectangle is\n" << dimensions.Width << endl;
cout << "Area of the Rectangle is\n" << Area << endl;
}
};
template <typename T>// template
struct Person {// constructure
struct Details{// constructure
string name;// declaration
T Age;// declaration
string addres;// declaration
};
Details Information;
Person(const string& name, T Age, const string& addres){
Information.name=name;
Information.Age=Age;
Information.addres= addres;
}
DisplayPersonDetails(){// functon
cout<<"The name is: "<<Information.name<<endl;// displaying the output and moving to next
line
cout<<"The age is: "<<Information.Age<<endl;// displaying the output and moving to next line
cout<<"The addres is: "<<Information.addres<<endl;// displaying the output and moving to
next line
}
};
template <typename T>// templATE FUNCTION
Person <T> CreatePerson(const string& name, T Age, const string& addres){//
return Person<T>(name, Age, addres);// returning values
};
#endif
//Functions.cpp
#include"Functions.h"
#include<iostream>
#include<iomanip>
using namespace std;

//main.cpp
#include"Functions.h"
#include<iostream>
using namespace std;
int main() {// start of main function
int x;
cout<<"OPTINS:-\n 1)Task 01\n 2)Task02\n";
cout<<"Enter a number = ";
cin>>x;
switch(x)
{
case 1:
{
int l, w;
cout<<"Enter the Lenght of the rectanlge"<<endl;
cin>>l;
cout<<"Enter the Width of the rectanlge"<<endl;
cin>>w;
Rectangle<int> data;
data.SetRectangleDimensions(l, w);
data.CalculateArea();
data.DisplayRectangleDetails();
return 0;

break;
}
case 2:
{

Person<int> person= CreatePerson("Muhammad Sami Ullah Laang", 18, "LCf furniture


Lahore");// displaying
person.DisplayPersonDetails();
return 0;// end of main function

break;
}
default:
{
cout<<"You eneterd a wrong number !!";
}
}
return 0;// end of main function
}

// Paste your output here


Assessment Rubric for Lab

Performance Max
Task CLO Description Exceeds expectation Meets expectation Does not meet expe
metric marks
Executes without errors Executes without errors, user
Does not execute du
excellent user prompts, prompts are understandable,
errors, runtime error
1. Realization good use of symbols, minimum use of symbols or
1 1 Functionality 40 prompts are mislead
of experiment spacing in output. Through spacing in output. Some
existent. No testing h
testing has been completed testing has been completed
completed (0-19)
(35-40) (20-34)
Actively engages and Cooperates with other group
Distracts or discoura
Group cooperates with other member(s) in a reasonable
2. Teamwork 1 3 5 group members from
Performance group member(s) in manner but conduct can be
the experiment (0-1)
effective manner (4-5) improved (2-3)
On Spot Able to make changes (8- Partially able to make
1 1 10 Unable to make chan
3. Conducting Changes 10) changes (5-7)
experiment Answered all questions (8- Unable to answer all
1 1 Viva/Quiz 10 Few incorrect answers (5-7)
10) (0-4)
4. Laboratory
Comments are added and Comments are added and
safety and Code
1 3 5 does help the reader to does not help the reader to Comments are not ad
disciplinary commenting
understand the code (4-5) understand the code (2-3)
rules
Excellent use of white
space, creatively organized Includes name, and
Poor use of white sp
work, excellent use of assignment, white space
5. Data (indentation, blank l
1 3 Code Structure 5 variables and constants, makes the program fairly easy
collection code hard to read, di
correct identifiers for to read. Title, organized work,
and messy (0-1)
constants, No line-wrap (4- good use of variables (2-3)
5)
Solution is efficient, easy A logical solution that is easy
6. Data A difficult and ineffi
1 4 Algorithm 20 to understand, and maintain to follow but it is not the most
analysis solution (0-5)
(15-20) efficient (6-14)
Documentation
7. Computer
1 2 & GitHub 5 Timely (4-5) Late (2-3) Not done (0-1)
use
Submissions
  Max Marks (total): 100 Obtained M

You might also like