0% found this document useful (0 votes)
91 views9 pages

EC-233 Data Structures and Algorithms: Lab Report#01

1) The document is a lab report submitted by two students for their Data Structures and Algorithms course. 2) The objective of the lab was to understand and practice user-defined data types like structures and classes. 3) The lab report includes three tasks - the first involves defining and using a structure, the second defines a time structure and function, and the third defines publication and derived classes.

Uploaded by

Mahnoor Inam
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)
91 views9 pages

EC-233 Data Structures and Algorithms: Lab Report#01

1) The document is a lab report submitted by two students for their Data Structures and Algorithms course. 2) The objective of the lab was to understand and practice user-defined data types like structures and classes. 3) The lab report includes three tasks - the first involves defining and using a structure, the second defines a time structure and function, and the third defines publication and derived classes.

Uploaded by

Mahnoor Inam
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/ 9

EC-233 Data Structures and Algorithms

LAB REPORT#01

  
             Submitted by:
Mahnoor Inam
18-CE-009
Amina Khalid
18-CE-017

Department of Computer Engineering


HITEC University Taxila

                                                                                                                                                                              
                            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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy