Object Oriented Programming Assignment
Object Oriented Programming Assignment
Uttar Pradesh
India 201303
ASSIGNMENTS
PROGRAM: PGD IT
SEMESTER-II
Subject Name : OBJECT ORIENTED PROGRAMMING
Study COUNTRY : UGANDA
Roll Number (Reg.No.) : PGDIT01152012-2013045
Student Name : KIIZA GODFREY
INSTRUCTIONS
a) Students are required to submit all three assignment sets.
e) All the three assignments are to be completed by due dates and need to be submitted for
evaluation by Amity University.
Signature : ________k.g_________________________
Date : ______7/05/2013_______________________
Assignment A
Q1) Differentiate between basic data types, derived data types and user defined data types?
Basic data types are the simplest form of data types which include int, double, float. They are the
kind of data types that the system has got to be aware before it takes it in.
Derived data types unlike basic data types are those that are defined by the system including
pointer, array, and reference. User defined derived datatypes are on the other hand built on
existing types .these include class, struct, union and enumerations.
A type of programming in which programmers define not only the data type of a data structure,
but also the types of operations (functions) that can be applied to the data structure. In this way,
the data structure becomes an object that includes both data and functions. In addition,
programmers can create relationships between one object and another. For example, objects can
inherit characteristics from other objects.
Classification
In principle, packaging data and procedures together makes perfect sense.
Suppose we have many objects of the same type- for example a thousand product objects,
each of which could report its current price. Data values would differ from one product to
the next.
But the methods for dealing with these data might well be the same. Do we have to copy
these methods and duplicate them in every object? No, this would be extremely
inefficient.
All object-oriented languages provide a simple way of capturing these commonalties in a
single place. That place is called a class. The class acts as a kind of template for objects
of similar nature.
Polymorphism
Polymorphism is a Greek word meaning many forms.
It is used to express the fact that the same message can be sent to many different objects
and interpreted in different ways by each object.
For example, we could send the message "move" to many different kinds of objects. They
would all respond to the same message, but they might do so in very different ways. The
move operation will behave differently for a window and differently for a chess piece.
Inheritance
Inheritance is the sharing of attributes and operations among classes on a hierarchical
relationship.
A class can be defined as a generalized form and then it specialized in a subclass.
Each subclass inherits all the properties of its superclass and adds its own properties in it.
For example, a car and a bicycle are subclasses of a class road vehicle, as they both
inherit all the qualities of a road vehicle and add their own properties to it.
Abstraction
In object oriented programming, classes use the concept of Abstraction for storing data.
A class encapsulates the relevant data and functions that operate on data by hiding the
complex implementation details from the user.
In other words ,user needs to focus on what a class does rather than how it does it.
Q3) Explain about passing parameters to as function in Call by Value and Call by Reference and
then distinguish between them?
Call by value and call by reference are ways used to make available to a function values or
parameters that are stored in variables by passing them to that function.
In call by value, the value passed to the function as a parameter is a copy of the original value.
Therefore the function does not change the original value.
In call by reference parameters are passed by referring to the value in a particular memory
location using pointers. Unlike call by value the function can change the value pertaining to that
particular memory location because the reference' is actually a pointer to the address of the
function or parameter involved.
Q4) What is Template? Explain the concept of Template in C++?
A template is a feature of C++ that allows functions and classes to operate with generic data
types by allowing them to work on different data types without having to be rewritten for each
one thus enabling the writing of reusable code.
Templates enable generic programming by making it possible to define generic classes and
functions thus reducing the code size by eliminating the need to overload functions
Two types of templates exist:
1.Function templates: generic functions which can handle different data types without the need
to write separate code for each of them. The syntax for function templates is:
template<Class T>
T function_name(T par1, T par2,.)
{ // template function body}
Function templates enable the writing of generic functions using single implementation that
operates on different data types.
2. Class templates: These are used to create generic classes using class templates that provide
same functionality for different data types thus enabling generic programming. The syntax for
class template is:
Template <class T>
Class classname
{
// class member
// specification with anonymous type
// T wherever appropriate.
}
Once the code is written as a class template, it can support multiple data types.For instance the
stack data structure. A stack may store items of any data type but not a mixture of data types
implying that the code used for maintaining a stack of integers can also work for a stack of float
or character data types. Instead of creating a separate stack class for different data types, a
template for a stack class may be created and used to define stacks that can store different data
types.
We can declare an external function as friend of a class. This allows this function to have access
to the private and protected members of this class.
For this, we should declare a prototype of this external function within the class, and preceding it
with the keyword friend.
A situation where we would like classes to share a particular function. Forexample, consider a
case where two classes manager and scientist have been defined. We would like to use a
function income_tax() to operate on the objects of both these classes. In such cases C++ allows
the common function to be made friendly with both the classes, thereby allowing the function to
have access to the private data of these classes.
To make an outside function friendly to a class, we have to simply declare this function as a
friend of the class.
b) Operator Overloading
Consider the statement c=a+b; here the + operator can be used to perform additional operation
on operands a and b which can either be of int, float, double types. Thus the + operator is
performing multiple tasks as it can perform addition of any of the built in types and hence
overloaded. However if a,b,c are variables (objects) of user defined type (class) then the
statement c=a+b; will not work as compiler does not understand how to add variables (objects)
of user defined type (class). The compiler will give us an error if addition of two objects is
carried out. So, in order to make the above operation work for the variables of user defined type
we need to overload the + operator explicitly by making a function to perform the desired task.
This property of giving special meaning to existing operators so that they can work with
variables of user defined type is known as operator overloading.
To define an additional task to an operator, we must specify what it means in relation to the class
to which the operator is applied. This is done with the help of special function called operator
function, which describes the task.
c) Inheritance
Inheritance is the sharing of attributes and operations among classes on a hierarchical
relationship.
Inheritance allows creating classes which are derived from other classes, so that they
automatically include some of its "parent's" members, plus its own.
The original class is called the base class and the new class created using base class is called
derived class.
Each subclass inherits all the properties of its superclass and adds its own properties in it.
For example, a car and a bicycle are subclasses of a class road vehicle, as they both inherit
all the qualities of a road vehicle and add their own properties to it.
d) Abstraction
Abstraction is the representation of the essential features of an object. These are encapsulated
into an abstract data type.
A good abstraction is the one that emphasizes details that are significant and suppresses details
that are not.
In object oriented programming, classes use the concept of abstraction. A class encapsulates the
relevant data and functions that operate on data by hiding the complex implementation details
from the user. In other words, user needs to focus on what a class does rather than how it will be
implemented.
e) Pointer
A Pointer is a variable that contains the address of another variable in memory. The value saved
into a pointer type variable is interpreted as memory address.
Pointer is like any other variable but the value it stores is the address of some other variable.
Suppose I is an integer variable having value 10 stored at address 2201 and address of i is
assigned to variable j, then j is a pointer variable pointing to memory address stored in it.
Pointer variables are also known as address variables as it always stores addresses of some other
variable.
We can initialize a pointer variable with address of a variable with the help of & operator.
Be careful about the data types. We can initialize a pointer to int type with the address of an
int type variable only. The same applies to all other data types.
f) Constructor & Destructor
Constructor is a member function in a class which is automatically called whenever a new object
of this class is created.
Constructor is used to initialize the members, thus turning raw memory into objects.
The destructor must have the same name as the class, but preceded with a tilde sign (~).
Assignment B
Q1) Explain I-O Stream file?
Iostream is a header file which is used for handling input/output operations in C++ . It is part
of the C++ standard library. The name stands for Input/Output Stream. Iostream provides basic
input and output services for C++ programs. The term stream refers to the flow of data which
may be from an input device such as a keyboard, disk file etc to a program in main memory or
from a program to any output device such as a monitor, printer, disk files etc.). Iostream uses the
objects cin, cout for sending data to and from the standard streams input, output respectively
and, cerr, and clog for error (unbuffered), and error (buffered) respectively. As part of the C++
standard library, these objects are a part of the std namespace.
For instance to use the iostream to print the words hello world to the screen the program would
be written as:
#include <iostream>
int main()
{
std::cout << "Hello, world!\n";
return 0;
}
Q2) Write a C++ program to create a data file with following attributes. S.No, name, age.
Accept n persons information and display the same.
##include <iostream>
int main()
{
int a;
cout <<"Enter your S.No: ";
cin >> a;
string b;
cout <<"\nEnter your name: ";
cin >> b;
int c;
cout <<"\nEnter your age: ";
cin >> c;
cout <<"\nYour S.No is"<< a;
cout <<"\nYour name is"<< b;
cout <<"\nYour age is"<< c;
return 0;
}
Q3) Write short note on Encapsulation and Data Binding with an Example?
Encapsulation is the process of combining data and functions into a single unit called class.. Data
encapsulation led to the important concept of data binding and hiding. Data hiding is whereby
the implementation details of a class are hidden from the user. The data is hidden in a class and
this class is available only through methods. The programmer cannot directly access the data.
This leads to the concept of data binding Data binding refers to the creating the data or variables
as well as functions that will operate on this data into a single(one) unit refered to as a class.Data
is bound to and only accessible through the functions present inside the class. Typically, only
the object's own methods can directly inspect or manipulate its fields. It is an information hiding
mechanism whereby the internal representation of an object of a class is generally hidden from
view outside of the object's definition. This concept of restricted access leads to writing of
specialized methods for performing the operations on hidden members of the class.
Data binding also introduces the ability to store data in variables for programming use.
Hiding the implementation details and providing restrictive access leads to the concept of
abstract data type. Although encapsulation leads to the concept of data binding, it is not restricted
to it. Encapsulation also represents the ability to bundle related data and functionality within a
single, autonomous entity called a class.
class Exforsys
{
public:
int sample();
int example(char *se)
int endfunc();
.........
......... //Other member functions
private:
int x;
float sq;
..........
......... //Other data members
};
In the above example, the data members integer x, float sq and other data members and member
functions sample(), example (char* se), endfunc() and other member functions are bundled and
put inside a single autonomous entity called class Exforsys. This is an example of Encapsulation
and how it leads to the concept of data binding and hiding. The data and functions bundled inside
the class take total control of maintenance and thus human errors are reduced. Although
encapsulated objects provide functionality, the calling objects will not know the implementation
details which enhances the security of the application.
The key strength behind Data Encapsulation in C++ is that the keywords or the access specifiers
can be placed in the class declaration as public, protected or private. A class placed after the
keyword public is accessible to all the users of the class. The elements placed after the keyword
private are accessible only to the methods of the class. In between the public and the private
access specifiers, there exists the protected access specifier. Elements placed after the keyword
protected are accessible only to the methods of the class or classes derived from that class. The
concept of encapsulation shows that a non-member function cannot access an object's private or
protected data.
Q4) Write a C++ program to find factorial of a given number using functions.
#include<iostream.h>
#include<conio.h>
int fact(int a)
{ int p=1,i;
for(i=1;i<=a;i++)
p*=1;
return (p);
}
void main()
{ int no,p;
cout<<"Enter the number";
cin>>no;
p=fact(no);
cout<<"The Factorial of "<< no <<"is"<<p;
getch();
}
Q5) What are access specifier? Explain their purpose and different type of access specifier?
Access specifiers are keywords that control access to classes, methods, and fields. The purpose
of access specifiers is to declare which entity cannot be accessed from where. The access
specifier keyword when applied to a variable, method, etc. is used to specify which other parts of
the program are permitted to access it.
access-specifier : member-list
The access-specifier basically determines the access to the names that follow it, up to the next
access-specifier or the end of the class declaration.
Types of access specifier:
Private- private methods and fields can only be accessed within the same class to which the
methods and fields belong. Private methods and fields are not visible within subclasses and are
not inherited by subclasses
Public- public classes, methods, and fields can be accessed from everywhere.
Protected- protected methods and fields can only be accessed within the same class to which the
methods and fields belong, within its subclasses, and within classes of the same package, but not
from anywhere else
#include <vector>
#include <iostream>
#include <iomanip>
#if 0
Assignment C
Q1) In Late binding the function calls gets resolved during
a) Compile Time
b) Run Time
c) Both A and B
d) None of the Above
Answer b
Q8) The variables that can be used only within the function in which it is declared is called as
a) Global Variables
b) Local Variables
c) Both A and B
d) None of the Above
Answer b
Q12) The header that should be included while using manipulators in C++ is
a iomanip.h
b) manip.h
c) ifstream.h
d) None of the Above
Answer a
Q13) The header file that must be included while using cout function in a C++ program is
a) conio.h
b) math.h
c) iostream.h
d) None of the Above
Answer c
Q17) What is used to convert C++ source code into object modules?
a) Compiler
b) Linker
c) Both A and B
d) None of the Above
Answer a
Q18) Which of the looping structure in C++ check condition at the beginning of loop?
a) do-while
b) while
c) Both A and B
d) None of the Above
Answer b
Q19) A condition that must be true on exit from a member function if called as
a) Precondition
b) Post-condition
c) Both A and B
d) None of the Above
Answer b
Q20) The method that is used for writing characters in C++ program is
a) put
b) get
c) write
d) None of the Above
Answer a
Q24) What is the value of variable z when the following program segment ends? int z; for(z=0;
z<50; z++) {}
a) 51
b) 49
c) 0
d) 50
Answer d
Q26) Which of the following OOPS concepts are used with cin and cout?
a) Encapsulation
b) Data Hiding
c) Operator Overloading
d) None of the Above
Answer c
Q27)The friend function of a class in C++ can access
a) Private members of the class
b) protected members of the class
c) Both A and B
d) None of the Above
Answer c