0% found this document useful (0 votes)
70 views7 pages

OOP Lab MID TERM SOLUTION (Maybe)

oop lab midterm solution of UCP

Uploaded by

alihamza4851
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views7 pages

OOP Lab MID TERM SOLUTION (Maybe)

oop lab midterm solution of UCP

Uploaded by

alihamza4851
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Object Oriented Programming

Mid-Term Examination (Fall 2022)


Reg. No: __________________ Time: 90mins
Q1 (25)
class Student {
char* Name;
int semester;
double CGPA;
double scholarship;

int length (const char* cptr);


public:
Student (const char *name_, int semester_, double cgpa_, double scholarship_) {
int len = length(name_); //Assume length function is given. Do not
// write code for length function.
Name = new char[len+1];
for (int i=0; i < len; ++i){
Name[i] = name_[i];
Name[len]=’\0’;
this->semester = semester_;
CGPA = cgpa_;
scholarship = scholarship_;

}
};

Student doSomething(Student ss){ // It is not important what this function does.


… // Do not write code for this function.
}

Solve the following for the above class.


int main ( ) {
Student s1(“Zameer Iqbal”, 7, 3.35, 2500);
Student s2(“Aleena Sheikh”, 3, 3.25, 1500);
Student s3(“Happy Cow”, 4, 2.12, 0);
s3 = doSomething(s1);
cout << s3 << endl; // Prints all the data members of an object.
}

Write all the required functions (members as well as non-members) such that the
int main ( ) function works correctly with no logical errors.

(Note: DO not write any getter and setter functions for this question. Writing getters and
setters will result in deduction of marks.)

Faculty of Information Technology Page 1 of 7


University of Central Punjab
Object Oriented Programming
Mid-Term Examination (Fall 2022)
Reg. No: __________________ Time: 90mins

I deduct 7marks if any getter or setter function(s) is written in Q1.


Ans:
//helper function written in private: part of the class
void Student::buildObject(const Student& obj){
int len = length (obj. Name);
Name = new char[len+1];
for (int i=0; i < len; ++i) {
Name[i] = obj. Name[i];
Name[len]=’\0’;
semester = obj. semester;
CGPA = obj.CGPA ;
scholarship = obj.scholarship ;
}

(5 marks) //Copy Constructor for Line 4

Student::Student (const Student& obj){


buildObject(obj);
}

(8 marks) //Assignment operator for Line 4

Student& Student::operator = (const Student& obj){


if (this != &obj) {
delete [] Name;
buildObject(obj);
}
return *this;
}

(5 marks) //destructor

Student::~Student (){
If (Name)
delete [] Name;
}

Faculty of Information Technology Page 2 of 7


University of Central Punjab
Object Oriented Programming
Mid-Term Examination (Fall 2022)
Reg. No: __________________ Time: 90mins

(7 marks) //Operator << overloading for Line 5


// The output does not have to be so nice as given below

friend ostream& operator<< (ostream&, const Student&); //declaration of friend function


//inside the Student class.

// Function definition outside the class

ostream& operator<< (ostream& os, const Student& obj){


os << “Name: ” << obj.Name
<< “Semester: “ << obj.semster << endl
<< “CGPA: “ << obj.CGPA << endl
<< “Scholarship: “ << obj.scholarship << endl;
return os;
}

Faculty of Information Technology Page 3 of 7


University of Central Punjab
Object Oriented Programming
Mid-Term Examination (Fall 2022)
Reg. No: __________________ Time: 90mins

Q2 (20)
a) For the Student class of Q1, given above, write the required functions so that the main
function given below executes successfully. Do not write code for ‘cout’ and overloaded
constructor as they are part of Q1.
b) Write the output of the program.

int main ( ) {
Student s1 (“Happy Cow”, 4, 3.5, 5000);
cout << s1++ << endl; //s1++ increases the scholarship of s1 by 10%.
// However, if scholarship of object is zero (0), then it
// increases it by 500.0
s1[0] = ‘N’;
cout << s1 << endl;

const Student s2(“Unhappy Cow”, 4, 2.5, 0);

s1[6] = s2[8];
cout << s1 << endl;
}

Faculty of Information Technology Page 4 of 7


University of Central Punjab
Object Oriented Programming
Mid-Term Examination (Fall 2022)
Reg. No: __________________ Time: 90mins
Ans:
a)
(5 marks) //postfix increment operator
char operator++(int) {
Student temp{*this);
if (scholarship > 0) {
scholarship = scholarship * 0.10;
else scholarship = 500.0 ;

return temp;
}

(5 marks) //indexing operator


// Assumption: idx is not out of bound
char& operator[](int idx){
return Name[idx]; // no need of error checking
}

(5 marks) //indexing operator


// Assumption: idx is not out of bound
const char& operator[](int idx) const {
return Name[idx]; // no need of error checking
}

Faculty of Information Technology Page 5 of 7


University of Central Punjab
Object Oriented Programming
Mid-Term Examination (Fall 2022)
Reg. No: __________________ Time: 90mins

b)
// The output of the program is as follows, according to code I have written.
(5 marks)

Name: Happy Cow


Semester: 4
CGPA: 3.5
Scholarship: 5000

Name: Nappy Cow


Semester: 4
CGPA: 3.5
Scholarship: 5500

Name: Nappy Cow


Semester: 4
CGPA: 3.5
Scholarship: 5500

Faculty of Information Technology Page 6 of 7


University of Central Punjab
Object Oriented Programming
Mid-Term Examination (Fall 2022)
Reg. No: __________________ Time: 90mins

Q3 (10)
For the Student class of Q1, given above, write the required member(s)/function(s) so that the
main function given below executes successfully.
Modify cout of Q1 so that it prints an additional member, University, to which all students
created through this Student class belong. This member is not part of any object of Student class.
It is also constant and its value is “University of Central Punjab”.
int main () {
Student sArray[10];
Student ss(“Object Oriented Programming”, 3, 2.0, 0);
cout << ss << endl;
}

Ans:

(2 marks)
Student::Student() = default;

(3 marks)
//data member declaration in the private part of Student class declaration

static const char* University;

(3 marks)
//data member initialization outside of Student class declaration, preferably, immediately
// after Student class declaration.

const char* Student::University = “University of Central Punjab”;

(2 marks)
//modification in cout code

ostream& operator<< (ostream& os, const Student& obj){


os << “Name: ” << obj.Name
<< “Semester: “ << obj.semster << endl
<< “CGPA: “ << obj.CGPA << endl
<< “Scholarship: “ << obj.scholarship
<< “University: “ << obj.University << endl;
return os;
}

Faculty of Information Technology Page 7 of 7


University of Central Punjab

You might also like