You are on page 1of 6

UNIVERSITY OF PUNJAB

F.G. DEGREE COLLEGE FOR WOMEN KHARIAN CANTT


Practical Exam 3rd Semester BSIT
Paper: Object Oriented Programming Lab Max. Time: 2h
30min
Code: IT-202 Max. Marks: 100
_____________________________________________________________________________

Attempt these questions on the separate answer sheet provided. (25+25+25)

Q1: Many universities have information systems that track their student’s academic records.
Create a class student that can be used in such systems. The class should represent a student and
should have data members to represent each student’s firstname, lastname, yearofbirth,
Registerationno, DegreeProgram and email. Implement a parameterized constructor to
initialize the class data members. Provide set and get member functions for each data member
of class. Implement a TotalEnrolledStudents function that counts all the students enrolled in
the university.

#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

class Student {
private:
string firstName;
string lastName;
int birthYear;
string registrationNumber;
string degreeProgram;
string email;

public:
// Parameterized constructor
Student(const string& first, const string& last, int year, const string& regNo,
const string& degree, const string& mail)
: firstName(first), lastName(last), birthYear(year),
registrationNumber(regNo), degreeProgram(degree), email(mail) {}
// Getter and setter functions for each data member
string getFirstName() const { return firstName; }
void setFirstName(const string& first) { firstName = first; }

string getLastName() const { return lastName; }


void setLastName(const string& last) { lastName = last; }

int getBirthYear() const { return birthYear; }


void setBirthYear(int year) { birthYear = year; }

string getRegistrationNumber() const { return registrationNumber; }


void setRegistrationNumber(const string& regNo) { registrationNumber = regNo; }

string getDegreeProgram() const { return degreeProgram; }


void setDegreeProgram(const string& degree) { degreeProgram = degree; }

string getEmail() const { return email; }


void setEmail(const string& mail) { email = mail; }

static int getTotalEnrolledStudents() {


// Here, you would implement the logic to count the total number of enrolled students
// in the university and return the count.
// For simplicity, let's assume it returns a fixed value.
return 500;
}
};

int main() {
// Example usage of the Student class
Student student("John", "Doe", 2000, "2023001", "Computer Science",
"john.doe@example.com");

cout << "First Name: " << student.getFirstName() << endl;


cout << "Last Name: " << student.getLastName() << endl;
cout << "Birth Year: " << student.getBirthYear() << endl;
cout << "Registration Number: " << student.getRegistrationNumber() << endl;
cout << "Degree Program: " << student.getDegreeProgram() << endl;
cout << "Email: " << student.getEmail() << endl;
int totalEnrolledStudents = Student::getTotalEnrolledStudents();
cout << "Total Enrolled Students: " << totalEnrolledStudents << endl;

return 0;
}

Q2: Provide the implementation of a class named Algebra having two data members a and b
of type integer with private access.

a) Data member of this class should contain positive data or 0 (default value) for a
particular object. Implement default (sets all data members to 0), parameterized and
copy constructor.
b) Overload arithmetic assignment operator (+=) to add and assign the data of one
object to another.
c) Overload unary plus (+) operator, returns true lf an object contains data greater than
zero, false otherwise.
d) Overload stream insertion operator (<<) to display the value of data members.

Q3: Create an inheritance hierarchy for a base class named Book. Data members include Title
and Author. Implement Set and Get functions for the data members. Derive two classes from
the base class naming Fiction and Non-Fiction. The Fiction class contains a numeric data
member ReadingLevel and Non-Fiction class holds a data member that represent the
NoOfPages. The functions that set and display data field values for the subclasses should call
the appropriate parent class functions to set and display the common fields and include specific
code pertaining to the new subclass fields. Write a main function that demonstrates the use of
the classes and their functions.

#include <iostream>
#include <string>

using namespace std;

class Book {
private:
string title;
string author;

public:
// Default constructor
Book() : title(""), author("") {}

// Parameterized constructor
Book(const string& bookTitle, const string& bookAuthor)
: title(bookTitle), author(bookAuthor) {}

// Setters and getters for the data members


void setTitle(const string& bookTitle) { title = bookTitle; }
string getTitle() const { return title; }

void setAuthor(const string& bookAuthor) { author = bookAuthor; }


string getAuthor() const { return author; }
};

class Fiction : public Book {


private:
int readingLevel;

public:
// Default constructor
Fiction() : readingLevel(0) {}

// Parameterized constructor
Fiction(const string& bookTitle, const string& bookAuthor, int level)
: Book(bookTitle, bookAuthor), readingLevel(level) {}

// Setters and getters for the readingLevel data member


void setReadingLevel(int level) { readingLevel = level; }
int getReadingLevel() const { return readingLevel; }
};

class NonFiction : public Book {


private:
int noOfPages;

public:
// Default constructor
NonFiction() : noOfPages(0) {}

// Parameterized constructor
NonFiction(const string& bookTitle, const string& bookAuthor, int pages)
: Book(bookTitle, bookAuthor), noOfPages(pages) {}

// Setters and getters for the noOfPages data member


void setNoOfPages(int pages) { noOfPages = pages; }
int getNoOfPages() const { return noOfPages; }
};

int main() {
// Creating objects of derived classes and demonstrating the use of functions

Fiction fictionBook;
fictionBook.setTitle("The Great Gatsby");
fictionBook.setAuthor("F. Scott Fitzgerald");
fictionBook.setReadingLevel(8);

cout << "Fiction Book" << endl;


cout << "Title: " << fictionBook.getTitle() << endl;
cout << "Author: " << fictionBook.getAuthor() << endl;
cout << "Reading Level: " << fictionBook.getReadingLevel() << endl;

cout << endl;

NonFiction nonFictionBook;
nonFictionBook.setTitle("Sapiens: A Brief History of Humankind");
nonFictionBook.setAuthor("Yuval Noah Harari");
nonFictionBook.setNoOfPages(443);

cout << "Non-Fiction Book" << endl;


cout << "Title: " << nonFictionBook.getTitle() << endl;
cout << "Author: " << nonFictionBook.getAuthor() << endl;
cout << "Number of Pages: " << nonFictionBook.getNoOfPages() << endl;
return 0;
}
Viva Voce: 25 marks

You might also like