Вы находитесь на странице: 1из 24

Amity Campus

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.

ASSIGNMENT DETAILS MARKS


Assignment A Five Subjective Questions 10
Assignment B Three Subjective Questions + Case Study 10
Assignment C Objective or one line Questions 10

b) Total weightage given to these assignments is 30%. OR 30 Marks

c) All assignments are to be completed as typed in word/pdf.

d) All questions are required to be attempted.

e) All the three assignments are to be completed by due dates and need to be submitted for
evaluation by Amity University.

f) The students have to attached a scan signature in the form.

Signature : ________k.g_________________________
Date : ______7/05/2013_______________________

( ) Tick mark in front of the assignments submitted


Assignment Assignment B Assignment C
A
Object Oriented Programming

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.

Q2) Explain mechanism in Object Oriented Programming System?

Object-oriented programming (OOP) is a programming paradigm using "objects" data


structures consisting of data fields and methods together with their interactions to design
applications and computer programs. Programming techniques may include features such as data
abstraction, encapsulation, messaging, modularity, polymorphism, and inheritance. Many
modern programming languages now support OOP.

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.

One of the principal advantages of object-oriented programming techniques over procedural


programming techniques is that they enable programmers to create modules that do not need to
be changed when a new type of object is added. A programmer can simply create a new object
that inherits many of its features from existing objects. This makes object-oriented programs
easier to modify.
To perform object-oriented programming, one needs an object-oriented programming language
(OOPL). Java, C++ and Smalltalk are three of the more popular languages, and there are also
object-oriented versions

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.

Q5) Write Short notes on:-


a) Friend Function

Friends are functions or classes declared with the friend keyword.

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.

The general form of an operator function is ::


return type class name :: operator op(arglist)
{
Function Body // task defined
}
Where return type :: type of value returned by the specified operation, op the operator being
overloaded.

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.

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.

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.

Let us take a real world example::


Consider an example of a Television which consists of features such as changing the channel,
ON/OFF switch etc. User interacts with television using interfaces like channel changer, volume
controller, and power button without going in to internal details of the working of each of the
interface. The user is not concerned about how it receives signals over the air, translate them and
display on the screen. So, it shows how the internal complexity of working of the television is
hidden from the user.

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.

In other words, j is a pointer variable containing address of I (i.e 2201).

Pointer variables are also known as address variables as it always stores addresses of some other
variable.

Example of pointer declaration is int *iptr; //pointer to int type 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 rules include the following:


Constructor is a member function with the same name as that of the class.

Constructor is called automatically at the time of object creation.

Constructor is used to initialize the members, thus turning raw memory into objects.

Constructor has no return types.

Constructor can be overloaded.

A Destructor is called implicitly when an object is destroyed. If no destructor is provided


compiler provides a default destructor.

The destructor must have the same name as the class, but preceded with a tilde sign (~).

Destructor cannot take arguments and hence cannot be overloaded.

Destructor cannot return values.

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.

For example in the code below:

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.

The syntax for access specifier is:

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

Q6) Write a C++ program to transpose a matrix using class concept.

#include <vector>
#include <iostream>
#include <iomanip>

using namespace std;

template <typename T>


class matrix : public vector< vector<T>* > {
public:
// Default constructor
matrix() :
vector< vector<T>* >(), _rows(0), _cols(0) {
cout << "matrix default constructor" << endl;
}
// Full constructor
matrix(size_t rows, size_t cols) :
vector< vector<T>* >(), _rows(rows),_cols(cols) {
for (size_t i = 0; i < _rows; i++) {
this->push_back(new vector<T>(_cols,T(0)));
}
}
// Destructor
~matrix() {
for (size_t i = 0; i < _rows; i++) {
delete (*this)[i];
}
}
// Set m[x][y] = val
void setVal(size_t x, size_t y, T val) {
vector<T> *v = (*this)[x];
(*v)[y] = val;
}
// Get m[x][y]
T getVal(size_t x, size_t y) {
vector<T> *v = (*this)[x];
return (*v)[y];
}
// Return transpose(m)
matrix *transpose() {
matrix *m1 = new matrix(_cols,_rows);
for (size_t i = 0; i < _rows; i++) {
for (size_t j = 0; j < _cols; j++) {
m1->setVal( j,i,this->getVal(i,j) );
}
}
return m1;
}
// Print matrix
void print(const string &s) {
typename vector< vector<T>* >::iterator i;
typename vector<T>::iterator j;
cout << endl << s << ":" << endl;
i = this->begin();
while (i != this->end()) {
j = (*i)->begin();
while (j != (*i)->end()) cout << (*j++) << " ";
cout << endl;
++i;
}
}
private:
size_t _rows;
size_t _cols;
};
int main(int argc, char *argv[]) {
size_t rows = 2, cols = 3;
matrix<int> m(rows,cols);
for (size_t i = 0; i < rows; i++) {
for (size_t j = 0; j < cols; j++) {
m.setVal(i,j,(i+1) + (j*cols));
}
}
m.print("M");
matrix<int> *mT = m.transpose();
mT->print("transpose(M)");
delete mT;
return 0;
}

#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

Q2) The value of EOF is


a) 1
b) 0
c) Infinity
d) -1
Answer d

Q3) Using pointers to call a function is called as


a) call by value
b) call by reference
c) call by address
d) All the Above
Answer b

Q4) The variable that contains address of another variable is called as


a) Pointer
b) Arrays
c) Unions
d) None of the Above
Answer a

Q5) Inheritance in C++ have default access specifier as


a) Private
b) Public
c) Protected
d) None of the Above
Answer a

Q6) How many values can be returned by a C++ function?


a) 1
b) Infinity
c) 0
d) None of the Above
Answer a

Q7) The array exforsys[10] can be declared using pointers as a) % exforsys[]


b) & exforsys[]
c) * exforsys[]
d) @ exforsys[]
Answer c

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

Q9) The class that in C++ for file input is


a) Ifstream
b) Ofstream
c) Both A and B
d) The class that in C++ for file input is
Answer a
Q10) Which of the following is an error statement in C++ array declaration?
a) int exforsys(10);
b) float exforsys[5][5];
c) int exforsys[5];
d) None of the Above
Answer a

Q11) The members of a class are by default


a) Private
b) Public
c) Protected
d) None of the Above
Answer a

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

Q14) The other name for external variables in C++ is


a) static variables
b) register variables
c) global variables
d) None of the Above
Answer c

Q15) A function in a C++ program can be called


a) Only Once
b) Cannot be called at all
c) Any number of times
d) None of the Above
Answer c

Q16) Which of the following denotes feature of OOPS?


a) Inheritance
b) Encapsulation
c) Polymorphism
d) All the Above
Answer d

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

Q21) Which of the following denote incorrect data type in C++?


a) real
b) double
c) float
d) int
Answer a

Q22) The isolation of data from direct access by a C+ program is called as


a) Data Hiding
b) Data Isolation
c) Data Encapsulation
d) None of the Above
Answer a

Q23) Reference to its own class can be accepted by


a) simple constructor
b) copy constructor
c) Both A and B
d) None of the Above
Answer b

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

Q25) The actual implementation is present in


a) declaration
b) definition
c) Both A and B
d) None of the Above
Answer b

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

Q28) A function defined within a class is called as


a) Member Function
b) Class Function
c) Object Function
d) None of the Above
Answer a

Q29) The private member in derived class


a) Cannot be inherited
b) Can be inherited at all instances
c) Can be inherited only if the derived class is inheriting from base class with private access
level
d) None of the Above
Answer c

Q30) Which of the following denote types of polymorphism in C++?


a) Virtual function
b) Function overloading
c) Operator Overloading
d) All the Above
Answer d

Q31) An instance of a user-defined type is called


a) Class
b) Object
c) Method
d) None of the Above
Answer b
Q32) Which of the following can be used to initialize a newly declared variable from an existing
variable?
a) Virtual Function
b) Namespaces
c) copy constructor
d) None of the Above
Answer c

Вам также может понравиться