Oop Lab 1
Oop Lab 1
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.
//with function
#include <iostream>
#include<string>
using namespace std;
struct employee
{
int EmpId,age;
string EmpName;
double salary;
void input()
{
cout << "Enter Employee id: "; cin >> EmpId;
cout << "Enter Employee Name: "; cin >> EmpName;
cout << "Enter Employee age: "; cin >> age;
cout << "Enter Employee salary: "; cin >> salary;
void output()
{
cout << "Employee id: "; cout<< EmpId <<endl;
cout << "Employee Name: "; cout << EmpName << endl;
cout << "Employee age: "; cout << age << endl;
cout << "Employee salary: "; cout << salary << endl;
};
%
Enrollment Number: ____________________________
int main()
{
employee a;
a.input();
a.output();
return 0;
}
//without function
#include <iostream>
#include<string>
using namespace std;
struct employee
{
};
int main()
{
employee a;
cout << "Employee id: "; cout << a.EmpId << endl;
cout << "Employee Name: "; cout << a.EmpName << endl;
cout << "Employee age: "; cout << a.age << endl;
cout << "Employee salary: "; cout << a.salary << endl;
return 0;
}
struct employee
{
};
int main()
{
employee a[5];
Page 2 of 8
%
Enrollment Number: ____________________________
cout << "Enter Employee id: "; cout << a[i].EmpId <<endl;
cout << "Enter Employee Name: "; cout << a[i].EmpName << endl;
cout << "Enter Employee age: "; cout << a[i].age << endl;
cout << "Enter Employee salary: "; cout << a[i].salary << endl;
return 0;
}
Task 2
Perform Task 1 using pointer structure.
#include <iostream>
#include<string>
using namespace std;
struct employee
{
};
int main()
{
employee *a,p;
a = &p;
Page 3 of 8
%
Enrollment Number: ____________________________
cout << "Employee id: "; cout << (*a).EmpId << endl;
cout << "Employee Name: "; cout << (*a).EmpName << endl;
cout << "Employee age: "; cout << (*a).age << endl;
cout << "Employee salary: "; cout << (*a).salary << endl;
return 0;
}
Task 3
Perform Task 1 and display the output using function. Pass the structure object in function
first by value and then by reference.
#include <iostream>
#include<string>
using namespace std;
struct employee
{
void input()
{
cout << "Enter Employee id: "; cin >> EmpId;
cout << "Enter Employee Name: "; cin >> EmpName;
cout << "Enter Employee age: "; cin >> age;
cout << "Enter Employee salary: "; cin >> salary;
};
void output(employee a)
{
cout << "Employee id: "; cout << a.EmpId << endl;
cout << "Employee Name: "; cout << a.EmpName << endl;
cout << "Employee age: "; cout << a.age << endl;
cout << "Employee salary: "; cout << a.salary << endl;
int main()
{
employee a;
a.input();
output(a);
return 0;
}
Page 4 of 8
%
Enrollment Number: ____________________________
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.
#include <iostream>
#include<string>
#include<conio.h>
using namespace std;
struct Marks
{
int rollno;
double cpmarks,ictmarks,oppmarks;
string name;
};
int main()
{
Marks student[5];
cout << "this program is for only five students" <<endl <<endl;
system("cls");
}
cout << "percentage of " << student[i].name <<": "<< per[i] <<endl;
return 0;
}
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.
#include <iostream>
#include<string>
#include<conio.h>
using namespace std;
struct customers
{
double balance;
string name,accno;
};
cout << "Enter Customer Account number : "; cin >> a[i].accno;
cin.ignore();
cout << "Enter " << a[i].accno << " Name: "; getline(cin, a[i].name);
cout << "Enter " << a[i].name << " Balance in $: "; cin >> a[i].balance;
cout << endl;
Page 6 of 8
%
Enrollment Number: ____________________________
system("cls");
}
return 0;
}
}
return 0;
cout << "Enter Customer name : "; cout << a[i].name; cout << endl;
cout << "Enter " << a[i].name << " Account Number: "; cout <<
a[i].accno; cout << endl;
cout << "Enter " << a[i].name << " Balance : "; cout << a[i].balance;
cout << endl;
return 0;
}
int main()
{
const int size = 50;
customers a[size];
cout << "this program is for only fifty customers" << endl;
input(a,size);
showname(a, size);
increment(a, size);
Page 7 of 8
%
Enrollment Number: ____________________________
showdetails(a, size);
return 0;
}
Note : Attempt all tasks and get them checked by your Lab Instructor.
Page 8 of 8