Oriental College of Technology: Ritika Makhija

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 23

ORIENTAL

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

Basic Procedure/Structure Object oriented.

oriented .

Approach Top-down. Bottom-up.

Basis Main focus is on "how to get Main focus is on 'data

the task done" i.e. on the security'. Hence, only

procedure or structure of a objects are permitted to

program . access the entities of a

class.

Division Large program is divided into Entire program is

units called functions. divided into objects.

Entity accessing No access specifier observed. Access specifier are

mode "public", "private",

"protected".
BASIS FOR
POP OOP
COMPARISON

Overloading or Neither it overload functions It overloads functions,

Polymorphism nor operators. constructors, and

operators.

Inheritance Their is no provision of Inheritance achieved in

inheritance. three modes public

private and protected.

Data hiding & There is no proper way of Data is hidden in three

security hiding the data, so data is modes public, private,

insecure and protected. hence

data security increases.

Data sharing Global data is shared among Data is shared among

the functions in the program. the objects through the

member functions.

Friend functions No concept of friend function. Classes or function can

or friend classes become a friend of

another class with the

keyword "friend".
BASIS FOR
POP OOP
COMPARISON

Note: "friend" keyword

is used only in c++

Virtual classes or No concept of virtual classes . Concept of virtual

virtual function function appear during

inheritance.

Example C, VB, FORTRAN, Pascal C++, JAVA, VB.NET,

C#.NET.

QUE.2) What are oops? What are the main features of


oops?
ANS. Object-Oriented Programming or OOPs refers to
languages that uses objects in programming. Object-oriented
programming aims to implement real-world entities like
inheritance, hiding, polymorphism etc in programming. The main
aim of OOP is to bind together the data and the functions that
operate on them so that no other part of the code can access
this data except that function.
Feature of OOPS

 Emphasis on data rather procedure.


 Programs are divided into units called “Objects”.
 Objects used to communicate with each other through
functions.
 New functionality can be easily developed by creating
objects and function

QUE.3) Describe object model.


ANS. An object model is a logical interface, software or system
that is modeled through the use of object-oriented techniques.
It enables the creation of an architectural software or system
model prior to development or programming.
An object model is part of the object-oriented programming
(OOP) lifecycle.
An object model helps describe or define a software/system in
terms of objects and classes. It defines the interfaces or
interactions between different models, inheritance,
encapsulation and other object-oriented interfaces and
features.

Object model examples include:

 Document Object Model (DOM): A set of objects that


provides a modeled representation of dynamic HTML and
XHTML-based Web pages
 Component Object Model (COM): A proprietary
Microsoft software architecture used to create software
components
QUE.4) Differentiate between class and object.
ANS.
BASIS FOR
OBJECT CLASS
COMPARISON

Definition An instance of a A template or blueprint with

class is known as which objects are created is

Object. known as Class.

Type of entity Physical Logical

Creation Object is invoked by Class is declared by using class

new keyword. keyword.

Memory allocation Creation of object The formation of a class doesn't

consumes memory. allocate memory.


ASSIGNMENT-2
QUE.1) What is a constructor and destructor? Explain with
program.
ANS. A constructor is a member function of a class that has the
same name as the class name. It helps to initialize the object of
a class. It can either accept the arguments or not. It is used to
allocate the memory to an object of the class. It is called
whenever an instance of the class is created. It can be defined
manually with arguments or without arguments. There can be
many constructors in class. It can be overloaded but it can not
be inherited or virtual. There is a concept of copy constructor
which is used to initialize a object from another object.

 Like constructor, deconstructor is also a member function of a


class that has the same name as the class name preceded by a
tilde(~) operator. It helps to deallocate the memory of an object.
It is called while object of the class is freed or deleted. In a class,
there is always a single destructor without any parameters so it
can’t be oveloaded. It is always called in the reverse order of the
constructor. if a class is inherited by another class and both the
classes have a destructor then the destructor of the child class
is called first, followed by the destructor of the parent or base
class. 
Example-
#include <iostream>
using namespace std;
class Line {
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor
private:
double length;
};
// Member functions definitions including constructor
Line::Line(void) {
cout << "Object is being created" << endl;
}
void Line::setLength( double len ) {
length = len;
}
double Line::getLength( void ) {
return length;
}

// Main function for the program


int main() {
Line line;
// set line length
line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl;
return 0;
}

QUE.2) what is encapsulation? Explain with program


ANS. Encapsulation is an Object Oriented Programming
concept that binds together the data and functions that
manipulate the data, and that keeps both safe from outside
interference and misuse. Data encapsulation led to the
important OOP concept of data hiding.
Data encapsulation is a mechanism of bundling the data, and
the functions that use them and data abstraction is a
mechanism of exposing only the interfaces and hiding the
implementation details from the user.
C++ supports the properties of encapsulation and data hiding
through the creation of user-defined types, called classes. We
already have studied that a class can contain private,
protected and public members. By default, all items defined
in a class are private.

#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;
}

QUE.3) Describe data abstraction in details?


ANS. Data abstraction is one of the most essential and
important feature of object oriented programming in C++.
Abstraction means displaying only essential information and
hiding the details. Data abstraction refers to providing only
essential information about the data to the outside world, hiding
the background details or implementation.
Consider a real life example of a man driving a car. The man
only knows that pressing the accelerators will increase the
speed of car or applying brakes will stop the car but he does not
know about how on pressing accelerator the speed is actually
increasing, he does not know about the inner mechanism of the
car or the implementation of accelerator, brakes etc in the car.
This is what abstraction is.

Abstraction using Classes: We can implement Abstraction in


C++ using classes. Class helps us to group data members and
member functions using available access specifiers. A Class
can decide which data member will be visible to outside world
and which is not.
Abstraction in Header files: One more type of abstraction in
C++ can be header files. For example, consider the pow()
method present in math.h header file. Whenever we need to
calculate power of a number, we simply call the function pow()
present in the math.h header file and pass the numbers as
arguments without knowing the underlying algorithm according
to which the function is actually calculating power of numbers.
Abstraction using access specifiers
Access specifiers are the main pillar of implementing abstraction
in C++. We can use access specifiers to enforce restrictions on
class members. For example:

 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.
We can easily implement abstraction using the above two
features provided by access specifiers. Say, the members that
defines the internal implementation can be marked as private in
a class. And the important information needed to be given to the
outside world can be marked as public. And these public
members can access the private members as they are inside
the class.

QUE.4) Explain access modifier in cpp?


ANS. Access modifiers are used to implement an important
aspect of Object-Oriented Programming known as Data Hiding.
Consider a real-life example: 
The Research and analysis wing (R&AW), having 10 core
members has come in possession of sensitive confidential
information regarding national security. Now we can corelate
these core members to data members or member functions of a
class which in turn can be corelated to the R&A wing. These 10
members can directly access confidential information from their
wing (the class), but anyone apart from these 10 members can’t
access this information directly i.e. outside functions other than
those prevalent in the class itself can’t access information that is
not entitled to them, without having either assigned privileges
(such as those possessed by friend class and inherited class as
will be seen in this article ahead) or access to one of these 10
members who is allowed direct access to the confidential
information (similar to how private members of a class can be
accessed in the outside world through public member functions
of the class that have direct access to private members). This is
what data hiding is in practice. 
Access Modifiers or Access Specifiers in a class are used to
assign the accessibility to the class members. That is, it sets
some restrictions on the class members not to get directly
accessed by the outside functions.
There are 3 types of access modifiers available in C++: 
1. Public
2. Private
3. Protected
Note: If we do not specify any access modifiers for the members
inside the class then by default the access modifier for the
members will be Private.

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.

QUE.2) What is association and aggregation?


ANS. Association:
An association is defined as an organization of people with a
common purpose and having a formal structure. It represents a
binary relationship between two objects that describes an
activity. It is a relationship between objects. For example, A
doctor can be associated with multiple patients.

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;
}

QUE.4) Explain IS-A relationship?


ANS. In object-oriented programming, the concept of IS-A is a
totally based on Inheritance, which can be of two types Class
Inheritance or Interface Inheritance. It is just like saying "A is a
B type of thing". For example, Apple is a Fruit, Car is a Vehicle
etc. Inheritance is uni-directional. For example, House is a
Building. But Building is not a House.
It is a key point to note that you can easily identify the IS-A
relationship. Wherever you see an extends keyword or
implements keyword in a class declaration, then this class is
said to have IS-A relationship.

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

QUE.2) Explain static and runtime polymorphism?


ANS. 1) Compile time Polymorphism
Function overloading and Operator overloading are perfect
example of Compile time polymorphism.

Compile time Polymorphism Example


In this example, we have two functions with same name but
different number of arguments. Based on how many
parameters we pass during function call determines which
function is to be called, this is why it is considered as an
example of polymorphism because in different conditions the
output is different. Since, the call is determined during compile
time thats why it is called compile time polymorphism.
2) Runtime Polymorphism
Function overriding is an example of Runtime polymorphism.
Function Overriding: When child class declares a method,
which is already present in the parent class then this is called
function overriding, here child class overrides the parent class.

In case of function overriding we have two definitions of the


same function, one is parent class and one in child class. The
call to the function is determined at runtime to decide which
definition of the function is to be called, thats the reason it is
called runtime polymorphism.

QUE.3) Explain method overloading with program


ANS. Function overloading is a C++ programming feature that
allows us to have more than one function having same name
but different parameter list, when I say parameter list, it means
the data type and sequence of the parameters, for example the
parameters list of a function myfuncn(int a, float b) is (int,
float) which is different from the function myfuncn(float a, int
b) parameter list (float, int). Function overloading is a compile-
time polymorphism.
Now that we know what is parameter list lets see the rules of
overloading: we can have following functions in the same
scope.
#include <iostream>
using namespace std;
class Addition {
public:
    int sum(int num1,int num2) {
        return num1+num2;
  }
    int sum(int num1,int num2, int num3) {
    return num1+num2+num3;
  }
};
int main(void) {
Addition obj;
    cout<<obj.sum(20, 15)<<endl;
    cout<<obj.sum(81, 100, 10);
   return 0;
}

QUE.4) Explain operator overloading with program.


ANS. In C++, we can make operators to work for user defined
classes. This means C++ has the ability to provide the operators
with a special meaning for a data type, this ability is known as
operator overloading.
For example, we can overload an operator ‘+’ in a class like
String so that we can concatenate two strings by just using +.
Other example classes where arithmetic operators may be
overloaded are Complex Number, Fractional Number, Big
Integer, etc.
#include<iostream>
using namespace std;
  
class Complex {
private:
    int real, imag;
public:
    Complex(int r = 0, int i =0)  {real = r;   imag = i;}
      
    // This is automatically called when '+' is used with
    // between two Complex objects
    Complex operator + (Complex const &obj) {
         Complex res;
         res.real = real + obj.real;
         res.imag = imag + obj.imag;
         return res;
    }
    void print() { cout << real << " + i" << imag << endl; }
};
  
int main()
{
    Complex c1(10, 5), c2(2, 4);
    Complex c3 = c1 + c2; // An example call to "operator+"
    c3.print();
}

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.

QUE.2) Explain string in cpp and its function.


ANS. Strings are objects that represent sequences of
characters.

The standard string class provides support for such objects with


an interface similar to that of a standard container of bytes, but
adding features specifically designed to operate with strings of
single-byte characters.

The string class is an instantiation of the basic_string class


template that uses char (i.e., bytes) as its character type, with
its default char_traits and allocator types (see basic_string for
more info on the template).

Note that this class handles bytes independently of the


encoding used: If used to handle sequences of multi-byte or
variable-length characters (such as UTF-8), all members of this
class (such as length or size), as well as its iterators, will still
operate in terms of bytes (not actual encoded characters).

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.

QUE.3) Describe multithreading.


ANS. Multithreading is a specialized form of multitasking and a
multitasking is the feature that allows your computer to run two
or more programs concurrently. In general, there are two types
of multitasking: process-based and thread-based.
Process-based multitasking handles the concurrent execution
of programs. Thread-based multitasking deals with the
concurrent execution of pieces of the same program.
A multithreaded program contains two or more parts that can
run concurrently. Each part of such a program is called a
thread, and each thread defines a separate path of execution.
C++ does not contain any built-in support for multithreaded
applications. Instead, it relies entirely upon the operating
system to provide this feature.

You might also like