EC-233 Data Structures and Algorithms: Lab Report#01
EC-233 Data Structures and Algorithms: Lab Report#01
LAB REPORT#01
Submitted by:
Mahnoor Inam
18-CE-009
Amina Khalid
18-CE-017
Lab. Instructor
Tauqeer Anjum
Experiment # 01
User Defined Data Types
Structures & Classes
1 OBJECTIVE:
Objective of this lab session is to understand and revise user defined data types i.e structures
and classes.
2 SOFTWARE TOOL: -
1. Microsoft Visual Studio/ Code Blocks
Theory:
User-defined types are collections of data, which describe an object's attributes and state. ... For
example, a class can be defined called MyClass, then objects can be instantiated
of type MyClass, like native variables can be of type short, char, etc
Lab Tasks
Task1
Input
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input
loop */
struct student{
int id;
string name;
int semesters;
int courses;
int marksincources;
}a;
struct courses{
void getdata()
{cout<<"Enter person 1 id:";
cin>>a.id;
cout<<"Enter person 1 name:";
cin>>a.name;
cout<<"Enter person 1 semesters:";
cin>>a.semesters;
cout<<"Enter marks in course:";
cin>>a.marksincources;
cout<<"Enter person 2 id:";
cin>>a.id;
cout<<"Enter person 2 name:";
cin>>a.name;
cout<<"Enter marks in cource:";
cin>>a.marksincources;
}
void displaydata()
{cout<<"person 1 id is:";
cout<<a.id;
cout<<endl;
cout<<"person 1 name is:";
cout<<a.name;
cout<<endl;
cout<<"person 1 semesters are:";
cout<<a.semesters;
cout<<endl;
cout<<"marks in course are:";
cout<<a.marksincources;
cout<<endl;
cout<<" person 2 id is:";
cout<<a.id;
cout<<endl;
cout<<"person 2 name is:";
cout<<a.name;
cout<<endl;
cout<<"marks in cource are:";
cout<<a.marksincources;
cout<<endl;
}
}s1;
int main ()
{
courses s1;
s1.getdata();
s1.displaydata();
}
Output:
Task2
Input
#include <iostream>
using namespace std;
struct time{
int hours;
int mins;
int secs;
};
time elapsed_time(time t4,time t3)
{time temp;
temp.hours=24-t4.hours;
temp.hours=temp.hours+t3.hours;
temp.mins=t3.mins+t4.mins;
if(temp.mins>=60)
{temp.hours++;
temp.mins-=60;
}
temp.secs=t3.secs+t4.secs;
if(temp.secs>=60)
{temp.mins++;
temp.secs-=60;
}
return temp;
}
int main()
{time t1,t2,t3;
cout<<"Enter time1(hours,mins,secs):\n";
cin>>t1.hours;
if(t1.hours>24)
{cout<<"Enter hours again:";
cin>>t1.hours;
}
cin>>t1.mins;
if(t1.mins>60)
{cout<<"Enter minutes again:";
cin>>t1.mins;
}
cin>>t1.secs;
if(t1.secs>60)
{cout<<"Enter seconds again:";
cin>>t1.secs;
}
cout<<"Enter time2(hours,mins,secs):\n";
cin>>t2.hours;
if(t2.hours>24)
{cout<<"Enter hours again:";
cin>>t2.hours;
}
cin>>t2.mins;
if(t2.mins>60)
{cout<<"Enter minutes again:";
cin>>t2.mins;
}
cin>>t2.secs;
if(t2.secs>60)
{cout<<"Enter seconds again:";
cin>>t2.secs;
}
t3=elapsed_time(t1,t2);
cout<<"Elapsed time is:";
cout<<t3.hours<<":"<<t3.mins<<":"<<t3.secs;
return 0;
}
Output:
Task3
Input
#include<iostream>
using namespace std;
class publication
{
protected:
string name;
float price;
public:
virtual void getdata()=0;
virtual void showdata()=0;
};
class book : public publication
{
private:
int page_count;
public:
void getdata()
{
cout<<"\n\nEnter name: ";
cin>>name;
cout<<"Enter price: ";
cin>>price;
cout<<"Enter total pages: ";
cin>>page_count;
cout<<"\n\n\n";
}
void showdata()
{
cout<<"\n\n**********************************************\n";
cout<<"\n\nName: "<<name<<endl;
cout<<"Price: "<<price<<endl;
cout<<"Total pages: "<<page_count;
cout<<"\n\n\n";
cout<<"**********************************************\n";
}
};
class tape : public publication
{
private:
float minutes;
public:
void getdata()
{
cout<<"\n\nEnter name: ";
cin>>name;
cout<<"Enter price: ";
cin>>price;
cout<<"Enter total minutes: ";
cin>>minutes;
cout<<"\n\n\n";
}
void showdata()
{
cout<<"\n\n**********************************************\n";
cout<<"\n\nName: "<<name<<endl;
cout<<"Price: "<<price<<endl;
cout<<"Total minutes: "<<minutes;
cout<<"\n\n\n";
cout<<"**********************************************\n";
}
};
int main (void)
{
char x,c;
int n=0;
publication* ptr[100];
do{
label1:
cout<<"\n\nPress \"b\" to enter data for books\nPress \"t\" to enter data for
tape\nYour Choice: ";
cin>>x;
switch (x)
{
case 'b':
ptr[n]=new book;
break;
case 't':
ptr[n]=new tape;
break;
default:
cout<<"\n\n\tInvalid input!\n";
goto label1;
}
ptr[n]->getdata();
n++;
cout<<"\nYou want to execute again? (y/n): ";
cin>>c;
}while(c=='y');
for(int i=0;i<n;i++)
{
ptr[i]->showdata(); }
return 0;
}
Output:
Conclusion
In this lab we learn about different data types, structures and classes.