Oriental College of Technology: Ritika Makhija
Oriental College of Technology: Ritika Makhija
Oriental College of Technology: Ritika Makhija
05/12/2020
COLLEGE OF
TECHNOLOGY
OBJECT ORIENTED PROGRAMMING
AND METHODOLOGY
ASSIGNMENT
RITIKA MAKHIJA
ENRL NO. 0126CS191091
CSE-B 3rd SEM
ASSIGNMENT-1
QUE.1) What is the difference between OOP and POP?
ANS.
BASIS FOR
POP OOP
COMPARISON
oriented .
class.
"protected".
BASIS FOR
POP OOP
COMPARISON
operators.
member functions.
keyword "friend".
BASIS FOR
POP OOP
COMPARISON
inheritance.
C#.NET.
#include<iostream>
using namespace std;
class Encapsulation
{
private:
// data hidden from outside world
int x;
public:
// function to set value of
// variable x
void set(int a)
{
x =a;
}
// function to return value of
// variable x
int get()
{
return x;
}
};
// main function
int main()
{
Encapsulation obj;
obj.set(5);
cout<<obj.get();
return 0;
}
ASSIGNMENT-3
QUE.1) What is inheritance and its type? Explain with
program.
ANS. Inheritance is one of the feature of OOPM, it allows the
child class to acquire the properties (the data members) and
functionality (the member functions) of parent class.
The main advantages of inheritance are code
reusability and readability. When child class inherits the
properties and functionality of parent class, we need not to
write the same code again in child class. This makes it easier to
reuse the code, makes us write the less code and the code
becomes much more readable.
Types of Inheritance in C++
1) Single inheritance
2) Multilevel inheritance
3) Multiple inheritance
4) Hierarchical inheritance
5) Hybrid inheritance
Single inheritance
In Single inheritance one class inherits one class exactly.
For example: Lets say we have class A and B
2)Multilevel Inheritance
In this type of inheritance one class inherits another child class.
Multiple Inheritance
In multiple inheritance, a class can inherit more than one class.
This means that in this type of inheritance a single child class
can have multiple parent classes.
4)Hierarchical Inheritance
In this type of inheritance, one parent class has more than one
child class.
5) Hybrid Inheritance
Hybrid inheritance is a combination of more than one type of
inheritance. For example, A child and parent class relationship
that follows multiple and hierarchical inheritance both can be
called hybrid inheritance.
Aggregation:
An aggregation is a collection, or the gathering of things
together. This relationship is represented by a “has a”
relationship. In other words, aggregation is a group, body, or
mass composed of many distinct parts or individuals For
example, phone number list is an example of aggregation.
QUE.3) Describe
abstract class with
program.
ANS. an abstract class in C++ is a class that has at
least one pure virtual function (i.e., a function that has no
definition). The classes inheriting the abstract
class must provide a definition for the pure virtual function;
otherwise, the subclass would become an abstract class itself.
Abstract classes are essential to providing an abstraction to the
code to make it reusable and extendable. For example,
a Vehicle parent class with Truck and Motorbike inheriting from
it is an abstraction that easily allows more vehicles to be added.
However, even though all vehicles have wheels, not all vehicles
have the same number of wheels – this is where a pure virtual
function is needed.
#include<iostream>
using namespace std;
class Base
{
int x;
public:
virtual void fun() = 0;
int getX() { return x; }
};
// This class inherits from Base and implements fun()
class Derived: public Base
{
int y;
public:
void fun() { cout << "fun() called"; }
};
int main(void)
{
Derived d;
d.fun();
return 0;
}
ASSIGNMENT-4
QUE.1) What is polymorphism?
ANS. 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. So the same person posses different
behavior in different situations. This is called polymorphism.
Polymorphism is considered as one of the important features of
Object Oriented Programming.
In C++ polymorphism is mainly divided into two types:
Compile time Polymorphism
Runtime Polymorphism
ASSIGNMENT-5
QUE.1) Explain exception handling in details.
ANS. One of the advantages of C++ over C is Exception
Handling. Exceptions are run-time anomalies or abnormal
conditions that a program encounters during its execution. There
are two types of exceptions: a)Synchronous,
b)Asynchronous(Ex:which are beyond the program’s control,
Disc failure etc). C++ provides following specialized keywords
for this purpose.
try: represents a block of code that can throw an exception.
catch: represents a block of code that is executed when a
particular exception is thrown.
throw: Used to throw an exception. Also used to list the
exceptions that a function throws, but doesn’t handle itself.
Why Exception Handling?
Following are main advantages of exception handling over
traditional error handling.
1) Separation of Error Handling code from Normal Code: In
traditional error handling codes, there are always if else
conditions to handle errors. These conditions and the code to
handle errors get mixed up with the normal flow. This makes the
code less readable and maintainable. With try catch blocks, the
code for error handling becomes separate from the normal flow.
2) Functions/Methods can handle any exceptions they
choose: A function can throw many exceptions, but may choose
to handle some of them. The other exceptions which are thrown,
but not caught can be handled by caller. If the caller chooses not
to catch them, then the exceptions are handled by caller of the
caller.
In C++, a function can specify the exceptions that it throws using
the throw keyword. The caller of this function must handle the
exception in some way (either by specifying it again or catching
it)
3) Grouping of Error Types: In C++, both basic types and
objects can be thrown as exception. We can create a hierarchy
of exception objects, group exceptions in namespaces or
classes, categorize them according to types.
Input Functions
1. getline() :- This function is used to store a stream of
characters as entered by the user in the object memory.
2. push_back() :- This function is used to input a character at
the end of the string.
3. pop_back() :- Introduced from C++11(for strings), this
function is used to delete the last character from the string.
Capacity Functions
4. capacity() :- This function returns the capacity allocated to
the string, which can be equal to or more than the size of the
string. Additional space is allocated so that when the new
characters are added to the string, the operations can be done
efficiently.
5. resize() :- This function changes the size of string, the size
can be increased or decreased.
6.length():-This function finds the length of the string
7.shrink_to_fit() :- This function decreases the capacity of the
string and makes it equal to the minimum capacity of the string.
This operation is useful to save additional memory if we are
sure that no further addition of characters have to be made.
Iterator Functions
8. begin() :- This function returns an iterator to beginning of
the string.
9. end() :- This function returns an iterator to end of the string.
10. rbegin() :- This function returns a reverse iterator pointing
at the end of string.
11. rend() :- This function returns a reverse iterator pointing
at beginning of string.
Manipulating Functions
12. copy(“char array”, len, pos) :- This function copies the
substring in target character array mentioned in its
arguments. It takes 3 arguments, target char array, length to
be copied and starting position in string to start copying.
13. swap() :- This function swaps one string with other.