2 marks (b) How do the calls of the functions f and h affect the states of the o
ID: 3904054 • Letter: 2
Question
2 marks (b) How do the calls of the functions f and h affect the states of the objects p and q of class Point in each of the examples. Explain your answers i. void f (Point p, Point q) t [2 marks] ii. void h(Point *p, Point q) t p->x = 42; p-l: [2 marks] (c) Write a class for an object called Job that has two integer variables startingTime and completionTime. Job should also have a default constructor and two pub- lic functions getProcessingTime and reschedule with the signatures: int getProcessingTime ); void reschedule (int neuStartingTime, int nevCompletionTime) Function getProcessingTime returns the processing time as the difference between completionTime and startingTime Function reschedule changes the values of startingTime and completionTime, respectively, to newStartingTime and newCompletionTime when the new com- pletion time is greater than or equal to the new starting time. Otherwise, no action is performed. The default constructor initialises both startingTime and completionTime to 0. Assuming that the C++ code has been declared separately, write the C+it body code for these functions and the default constructor 6 marks]Explanation / Answer
(b) PROGRAM with Examplanation
#include<iostream>
using namespace std;
class Point
{
public: int x; // declare member integer variable x
void f(Point p,Point q)
{
p.x=42;
p=q;
}
void h(Point *p,Point *q)
{
p->x=42;
p=q;
}
};
int main()
{
Point point,p,q;
// Calling f() and receive p.x value is 1
// because calling directly without pointer then we receive value is 1
// when calling q.x value is -1, becasue just assign value of p=q
point.f(p,q); // calling f() where two ordinary objects
cout<<"Ordinary Object P Object Value: "<<p.x<<endl;
// Calling h() and receive &p.x value is 42
// because calling from memory of object p value
// when calling q.x value is -1, because just assign value of p=q
point.h(&p,&q); // calling h() where objects using reference(&) since two formal parameters are pointer objects
cout<<"Pointer Object P Value: "<<p.x<<endl;
return 0;
}
OUTPUT
Ordinary Object P Object Value: 1
Pointer Object P Value: 42
c) Program
#include<iostream>
using namespace std;
class Job
{
public: int sTime,cTime; // declare two integer variables
Job(){ // declare default constructor and initialize two members 0
sTime=0;
cTime=0;
}
int getProcessingTime() // implmenet getProcessingTime() method
{
return cTime-sTime; // return with subtract compiletime - startingtime
}
void reschedule(int newStartingTime,int newCompletionTime) // implement reschedule() method
{
// assign formal parameters to actual members of class
sTime=newStartingTime;
cTime=newCompletionTime;
if(newCompletionTime<=newStartingTime) // check condition
{
// then display this message
cout<<"No Action Performed... Because Setting Compilation Time <= Starting Time";
exit(0); // then exit from this program
}
}
};
int main()
{
Job job; // create object of Job is job
int st,ct; // declare two integer variables st and ct
cout<<"Initialize Default Constructor"<<endl;
cout<<"Starting Time: "<<job.sTime<<" "<<"Compilation Time: "<<job.cTime<<endl; // display default constructor values
// reading two input values start-time and compile-time
cout<<" Enter Starting Time: "; cin>>st;
cout<<" Enter Compilation Time: "; cin>>ct;
job.reschedule(st,ct); // calling reschedule() function
cout<<"After Setting Schedule Time"<<endl;
cout<<"Starting Time: "<<job.sTime<<" "<<"Compilation Time: "<<job.cTime<<endl; // display after getting values
cout<<"Processing Time is: "<<job.getProcessingTime(); // finally, display processing time
return 0;
}
OUTPUT-1
Initialize Default Constructor
Starting Time: 0 Compilation Time: 0
Enter Starting Time: 5
Enter Compilation Time: 33
After Setting Schedule Time
Starting Time: 5 Compilation Time: 33
Processing Time is: 28
OUTPUT-2
Initialize Default Constructor
Starting Time: 0 Compilation Time: 0
Enter Starting Time: 20
Enter Compilation Time: 5
No Action Performed...
Because Setting Compilation Time <= Starting Time
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.