0% found this document useful (0 votes)
30 views

Assignment 1

The document contains 9 questions regarding C++ classes and objects. Each question provides code for a class or classes to solve programming exercises related to concepts like object serialization numbers, date classes, time classes, merging classes, and counting the number of objects created. The student is asked to modify or extend the provided code to meet the requirements of each question.

Uploaded by

Huzaifa Mirza
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Assignment 1

The document contains 9 questions regarding C++ classes and objects. Each question provides code for a class or classes to solve programming exercises related to concepts like object serialization numbers, date classes, time classes, merging classes, and counting the number of objects created. The student is asked to modify or extend the provided code to meet the requirements of each question.

Uploaded by

Huzaifa Mirza
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

ASSIGNMENT – 1

Name : M Huzaifa Abdulahad


Arid-No : 22-ARID-860
Subject : OOP
Submitted-To : Sir Tariq
Task : Solve Ex of CHP 6 ( obj and classes ) ||
from( Robert Lafore Book )
____________________________________________

Q1:INT Class
// ConsoleApplication1.cpp : This file contains the
'main' function. Program execution begins and ends
there.
//

#include <iostream>
using namespace std;
class INT {
int a;
public:
INT() // according to program statment
{
a = 0;
}
INT(int a)
{
this->a = a;
}
int sum(INT num1, INT num2)
{
int add = num1.a + num2.a;
return add;
}
void display()
{
cout << a << endl;
}
};
// by huzaifa
int main()
{
INT obj(20), obj2(20), obj3;// 2 intilize and 1
unintilize acc to given condation
obj3 = obj3.sum(obj, obj2);
obj3.display();
return 0;
}
______________________________________________
______________________________________________
_____________________________
Q2:TOOLBOOTH
// ConsoleApplication1.cpp : This file contains the
'main' function. Program execution begins and ends
there.
//

#include <iostream>
using namespace std;

class TOOLBOOTH {
private:
static int carsCount;
static int nonPayingCars;
static int payingCars;
public:
void payingCars1()
{
++carsCount;
++payingCars;
}
void nonPayingCars1()
{
++nonPayingCars;
carsCount++;
}
void display()
{
cout << "Total-Cars passes by : " << carsCount
<< endl;
cout << "Total-cars that pay tax on booth : " <<
payingCars << endl;
cout << "Total-cars that doesn't pay on booth :
" << nonPayingCars << endl;
cout << "Total-amount we collected is : " <<
payingCars * 0.50 << endl;
}
};
int TOOLBOOTH::carsCount = 0;
int TOOLBOOTH::payingCars = 0;
int TOOLBOOTH::nonPayingCars = 0 ;

int main()
{
TOOLBOOTH obj;
while (true)
{
cout << "Enter 1 for paying cars \n Enter 2 for
non paying cars \n Enter 3 to see record : ";
int option;
cin >> option;
system("cls");
if (option == 1)
{
obj.payingCars1();
}
else if (option == 2)
{
obj.nonPayingCars1();
}
else if (option == 3)
{
obj.display();
system("cls");

}
}
return 0;
}

______________________________________________
______________________________________________
____________________________
Q3 : time problem ?

// ConsoleApplication1.cpp : This file contains the


'main' function. Program execution begins and ends
there.
//

#include <iostream>
using namespace std;
class TIME {
int hours, minutes, seconds;
public :
TIME()
{
hours = 0;
minutes = 0;
seconds = 0;
}
TIME(int hours , int minutes , int seconds )
{
this->hours = hours;
this->minutes = minutes;
this->seconds = seconds;
}
void display()
{
cout << endl << hours << ":" << minutes << ":"
<< seconds << endl;
}
TIME add(TIME t1, TIME t2)
{
TIME temp;
temp.hours = t1.hours + t2.hours;
temp.minutes = t1.minutes + t2.minutes;
temp.seconds = t1.seconds + t2.seconds;
if ( temp.seconds > 60 )
{
int holder = temp.seconds / 60;
temp.seconds -= (holder * 60);
temp.minutes += holder;
if ( temp.minutes > 60 )
{
int tempHolder = temp.minutes / 60;
temp.hours += tempHolder;
temp.minutes -= (tempHolder * 60);
}
}

return temp;
}
};

int main()
{
TIME obj1(2, 30, 60), obj2(5, 60, 56), obj3; // acc to
condation one intilize and 2 uninitilize
obj3 = obj3.add(obj1, obj2);
obj3.display();

return 0;
}

______________________________________________
______________________________________________
____________________________
Q4: employee class
// ConsoleApplication1.cpp : This file contains the
'main' function. Program execution begins and ends
there.
//

#include <iostream>
using namespace std;
class employee {
float compensation;
int employeeNo;
public:
void setter()
{
cout << "Enter your employee number : ";
cin >> employeeNo;
cout << "Enter your compensation in dollers : ";
cin >> compensation;
}
void getter()
{
cout << "\n Employee-No : " << employeeNo <<
"\n Employee-Compensation : " << compensation << "$"
<< endl;
}
};

int main()
{
employee allowUseroEnterRecOFthreeEmp[3];
cout << "Enter recod " << endl;
for (int i = 0; i < 3; i++)
{
allowUseroEnterRecOFthreeEmp[i].setter(); //
set value
}
system("cls");
for (int i = 0; i < 3; i++)
{
allowUseroEnterRecOFthreeEmp[i].getter(); //
get value
}

return 0;
}

______________________________________________
______________________________________________
_____________________________

Q5 : modify chp 4 program ?


// ConsoleApplication1.cpp : This file contains the
'main' function. Program execution begins and ends
there.
//

#include <iostream>
using namespace std;
class Date {
int month, day, year;
public :
void setDate()
{
cout << "Enter month : ";
cin >> month;
cout << "Enter year : ";
cin >> year;
cout << "Enter day : ";
cin >> day;

}
void getDate()
{
cout << "\n " << month << "/" << day << "/" <<
year << endl;
}
};

int main()
{
Date obj;
obj.setDate();
obj.getDate();

return 0;
}

______________________________________________
______________________________________________
______________________________

Q6 : merge employee with date ? employee + date


classes ?

// ConsoleApplication1.cpp : This file contains the


'main' function. Program execution begins and ends
there.
//

#include <iostream>
using namespace std;
class Date {
int month, day, year;
public :
void setDate()
{
cout << "Enter month : ";
cin >> month;
cout << "Enter year : ";
cin >> year;
cout << "Enter day : ";
cin >> day;

}
void getDate()
{
cout << "\n " << month << "/" << day << "/" <<
year << endl;
}
};
class Employee { // as we dont study enum so im not
using that
Date hiringDate;
int employeeNo;
float compensation;
public :
void setter()
{
cout << "Enter hiring date of employee " <<
endl;
hiringDate.setDate();
cout << "Enter employee no : ";
cin >> employeeNo;
cout << "Enter compensation : ";
cin >> compensation;
}
void getter()
{
cout << "\nHiring date is : ";
hiringDate.getDate();
cout << "\nEmploye no : " << employeeNo << "\
n Employee Compensation : " << employeeNo << "$" <<
endl;
}

};

int main()
{
Employee obj, obj2, obj3; // for three employee acc
to condation
obj.setter();
obj.getter();
obj2.setter();
obj2.getter();
obj3.setter();
obj3.getter();
return 0;
}

______________________________________________
______________________________________________
______________________________
Q7 : time , angle , minutes ?
#include <iostream>
#include<string>
using namespace std;
class direction
{
int angleIndegree;
float minutes;
char directionEntry;
public :
// condation of question write 3 argument con and
then display it
direction()
{
angleIndegree = 0;
minutes = 0;
directionEntry = 0;
}
direction(int angleIndegree, float minutes, char
directionEntry)
{
this->angleIndegree = angleIndegree;
this->minutes = minutes;
this->directionEntry = directionEntry;
}
void setter()
{

cout << "Enter degree : ";


cin >> angleIndegree;
cout << "Enter time : ";
cin >> minutes;
cout << "Enter Direction(E,W,N,S) :";
cin >> directionEntry;

}
void getter()
{
cout << "\n" << angleIndegree << "\xF8" <<
minutes << "' " << directionEntry; // acc to program
format
}
};

int main()
{
direction obj(22, 59.9, 'E');
obj.getter();
// another condation of question : get entry from
user again and again and then dispay
while ( true )
{
cout << endl;
obj.setter(); // set value of class data members
obj.getter(); // get value from class and then
dispay it
}
return 0;
}
______________________________________________
______________________________________________
______________________________
Q8:Serial no?

#include <iostream>
#include<string>
using namespace std;
class objSerialNumChecker {
int serialRecHolder; // for each obj its hold obj
serial no
static int counter; // declaring
public:
objSerialNumChecker()
{
serialRecHolder = 0; // erasing garbage value
counter++;
serialRecHolder = counter;
}
void mySerialNum()
{
cout << "\n Through which object you invoke
me has a serial num : " << serialRecHolder;
}
void totalCountOfObj()
{
cout << "\n The total obj of this class is : " <<
counter;
}
};
int objSerialNumChecker::counter = 0; // defining
int main()
{
objSerialNumChecker obj, obj2, obj3;
obj3.mySerialNum();
obj.totalCountOfObj();
return 0;
}

______________________________________________
______________________________________________
______________________________
Q9 : count how many obj is created ? (im obj no ....)
(guess which keyword is gonna use ) ?

// ConsoleApplication1.cpp : This file contains the


'main' function. Program execution begins and ends
there.
//

// ConsoleApplication1.cpp : This file contains the


'main' function. Program execution begins and ends
there.
//

#include <iostream>
using namespace std;
class ques9 {
int static objCounter; // defining
int totalObj = 0;
public:
ques9()
{
objCounter++;
totalObj = objCounter;
cout << "\n I'm obj no : " << objCounter;
}
void checkTotalObjTillNow()
{
cout << "Toal no of objs is : " << objCounter <<
endl;
}
};
int ques9::objCounter = 0;// declaring

int main()
{
ques9 a, b, c;// just run program
a.checkTotalObjTillNow();
return 0;
}
______________________________________________
______________________________________________
______________________________
Q10:ship location plus serial num ?

sol:

#include <iostream>
#include<string>
using namespace std;
class ship{
int shipSerialHolder; // separatly holds obj serial
code
int angle;
float time;
char location;
static int counter;
public:
ship()
{
shipSerialHolder = 0; // fist removing all
garbage value
counter++;
shipSerialHolder = counter;
}
void setter()
{
cout << "\n Enter the angle : ";
cin >> angle;
cout << "Enter the time :";
cin >> time;
cout << "Enter the Direction(N,E,W,S) : ";
cin >> location;
}
void getter()
{
cout << "This ship serial num is : " << "00000"
<< shipSerialHolder << endl;
cout << "In ocean location is : " << angle << "\
xF8" << time << "' " << location << endl;
}

};
int ship::counter = 0;

int main()
{
ship arr[3]; // acc to condation
for (int i = 0; i < 3; i++)
{
arr[i].setter();
}
system("cls");
for (int i = 0; i < 3; i++)
{
arr[i].getter();
}
return 0;
}

You might also like