Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Assignment #1

Department: Computing & IT Department


Subject: OOP

Student Name: Maham Ansar


Class: ADP IT Grey
Semester: 2
Roll #: 045
Submitted to: Ma’am Wajeeha Fareed
Submission Date: 23/10/2022
Write a Program for the following Questions:

Q1.Create a structure called time. Its three members, all type int, should be
called hours, minutes, and seconds. Write a program that prompts the user to
enter a time value in hours, minutes, and seconds. This can be in 12:59:59
format, or each number can be entered at a separate prompt (“Enter hours:”,
and so forth). The program should then store the time in a variable of type
struct time, and finally print out the total number of seconds represented by
this time value: long totalsecs = t1.hours*3600 + t1.minutes*60 + t1.seconds
Program:
// adding libraries
using namespace std;
#include<iostream>
// making a structure class
struct time{
int hours,mint,sec;
};
// main part of program
int main()
{
time t;
int total_sec;
cout<<"Enter the Hours:";
cin>>t.hours;
cout<<"Enter the minutes:";
cin>>t.mint;
cout<<"Enter seconds:";
cin>>t.sec;
cout<<"Your entered time is "<<t.hours<<":"<<t.mint<<":"<<t.sec<<endl;
total_sec=t.hours*3600+t.mint*60+t.sec;
cout<<"Total second in your entered time is: "<<total_sec;
cout<<"program done by 045(maham)"<<endl;
}

Q2.Create a structure called employee that contains two members: an


employee number (type int) and the employee’s compensation (in dollars; type
float). Ask the user to fill in this data for three employees, store it in three
variables of type struct employee, and then display the information for each
employee.
Program:
//adding libraries
#include <iostream>
using namespace std;

//Create a structure called employee that contains two members: an employee


//number (type int) and the employee’s compensation (in dollars; type float).
struct Employee{
int number;
float compensation;
};

int main(){
struct Employee employee[3];
//Ask the user to fill in this data for three employees store it in three
variables of type struct employee
for(int i=0;i<3;i++){
cout<<"Enter the employee number "<<(i+1)<<": ";
cin>>employee[i].number;
cout<<"Enter the employee compensation "<<(i+1)<<": ";
cin>>employee[i].compensation;
cout<<"\n";
}
//display the information for each employee.
cout<<"\n";
for(int i=0;i<3;i++){
cout<<"The employee number "<<(i+1)<<":
"<<employee[i].number<<"\n";
cout<<"The employee compensation "<<(i+1)<<":
"<<employee[i].compensation<<"\n";
}
return 0;
}
Q3.Create a class called Triangle that stores the length of the base and height
of a right triangle in two private instance variables. Include a constructor that
sets these values. Define two functions. The first is hypot( ), which returns the
length of the hypotenuse. The second is an area( ), which returns the area of
the triangle.
Note: Hypot= sqrt((length*length) + (height*height))
Area = sqrt((length*length) + (height*height))

Program:
//adding libraries
#include <iostream>
using namespace std;
// creating class named triangle
class triangle{
public:
int height;
int length;
float hyp;
float ar;
triangle(int l, int h){
length=l;
height=h;
}
//function for hypotenuse
void hypotenuse(){
hyp=((length*length)+(height*height));
cout<<"hypotneuse = "<<hyp<<endl;
}
//function for area
void area(){
ar=(length*height)/2;
cout<<"area = "<<ar<<endl;
}

};

int main() {
int l,h;
cout<<"enter length of the base of the right angled triangle: "<<endl;
cin>>l;
cout<<"enter height the right angled triangle: "<<endl;
cin>>h;

triangle t1(l,h);
t1.hypotenuse();
t1.area();

return 0;
}

Q4.Create a class named 'Programming'. While creating an object of the


class, if nothing is passed to it, then the message "I love programming
languages" should be printed. If some String is passed to it, then in place of
"programming languages" the name of that String variable should be printed.
For example, while creating the object if we pass "CPP", then "I love CPP"
should be printed.
Program:
//adding libraries
#include <iostream>
using namespace std;
//creating class
class programming{
public:
void function(){
string input;
cout<<"Choose your CPP Languages"<<endl;
cout<<"I Love Programming Languages"<<endl;
cout<<"Enter Language Name"<<endl;
cin>>input;
if(input=="c++" || input=="cpp"){
cout<<"I love Cpp"<<endl;
}
else{
cout<<"Programming languages"<<endl;
}
}
};
int main() {
programming p1;
p1.function();
return 0;
}
Q5.Imagine a tollbooth at a bridge. Cars passing by the booth are expected to
pay a 50-cent toll. Mostly they do, but sometimes a car goes by without
paying. The tollbooth keeps track of the number of cars that have gone by,
and of the total amount of money collected. Model this tollbooth with a class
called tollBooth. The two data items are a type unsigned int to hold the total
number of cars, and a type double to hold the total amount of money
collected. A constructor initializes both of these to 0. A member function
called payingCar() increments the car total and adds 0.50 to the cash total.
Another function, called nopayCar(), increments the car total but adds
nothing to the cash total. Finally, a member function called display() displays
the two totals. Make appropriate member functions const. Include a program
to test this class. This program should allow the user to push one key to count
a paying car, and another to count a nonpaying car. Pushing the Esc key
should cause the program to print out the total cars and total cash and then
exit.
Program:

#include <iostream>
using namespace std;
class Tollboth //class declaretion
{
//attributes
int cars;
double collect;
public:
//contstructor
Tollboth()
{
cars=0;
collect=0;
}
//methods
void payingcar() //paying car method
{
cars++;
collect+=0.50;
cout<<"Entry Succesfully added."<<endl;
}

void nopayingcar() //nonpaying car method


{
cars++;
cout<<"Entry Succesfully added."<<endl;
}

void display() //methods to display


{
cout<<"Total numbers of cars: "<<cars<<endl;
cout<<"Total payment: "<<collect<<endl;
}
};
int main()
{
int ask;
char ag;
Tollboth a; // create objects
again: //lable
cout<<"Press 1 if payment is delievered by car owner.\n";
cout<<"Press 2 if payment is not delievered by car owner.\nPress 3 to display
total payment and cars.\nPress 1 or 2 or 3: "<<endl;
//ask paying cara or nonpaying car.
cin>>ask;
if(ask==1)
{
a.payingcar();
}
else if(ask==2)
{
a.nopayingcar();
}
else if(ask==3)
{
a.display();
}
else
{
cout<<"Enter Valid Data."<<endl;
goto again;
}
//ask to calculation again.
cout<<"You like to enter data again? Y/N ";
cin>>ag;
if(ag=='Y' || ag=='y')
{
goto again; //goto lable again at above.
}
}

You might also like