You are on page 1of 12

Bahria University, Lahore Campus

Department of Computer Science


Lab Journal 01
(Spring 2021)

Object Oriented Programming -


Course: Lab Date: 03-March-2021
Course Code: CSL-210 Max Marks: 10
Faculty’s Name: Mr. Irfan Latif

Name: Muhammad Haseeb Akbar Enroll No: 03-134202-052 Class: BSCS-2B

Objective(s):
Upon completion of this lab session, learners will be able to:
• Define structures
• Define & Implement an Array of Structures
• Pass Pointers to Structures
• Pass Structure to Function

Lab Tasks:

Task 1
Write a program to create structure named employee. Take information of employee from
user as input (EmpID, EmpName, EmpAge, EmpSalary) Display the output.

Code:
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;

struct Employee
{
int EmpID;
string Empname;
string Empage;
int Empsalary;

}Emp;

void main()
{
Employee E;

cout << "enter Employee id : " << endl;


cin >> E.EmpID;
cout << "enter Employee Name : " << endl;
cin >>E.Empname;
cout << "enter employee Age : " << endl;
cin >> E.Empage;
cout << "enter Employee Salary : " << endl;
Enrollment Number: 03-134202-052

cin >> E.Empsalary;

cout << "Employee ID : " << E.EmpID << endl;


cout << "Employee Name : " << E.Empname << endl;
cout << "Employee Age : " << E.Empage << endl;
cout << "Employee Salary : " << E.Empsalary << endl;

system("pause");

Display/Output:

Task 2
Perform Task 1 using pointer structure.

Code:
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;

struct Employee
{
int EmpID;
string Empname;
string Empage;
int Empsalary;

}Emp;

int main()
{
Employee E;

Employee *ptr;

Page 2 of 12
Enrollment Number: 03-134202-052

ptr=&E;

cout << "enter Employee id : " << endl;


cin >> E.EmpID;
cout << "enter Employee Name : " << endl;
cin >>E.Empname;
cout << "enter employee Age : " << endl;
cin >> E.Empage;
cout << "enter Employee Salary : " << endl;
cin >> E.Empsalary;

cout<<"Location of Employee is : "<< ptr <<endl;


cout << "Employee ID : " << E.EmpID << endl;
cout << "Employee Name : " << E.Empname << endl;
cout << "Employee Age : " << E.Empage << endl;
cout << "Employee Salary : " << E.Empsalary << endl;

return 0;

Display/Output:

Task 3
Perform Task 1 and display the output using function. Pass the structure object in function
first by value and then by reference.

Code:

#include<iostream>
using namespace std;
struct employee
{
int EmpID;
char Empname[20];

Page 3 of 12
Enrollment Number: 03-134202-052

int Empage;
int Empsalary;
};
void pass_reference(employee &value)
{

cout << "Employee ID : ";


cin >> value.EmpID;cout << "Enter Employee Name : " ;
cin.ignore();
cin.getline(value.Empname, 20);
cout << "Enter Employee Age: ";
cin >> value.Empage;cout << "Enter Employee Salary : ";
cin >> value.Empsalary;

void showdata(employee data)


{
cout << "Employee ID : ";
cout << data.EmpID;
cout << endl;cout << "Employee Name : ";
cout << data.Empname;cout << endl;
cout << "Employee age : ";
cout << data.Empage;cout << endl;
cout << "Employee salary : ";
cout << data.Empsalary;
cout << endl;

int main()
{employee data;
cout << "Give Us Information about Employee :) " << endl;
pass_reference(data);
showdata(data);

return 0;

Display/output:

Page 4 of 12
Enrollment Number: 03-134202-052

Task 4
Enter the marks of 5 students in Computer Programming, ICT and Object-Oriented
Programming (each out of 100) using a structure named Marks having elements roll no.,
name, cp_marks, ict_marks and oop_marks and then display the percentage of each student.
Code:

#include<iostream>
#include<conio.h>
#include<string>
using namespace std;

struct Student
{
int Enum;
int CPmarks;
int ICTmarks;
int OOPmarks;

}s1,s2,s3,s4,s5;

void student1(); //prototype


void student2(); //prototype
void student3(); //prototype
void student4(); //prototype
void student5(); //prototype

void student1()
{

Student s1;

cout << "Enter S1 Enum : " << endl;


cin >>s1.Enum;
cout << "Enter S1 CP Marks : " << endl;
cin >>s1.CPmarks;
cout << "Enter S1 ICT Marks : " << endl;

Page 5 of 12
Enrollment Number: 03-134202-052

cin >> s1.ICTmarks;


cout << "Enter S1 OOP Marks : " << endl;
cin >> s1.OOPmarks;

float per = (s1.CPmarks + s1.ICTmarks + s1.OOPmarks) /3;

cout << "Student1 Enumber : " << s1.Enum << endl;


cout << "Student1 CP Marks : " << s1.CPmarks << endl;
cout << "Student1 ICTmarks : " << s1.ICTmarks << endl;
cout << "Student1 OOP Marks : " << s1.OOPmarks << endl;
cout << "Student1 Percentage : " << per << endl ;

void student2()
{
cout << "Enter S2 Enum : " << endl;
cin >> s2.Enum;
cout << "Enter S2 CP Marks : " << endl;
cin >> s2.CPmarks;
cout << "Enter S2 ICT Marks : " << endl;
cin >> s2.ICTmarks;
cout << "Enter S2 OOP Marks : " << endl;
cin >> s2.OOPmarks;

float per = (s2.CPmarks + s2.ICTmarks + s2.OOPmarks) /3;

cout << "Student2 Enumber : " << s2.Enum << endl;


cout << "Student2 CP Marks : " << s2.CPmarks << endl;
cout << "Student2 ICTmarks : " << s2.ICTmarks << endl;
cout << "Student2 OOP Marks : " << s2.OOPmarks << endl;
cout << "Student2 Percentage : " << per << endl ;
}

void student3 ()
{

cout << "Enter S3 Enum : " << endl;


cin >> s3.Enum;
cout << "Enter S3 CP Marks : " << endl;
cin >> s3.CPmarks;
cout << "Enter S3 ICT Marks : " << endl;
cin >> s3.ICTmarks;
cout << "Enter S3 OOP Marks : " << endl;
cin >> s3.OOPmarks;

float per = (s3.CPmarks + s3.ICTmarks + s3.OOPmarks) /3;

cout << "Student3 Enumber : " << s3.Enum << endl;


cout << "Student3 CP Marks : " << s3.CPmarks << endl;
cout << "Student3 ICTmarks : " << s3.ICTmarks << endl;
cout << "Student3 OOP Marks : " << s3.OOPmarks << endl;
cout << "Student3 Percentage : " << per << endl ;

Page 6 of 12
Enrollment Number: 03-134202-052

void student4()
{
cout << "Enter S4 Enum : " << endl;
cin >> s4.Enum;
cout << "Enter S4 CP Marks : " << endl;
cin >> s4.CPmarks;
cout << "Enter S4 ICT Marks : " << endl;
cin >> s4.ICTmarks;
cout << "Enter S4 OOP Marks : " << endl;
cin >> s4.OOPmarks;

float per = (s4.CPmarks + s4.ICTmarks + s4.OOPmarks) /3;

cout << "Student4 Enumber : " << s4.Enum << endl;


cout << "Student4 CP Marks : " << s4.CPmarks << endl;
cout << "Student4 ICTmarks : " << s4.ICTmarks << endl;
cout << "Student4 OOP Marks : " << s4.OOPmarks << endl;
cout << "Student4 Percentage : " << per << endl;
}

void student5()
{
cout << "Enter S5 Enum : " << endl;
cin >> s5.Enum;
cout << "Enter S5 CP Marks : " << endl;
cin >> s5.CPmarks;
cout << "Enter S5 ICT Marks : " << endl;
cin >> s5.ICTmarks;
cout << "Enter S5 OOP Marks : " << endl;
cin >> s5.OOPmarks;

float per = (s5.CPmarks + s5.ICTmarks + s5.OOPmarks) /3;

cout << "Student5 Enumber : " << s5.Enum << endl;


cout << "Student5 CP Marks : " << s5.CPmarks << endl;
cout << "Student5 ICTmarks : " << s5.ICTmarks << endl;
cout << "Student5 OOP Marks : " << s5.OOPmarks << endl;
cout << "Student5 Percentage : " << per << endl;
}

int main()
{

cout <<"***********************************"<<endl;

cout <<" Press 1 for student 1 data"<< endl;


cout <<" Press 2 for student 2 data"<< endl;
cout <<" Press 3 for student 3 data"<< endl;
cout <<" Press 4 for student 4 data"<< endl;
cout <<" Press 5 for student 5 data"<< endl;

Page 7 of 12
Enrollment Number: 03-134202-052

cout <<"***********************************"<<endl;

cout <<" Enter your Student Number : ";


int num;
cin >> num;

cout <<"***********************************"<<endl;

switch (num) {
case 1:
student1();
break;
case 2:
student2();
break;
case 3:
student3();
break;
case 4:
student4();
break;
case 5:
student5();
break;

default:
cout << "Student Doesn't Exist" << endl;
}

return 0;
}

Display/Output:

Page 8 of 12
Enrollment Number: 03-134202-052

Task 5

Write a structure to store the name, account number and balance of 50 customers and store
their information.
1 - Write a function to print the names of all the customers having balance less than $200.
2 - Write a function to add $100 in the balance of all the customers having more than $1000
in their balance and then print the incremented value of their balance.

Code:

#include<iostream>
#include<string>
using namespace std;

struct data
{
string username;
int Acc_no;
float Balance;

}U1, U2, U3, U4, U5, U6, U7, U8, U9, U10, U11, U12, U13, U14, U15, U16, U17, U18,
U19, U20;

void showData(data U)

{
if (U.Balance < 200)
{
cout << "Account having balance less than 200 ";
cout <<"is : "<< U.username << endl;
cout<<endl;
}

if (U.Balance>1000)
{
int addBalance = U.Balance + 100;
cout <<"Account having Balance more then 1000 : "<< addBalance << "With Username '"<<
U.username << "'" << endl;

cout<<endl;
}

int main(){

cout << "Enter User Name : "; cin >> U1.username;


cout << "Enter Account No : "; cin >> U1.Acc_no;
cout << "Enter your Balance : "; cin >> U1.Balance;
cout<<"=========================" <<endl;
cout << endl;
cout << "Enter User Name : "; cin >> U2.username;
cout << "Enter Account No : "; cin >> U2.Acc_no;

Page 9 of 12
Enrollment Number: 03-134202-052

cout << "Enter your Balance : "; cin >> U2.Balance;


cout<<"=========================" <<endl;
cout << endl;
cout << "Enter User Name : "; cin >> U3.username;
cout << "Enter Account No : "; cin >> U3.Acc_no;
cout << "Enter your Balance : "; cin >> U3.Balance;
cout<<"=========================" <<endl;
cout << endl;
cout << "Enter User Name : "; cin >> U4.username;
cout << "Enter Account No : "; cin >> U4.Acc_no;
cout << "Enter your Balance : "; cin >> U4.Balance;
cout<<"=========================" <<endl;
cout << endl;
cout << "Enter User Name : "; cin >> U5.username;
cout << "Enter Account No : "; cin >> U5.Acc_no;
cout << "Enter your Balance : "; cin >> U5.Balance;
cout<<"=========================" <<endl;
cout << endl;
cout << "Enter User Name : "; cin >> U6.username;
cout << "Enter Account No : "; cin >> U6.Acc_no;
cout << "Enter your Balance : "; cin >> U6.Balance;
cout<<"=========================" <<endl;
cout << endl;
cout << "Enter User Name : "; cin >> U7.username;
cout << "Enter Account No : "; cin >> U7.Acc_no;
cout << "Enter your Balance : "; cin >> U7.Balance;
cout<<"=========================" <<endl;
cout << endl;
cout << "Enter User Name : "; cin >> U8.username;
cout << "Enter Account No : "; cin >> U8.Acc_no;
cout << "Enter your Balance : "; cin >> U8.Balance;
cout<<"=========================" <<endl;
cout << endl;
cout << "Enter User Name : "; cin >> U9.username;
cout << "Enter Account No : "; cin >> U9.Acc_no;
cout << "Enter your Balance : "; cin >> U9.Balance;
cout<<"=========================" <<endl;
cout << endl;
cout << "Enter User Name : "; cin >> U10.username;
cout << "Enter Account No : "; cin >> U10.Acc_no;
cout << "Enter your Balance : "; cin >> U10.Balance;
cout<<"=========================" <<endl;
cout << endl;
cout << "Enter User Name : "; cin >> U11.username;
cout << "Enter Account No : "; cin >> U11.Acc_no;
cout << "Enter your Balance : "; cin >> U11.Balance;
cout<<"=========================" <<endl;
cout << endl;
cout << "Enter User Name : "; cin >> U12.username;
cout << "Enter Account No : "; cin >> U12.Acc_no;
cout << "Enter your Balance : "; cin >> U12.Balance;
cout<<"=========================" <<endl;
cout << endl;

Page 10 of 12
Enrollment Number: 03-134202-052

cout << "Enter User Name : "; cin >> U13.username;


cout << "Enter Account No : "; cin >> U13.Acc_no;
cout << "Enter your Balance : "; cin >> U13.Balance;
cout<<"=========================" <<endl;
cout << endl;
cout << "Enter User Name : "; cin >> U14.username;
cout << "Enter Account No : "; cin >> U14.Acc_no;
cout << "Enter your Balance : "; cin >> U14.Balance;
cout<<"=========================" <<endl;
cout << endl;
cout << "Enter User Name : "; cin >> U15.username;
cout << "Enter Account No : "; cin >> U15.Acc_no;
cout << "Enter your Balance : "; cin >> U15.Balance;
cout<<"=========================" <<endl;
cout << endl;
cout << "Enter User Name : "; cin >> U16.username;
cout << "Enter Account No : "; cin >> U16.Acc_no;
cout << "Enter your Balance : "; cin >> U16.Balance;
cout<<"=========================" <<endl;
cout << endl;
cout << "Enter User Name : "; cin >> U17.username;
cout << "Enter Account No : "; cin >> U17.Acc_no;
cout << "Enter your Balance : "; cin >> U17.Balance;
cout<<"=========================" <<endl;
cout << endl;
cout << "Enter User Name : "; cin >> U18.username;
cout << "Enter Account No : "; cin >> U18.Acc_no;
cout << "Enter your Balance : "; cin >> U18.Balance;
cout<<"=========================" <<endl;
cout << endl;
cout << "Enter User Name : "; cin >> U19.username;
cout << "Enter Account No : "; cin >> U19.Acc_no;
cout << "Enter your Balance : "; cin >> U19.Balance;
cout<<"=========================" <<endl;
cout << endl;
cout << "Enter User Name : "; cin >> U20.username;
cout << "Enter Account No : "; cin >> U20.Acc_no;
cout << "Enter your Balance : "; cin >> U20.Balance;
cout<<"=========================" <<endl;
cout << endl;
showData(U1);
showData(U2);
showData(U3);
showData(U4);
showData(U5);
showData(U6);
showData(U7);
showData(U8);
showData(U9);
showData(U10);
showData(U11);
showData(U12);
showData(U13);

Page 11 of 12
Enrollment Number: 03-134202-052

showData(U14);
showData(U15);
showData(U16);
showData(U17);
showData(U18);
showData(U19);
showData(U20);

return 0;
}

Display/output:

Other users are empty at


That time.

Lab Grading Sheet :


Max
Obtained
Task Mark Comments(if any)
Marks
s
1. 02
2. 02
3. 02
4. 02
5. 02
Total 10 Signature

Note : Attempt all tasks and get them checked by your Lab Instructor.

Page 12 of 12

You might also like