Untitled Document
Untitled Document
Untitled Document
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; }
int main() {
// Example usage of the Student class
Student student("John", "Doe", 2000, "2023001", "Computer Science",
"john.doe@example.com");
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>
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) {}
public:
// Default constructor
Fiction() : readingLevel(0) {}
// Parameterized constructor
Fiction(const string& bookTitle, const string& bookAuthor, int level)
: Book(bookTitle, bookAuthor), readingLevel(level) {}
public:
// Default constructor
NonFiction() : noOfPages(0) {}
// Parameterized constructor
NonFiction(const string& bookTitle, const string& bookAuthor, int pages)
: Book(bookTitle, bookAuthor), noOfPages(pages) {}
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);
NonFiction nonFictionBook;
nonFictionBook.setTitle("Sapiens: A Brief History of Humankind");
nonFictionBook.setAuthor("Yuval Noah Harari");
nonFictionBook.setNoOfPages(443);