You are on page 1of 5

Air University A&AC Kamra

Mid-Term Examination, Spring 2023


Subject: Object Oriented Programming Student ID:
Course Code: CS112 Date: 28-03-2023
Class: BSCYS 2 nd Time: 02 Hrs (0900:1100)
Semester: Spring 2023 Faculty Name: Mr. Tariq Aziz
Section(s): A Marks: 50

Q. No. 1 CLO1, PLO1, C2 Marks: 20

Provide brief answers to the following questions:


i. What does the term abstraction mean in OOP?
Abstraction is a fundamental concept in Object-Oriented Programming (OOP) that refers to the
process of distilling complex systems into simpler, higher-level representations that capture their
essential properties and behaviors.
In OOP, abstraction is achieved through the use of abstract classes, interfaces, and other abstract
data types that define a set of common behaviors or properties that can be implemented by
different objects in different ways. These abstract definitions provide a way to model complex
systems in a more intuitive and manageable way, by focusing on the essential aspects of the
system and abstracting away the details that are not relevant to the problem at hand.
Abstraction helps to reduce complexity and increase the reusability of code, by creating modular,
composable building blocks that can be combined to create larger, more complex systems. It also
helps to improve the maintainability and scalability of code, by making it easier to modify or
extend the behavior of individual components without affecting the rest of the system.

ii. How polymorphism aid in OOP paradigm?


Polymorphism is a feature of object-oriented programming languages that allows a specific
routine to use variables of different types at different times. Polymorphism in programming
gives a program the ability to redefine methods for derived classes.

iii. What is the benefit of using Inheritance in OOP?


Code reuse: By inheriting from an existing class, a new class can reuse the code of the parent
class, reducing the amount of code that needs to be written and promoting code reuse across
different parts of a program.
Modularity: Inheritance promotes a modular approach to software design, by allowing related
classes to be organized into a hierarchy, with common behaviors and attributes defined at higher
levels of the hierarchy and more specialized behaviors and attributes defined at lower levels.
Flexibility: Inheritance allows new classes to be created by extending existing classes, while still
retaining the ability to override or modify the behavior of the parent class. This makes it easier to
create variations on existing classes without needing to rewrite large amounts of code.

iv. Define the term encapsulation and how it is achieved in OOP?


Encapsulation is a fundamental principle of Object-Oriented Programming (OOP) that refers to
the practice of binding together the data and methods that operate on that data, and controlling
access to them through well-defined interfaces.
In OOP, encapsulation is typically achieved through the use of classes, which define a set of data

1
fields and methods that operate on those fields. The data fields and methods are bound together
within the class, creating a self-contained unit that can be used to model a particular concept or
behavior in a program.

v. What do you mean by subtyping (extension)?


Subtyping, also known as extension, is a concept in Object-Oriented Programming (OOP) that
allows a new class to be created by extending an existing class, while also adding new properties,
methods, or behaviors.
In subtyping, the new class, called the subclass, inherits all of the properties and methods of the
existing class, called the superclass, and can then add additional properties or methods to create a
more specialized version of the superclass. The subclass can also override existing methods of the
superclass, allowing it to modify or extend the behavior of the superclass.

vi. How specialization (restriction) can be achieved in OOP?


Method overriding: By overriding one or more methods of the superclass, a subclass can restrict
or modify the behavior of the superclass to better suit its own needs.
Access modifiers: By using access modifiers such as private or protected, a subclass can restrict
access to certain properties or methods of the superclass, preventing them from being used or
modified outside of the subclass.
Interface implementation: By implementing a specific interface, a subclass can restrict itself to
a specific set of methods or behaviors defined by the interface, while still inheriting from a
superclass and possibly adding additional properties or methods.
Sub-Classing: By creating a new class that inherits from a superclass, but with a narrower set of
properties or methods, a subclass can specialize the behavior of the superclass to better suit its
own needs.

vii. What is overriding? Support your answer with an example.


Overriding is a feature in Object-Oriented Programming (OOP) that allows a subclass to provide
a different implementation of a method that is already defined in its superclass. When a method is
overridden, the implementation of the method in the subclass is used instead of the
implementation in the superclass.
For example, we have a superclass Animal with a speak() method that prints "Animal is
speaking". We also have two subclasses Cat and Dog that inherit from Animal and override the
speak() method with their own implementation.

viii. Differentiate between Abstract class & Concrete class

 Abstract class:
o An abstract class is a class that cannot be instantiated.
o It is designed to serve as a base class for other classes to inherit from.
o It contains at least one pure virtual function, which means that the function has no
implementation in the abstract class and must be overridden by any derived class that
inherits from it.
o Abstract classes are used to create a common interface for a group of related classes, and
to define a set of operations that the derived classes must implement.
 Concrete class:
o A concrete class is a class that can be instantiated.
o It is a complete class that has a defined implementation for all of its methods.
o A concrete class does not have any pure virtual functions.

2
o It can be instantiated directly, and can also be used as a base class for other classes to
inherit from.

ix. What does the term information hiding means and how it is achieved in Object Oriented
Model?
Information hiding is a principle of software design that aims to minimize the dependencies
between different parts of a program by restricting access to implementation details and
only exposing a well-defined interface to other components. This helps to keep the code
modular, easier to maintain and modify, and reduces the risk of introducing bugs or errors.
In Object-Oriented Programming (OOP), information hiding is achieved through
encapsulation. Encapsulation is a technique of binding together the data and methods that
operate on that data, and controlling access to them through well-defined interfaces.
By encapsulating the data and methods within a class, other components of the program
can only access them through the public interfaces provided by the class, which hides the
implementation details and protects the internal state of the object from external
modification. This allows for better control over the behavior and state of objects in a
program, and helps to prevent unintended consequences and errors.

x. Define constructor overloading? Provide an example to support your understanding.


Constructor overloading is a feature in Object-Oriented Programming (OOP) that allows multiple
constructors to have the same name but with different parameters. This means that a class can
have multiple ways of creating objects with different initializations, depending on the parameters
passed to the constructor.
Example: Rectangle(int l, int w) //constructor with 2 parameters
#include <iostream> { length = l; width = w; } Rectangle rect3(3, 5);
using namespace std; int area()
class Rectangle { return length * width; } cout << "\nRectangle 1 area: " <<
{ }; rect1.area();
private: cout << "\nRectangle 2 area: " <<
int length, width; void main() rect2.area();
public: { cout << "\nRectangle 3 area: " <<
Rectangle() // default constructor rect3.area();
{ length = 0; width = 0; } Rectangle rect1; }
Rectangle(int l) // constructor with 1 parameter
{ length = l; width = 0; } Rectangle rect2(4);

Q. No. 2 CLO2, PLO2, C3 Marks: 15


i. (05) Identify the error in the following code. Protected to Public
ii. (10) After correcting, write the output of the following code. Output will be invalid if the
code is not corrected.
#include <iostream> }
#include <string> Student(string n, string id, int year)
using namespace std; {
cout<<"\nParent Parameterized
const int MATH_HOURS = 20; Constructor";
const int CS_HOURS = 40; set(n, id, year);
const int GEN_ED_HOURS = 60; }

class Student void set(string n, string id, int year)


{ {
protected: name = n;
string name; idNumber = id;

3
string idNumber; yearAdmitted = year;
int yearAdmitted; }

public: const string getName() const


Student() { return name; }
{
cout << "\nParent Default const string getIdNum() const
Constructor"; { return idNumber; }
name = ""; int getYearAdmitted() const
idNumber = ""; { return yearAdmitted; }
yearAdmitted = 0; };

class CsStudent : public Student genEdHours = 0;


{ }
private:
int mathHours; void setMathHours(int mh)
int csHours; { mathHours = mh; }
int genEdHours;
public: void setCsHours(int csh)
CsStudent() : Student() { csHours = csh; }
{
cout << "\nChild Default void setGenEdHours(int geh)
Constructor"; { genEdHours = geh; }
mathHours = 0;
csHours = 0; int getRemainingHours() const
genEdHours = 0; {
} int reqHours, remainingHours;
reqHours = MATH_HOURS + CS_HOURS +
CsStudent(string n, string id, int GEN_ED_HOURS;
year) remainingHours = reqHours -
: Student(n, id, year) (mathHours + csHours + genEdHours);
{ return remainingHours;
cout << "\nChild Parameterized }
Constructor"; };
mathHours = 0;
csHours = 0;
void main()
{
CsStudent student("Jennifer Haynes", "167W98337", 2006);
student.setMathHours(12);
student.setCsHours(20);
student.setGenEdHours(40);
cout << "\nThe student " << student.getName() << " needs to take "
<< student.getRemainingHours() << " more hours to graduate.\n";
}
Parent Parameterized Constructor
Child Parameterized Constructor
The student Jennifer Haynes needs to take 48 more hours to graduate.

4
Q. No. 3 CLO3, PLO4, C6 Marks: 15

In a population, the birth rate and death rate are calculated as follows:
Birth Rate = Number of Births ÷ Population
Death Rate = Number of Deaths ÷ Population
For example, in a population of 100,000 that has 8,000 births and 6,000 deaths per year, the birth rate and
death rate are:
Birth Rate = 8,000 ÷ 100,000 = 0.08
Death Rate = 6,000 ÷ 100,000 = 0.06
Design a Population class that stores a population, number of births, and number of deaths for a period of
time. Member functions should return the birth rate and death rate. Implement the class in a program.
i. Input Validation: Do not accept population figures less than 1, or birth or death numbers less than 0.
ii. The program should continue execution until the user explicitly wants to quit (Sentinel).
iii. Implement the functions in sequence i.e. 3 x Constructors, Set Functions, Other required functions,
get/display functions, destructors.
#include <iostream> else death = z;
#include <iomanip> }
using namespace std; double getBirth()
{ return static_cast<double>(birth); }
class Population
{ double getDeath()
private: { return static_cast<double>(death); }
int population, birth, death; };

public: void main()


Population() {
{ int population, birth, death;
population = 2;
birth = 0; cout << "What is the starting population: ";
death = 0; cin >> population;
}
Population(int x, int y, int z) cout<<"What is the amount of birth and deaths:";
{ cin >> birth >> death;
if (x < 2) population = 0;
else population = x; Population place1(20500, 1000, 4000), place2(population,
birth, death), place3;
if (y < 0) birth = 0;
else birth = y; place3.setPopulation(100000);
place3.setBirth(8000);
if (z < 0) death = 0; place3.setDeath(6000);
else death = z;
} cout << fixed << setprecision(2);
Population(Population & obj)
{ cout<<"\nBirth rate is "<<place1.getBirth();
this->population = obj. population; cout<<"\nDeath rate is "<<place1.getDeath();
this->birth= obj.birth;
this->death=obj.death; cout<<endl<<endl;
}
void setPopulation(int x) cout<<"\nBirth rate is "<<place2.getBirth();
{ cout<<"\nDeath rate is "<<place2.getDeath();
if (x < 2) population = 0;
else population = x; cout<<endl<<endl;
}
void setBirth(int y) cout<<"\nBirth rate is "<<place3.getBirth();
{ cout<<"\nDeath rate is "<<place3.getDeath();
if (y < 0) birth = 0;
else birth = y; }
}
void setDeath(int z)
{
if (z < 0) death = 0;

You might also like