Cskill Quiz 04
Cskill Quiz 04
Cskill Quiz 04
20-ARID-3964
BSCS (B)
2ND SEMESTER
QUESTION NO 01:
What are the FOUR main characteristics of Object-Oriented Programming? Explain with
concise example.
ANSWER:
“Object-oriented programming”
Object-oriented programming is based on the concept of objects. In object-oriented
programming data structures or objects are defined, each with its own properties or attributes.
Each object can also contain its own procedures or methods. Software is designed by
using objects that interact with one another.
Encapsulation
Encapsulation is accomplished when each object maintains a private state, inside a class. Other
objects cannot access this state directly; instead, they can only invoke a list of public functions. The
object manages its own state via these functions and no other class can alter it unless explicitly
allowed.
For example:
#include<iostream>
class Encapsulation
private:
int x;
public:
// variable x
void set(int a)
x =a;
// variable x
int get()
return x;
}
};
// main function
int main()
Encapsulation obj;
obj.set(5);
cout<<obj.get();
return 0;
Output:
5
Explanation:
In the above program, the variable x is made private. This variable can be accessed and manipulated
only using the functions get() and set() which are present inside the class. Thus we can say that here,
the variable x and the functions get() and set() are together which is nothing but encapsulation
Abstraction
Abstraction is an extension of encapsulation. It is the process of selecting data from a larger pool
to show only the relevant details to the object. Suppose you want to create a dating application and
you are asked to collect all the information about your users. You might receive the following data
from your user: Full name, address, phone number, favorite food, favorite movie, hobbies, tax
information, social security number, and credit score. This amount of data is great however not all of
it is required to create a dating profile. You only need to select the information that is pertinent to
your dating application from that pool. Data like Full name, favorite food, favorite movie, and
hobbies make sense for a dating application. The process of fetching/removing/selecting the user
information from a larger pool is referred to as Abstraction. One of the advantages of Abstraction is
being able to apply the same information you used for the dating application to other applications
with little or no modification
Abstraction in C++. We can use access specifiers to enforce restrictions on class members.
Members declared as public in a class, can be accessed from anywhere in the program.
Members declared as private in a class, can be accessed only from within the class. They are
not allowed to be accessed from any part of code outside the class.
For example:
#include <iostream>
class implementAbstraction
private:
int a, b;
public:
// method to set values of
// private members
a = x;
b = y;
void display()
};
int main()
implementAbstraction obj;
obj.set(10, 20);
obj.display();
return 0;
}
Output:
a = 10
b = 20
Explanation:
You can see in the above program we are not allowed to access the variables a and b directly,
however one can call the function set() to set the values in a and b and the function display() to
display the values of a and b.
Polymorphism:
The word polymorphism means having many forms. In simple words, we can define polymorphism
as the ability of a message to be displayed in more than one form. A real-life example of
polymorphism, a person at the same time can have different characteristics. Like a man at the same
time is a father, a husband, an employee. Therefore, the same person possess different behavior in
different situations. This is called polymorphism.
For example:
// C++ program for polymorphism
#include <iostream>
Geeks obj1;
Explanation:
In the above example, a single function named func acts differently in three different situations,
which is the property of polymorphism.
Inheritance:
The capability of a class to derive properties and characteristics from another class is
called Inheritance. Inheritance is one of the most important feature of Object Oriented
Programming.
Sub Class: The class that inherits properties from another class is called Sub class or Derived
Class.
Super Class: The class whose properties are inherited by sub class is called Base Class or Super
class.
For example:
// C++ program to implementation of Inheritance
#include <iostream>
using namespace std;
//Base class
class Parent
{
public:
int id_p;
};
// Sub class inheriting from Base Class(Parent)
class Child : public Parent
{
public:
int id_c;
};
//main function
int main()
{
Child obj1;
Explanation:
In the above program the ‘Child’ class is publicly inherited from the ‘Parent’ class so the public data
members of the class ‘Parent’ will also be inherited by the class ‘Child’.
Question No 2
Write a C++ program to develop a hospital management system in which the user enter the
i. doctor's private details i.e. name and it's specialization by a constructor name of
doctor(),
ii. the patient's private details i.e. name and number by using a constructor
patients_details()
iii. the ward_number of the patient must be entered by a user using a constructor Ward()
iv. the dues of a patient according to the department(e.g. heart specialist Rs 1500/- to be
paid) must be entered to get appointment in the class name Dues using a member method
enter_fee()
v. Note: enter the amount of at least 5 specialist(all there fee must vary)
vi. All these user entered record must be shown/displayed in call to display() from the
main body of program.