Professional Documents
Culture Documents
SECTION A
Ans: Synchronous messages, which are associated with an operation, have a send
and a receive message. A message is sent from the source lifeline to the target
lifeline. The source lifeline is blocked from other operations until it receives a
response from the target lifeline.
Asynchronous messages, which are associated with operations, typically have
only a send message, but can also have a reply message. In contrast to a
synchronous message, the source lifeline is not blocked from receiving or sending
other messages. You can also move the send and receive points individually to
delay the time between the send and receive events.
Ans:
call by value call by reference
In call by value, a copy of actual arguments is In call by reference, the location (address) of
passed to formal arguments of the called actual arguments is passed to formal
function and any change made to the formal arguments of the called function. This means
arguments in the called function have no effect by accessing the addresses of actual arguments
on the values of actual arguments in the calling we can alter them within from the called
function. function.
In call by value, actual arguments will remain In call by reference, alteration to actual
safe, they cannot be modified accidentally. arguments is possible within from called
function; therefore the code must handle
arguments carefully else you get unexpected
results.
Ans: Access modifiers are keywords in object oriented languages that set the accessibility of
classes, methods, and other members. There are 3 types of specifiers public, private and protected.
SECTION B
Ans:
Object is an instance of class. Class is an entity in which data and functions are organized.
Each object has a class which defines its data and behavior. When program is executed the
objects interact by sending messages to one another.
Object characterstics are:
-Encapsulation – Encapsulation is capturing data and keeping it safely and securely from
outside interfaces.
-Inheritance- This is the process by which a class can be derived from a base class with all
features of base class and some of its own. This increases code reusability.
-Polymorphism- This is the ability to exist in various forms. For example an operator can be
overloaded so as to add two integer numbers and two floats.
-Abstraction- The ability to represent data at a very conceptual level without any details.
- Emphasis on data rather than procedure
- Programs are divided into entities known as objects
- Data Structures are designed such that they characterize objects
- Functions that operate on data of an object are tied together in data structures
- Data is hidden and cannot be accessed by external functions
- Objects communicate with each other through functions
- New data and functions can be easily added whenever necessary
- Follows bottom up design in program design
• Design
• Implementation
• Process
• Deployment
The center is the Use Case view which connects all these four. A Use Caserepresents the
functionality of the system. Hence, other perspectives are connected with use case.
Design of a system consists of classes, interfaces, and collaboration. UML provides class
diagram, object diagram to support this.
Process defines the flow of the system. Hence, the same elements as used in Design are also
used to support this perspective.
Deployment represents the physical nodes of the system that forms the hardware. UML
deployment diagram is used to support this perspective.
Ans: C++ provides a data structure, the array, which stores a fixed-size sequential
collection of elements of the same type. An array is used to store a collection of data,
but it is often more useful to think of an array as a collection of variables of the same
type.
To declare an array in C++, the programmer specifies the type of the elements and the
number of elements required by an array as follows –
Ans: Converting an expression of a given type into another type is known as type-casting.
1. dynamic_cast
dynamic_cast can be used only with pointers and references to objects. Its purpose is to ensure
that the result of the type conversion is a valid complete object of the requested class.
therefore, dynamic_cast is always successful when we cast a class to one of its base classes:
class CBase { };
class CDerived: public CBase { };
CBase b; CBase* pb;
CDerived d; CDerived* pd;
2. static_cast
static_cast can perform conversions between pointers to related classes, not only from the
derived class to its base, but also from a base class to its derived. This ensures that at least the
classes are compatible if the proper object is converted, but no safety check is performed
during runtime to check if the object being converted is in fact a full object of the destination
type. Therefore, it is up to the programmer to ensure that the conversion is safe. On the other
side, the overhead of the type-safety checks of dynamic_cast is avoided.
3. reinterpret_cast
reinterpret_cast converts any pointer type to any other pointer type, even of unrelated classes.
The operation result is a simple binary copy of the value from one pointer to the other. All
pointer conversions are allowed: neither the content pointed nor the pointer type itself is
checked.
class A {};
class B {};
A * a = new A;
B * b = reinterpret_cast<B*>(a)
4 .const_cast
This type of casting manipulates the constness of an object, either to be set or to be removed.
For example, in order to pass a const argument to a function that expects a non-constant
paramete
#include <iostream>
using namespace std;
int main () {
const char * c = "sample text";
print ( const_cast<char *> (c) );
return 0;
}
SECTION C
3. Attempt any one part of the following: 7x1=7
(a) Discuss JSD and its approach comparing with object modeling techniques.
Ans: JSD, Jackson Structure Design is a methodology to specify and design systems in which
time factor is significant and system may be described using sequence of events. Developed
by Michael A. Jackson , this design method considers the fact that the design of the system is
an extension of the programme design. The purpose of this design method is to create a
maintainable software. The method addresses all stages of the software development life
cycle. It has three phases:
Modeling phase - A JSD model starts with real world consideration. This phase is a part of
analysis process. The aspects of the real world relevant to the system being developed are
modeled in this phase.
Specification phase - This phase focuses on the specification. In this phase JSD determines
what is to be done? The previous phase i.e. the modeling phase provides the basic for the
system specifications to achieve the required functionality.
implementation phase - In this phase , JSD determines how to achieve required functionality.
Operational specifications are executed so that it expresses desired system behavior in terms
of some abstract machine.
(b) Show the difference between reference and pointers. Demonstrate pointer constant and
pointer to constant using example.
Ans:
4. Attempt any one part of the following: 7x1=7
(a) What do you mean by operator overloading? What are the pitfalls of operator
overloading? Explain in brief.
Ans In C++, we can make operators to work for user defined classes. 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;}
void print() { cout << real << " + i" << imag << endl; }
friend Complex operator + (Complex const &, Complex const &);
};
Complex operator + (Complex const &c1, Complex const &c2)
{
return Complex(c1.real + c2.real, c1.imag + c2.imag);
}
int main()
{
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2; // An example call to "operator+"
c3.print();
return 0;
}
1) For operator overloading to work, at leas one of the operands must be a user defined
class object.
2) Assignment Operator: Compiler automatically creates a default assignment
operator with every class. The default assignment operator does assign all members of
right side to the left side and works fine most of the cases (this behavior is same as
copy constructor).
3) Conversion Operator: We can also write conversion operators that can be used to
convert one type to another type.
PITFALLS
• Arity (number of operands) cannot be changed
• Associativity of an operator cannot be changed
• Precedence of an operator cannot be changed
• No new operators can be created
• No overloading operators for built-in types
• Cannot change how two integers are added
• Produces a syntax error
(b) What are the three models in OMT? How object oriented analysis and design attached with
OMT. Explain with example?
Ans The object-modeling technique (OMT) is an object modeling approach
for software modeling and designing. It was developed around 1991 by Rumbaugh as
a method to develop object-oriented systems and to support object-oriented
programming. OMT describes object model or static structure of the system.
OMT was developed as an approach to software development. The purposes of
modeling according to Rumbaugh are:
• Object model: The object model represents the static and most stable phenomena in
the modeled domain. Main concepts are classes and associations with attributes
and operations. Aggregation and generalization (with multiple inheritance) are
predefined relationships.
• Dynamic model: The dynamic model represents a state/transition view on the
model. Main concepts are states, transitions between states, and events to trigger
transitions. Actions can be modeled as occurring within states. Generalization and
aggregation (concurrency) are predefined relationships.
• Functional model: The functional model handles the process perspective of the
model, corresponding roughly to data flow diagrams. Main concepts are process,
data store, data flow, and actors.
The systems designed using OMT are closer to the real world as the real world
functioning of the system is directly mapped into the system designed using OMT.
Because of this, it becomes easier to produce and understand designs.
• The objects in the system are immune to requirement changes because of data hiding
and encapsulation features of object orientation.
• OMT designs encourage more reusability. The classes once defined can easily be
used by other applications. This is achieved by defining classes and putting them into a
library of classes where all the classes are maintained for future use. Whenever a new
class is needed the programmer first looks into the library of classes and if it is
available, it can be used as it is or with some modification. This reduces the
development cost & time and increases quality.
• As the programmer has to spend less time and effort so he can utilize saved time in
concentrating on other aspects of the system.
5. Attempt any one part of the following: 7x1=7
(a) Describe the concept of inheritance and its types with example in c++.
Ans: 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.
Syntax:
class subclass_name : access_mode base_class_name
{
//body of subclass
};
#include <bits/stdc++.h>
using namespace std;
class Parent
{
public:
int id_p;
};
class Child : public Parent
{
public:
int id_c;
};
int main()
{
Child obj1;
obj1.id_c = 7;
obj1.id_p = 91;
cout << "Child id is " << obj1.id_c << endl;
cout << "Parent id is " << obj1.id_p << endl;
return 0;
}
In C++, we have 5 different types of Inheritance. Namely,
1. Single Inheritance
2. Multiple Inheritance
3. Hierarchical Inheritance
4. Multilevel Inheritance
5. Hybrid Inheritance (also known as Virtual Inheritance)
Single Inheritance:
In this type of inheritance one derived class inherits from only one base class. It is the most
simplest form of Inheritance.
Multiple Inheritance
In this type of inheritance a single derived class may inherit from two or more than two base
classes.
Hierarchical Inheritance
In this type of inheritance, multiple derived classes inherits from a single base class.
Multilevel Inheritance
In this type of inheritance the derived class inherits from a class, which in turn inherits from
some other class. The Super class for one, is sub class for the other.
Hybrid Inheritance
Hybrid Inheritance is combination of Hierarchical and Mutilevel Inheritance.
(b) Illustrate Polymorphism in c++ with suitable example.
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.
Real life example of polymorphism, a person at a same time can have different characteristic.
Like a man at a same time is a father, a husband, a employee. So a same person posses have
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
Example: #include <iostream>
using namespace std;
class Shape {
protected:
int width, height;
public:
Shape( int a = 0, int b = 0){
width = a;
height = b;
}
int area() {
cout << "Parent class area :" <<endl;
return 0;
}
};
class Rectangle: public Shape {
public:
Rectangle( int a = 0, int b = 0):Shape(a, b) { }
int area () {
cout << "Rectangle class area :" <<endl;
return (width * height);
}
};
class Triangle: public Shape {
public:
Triangle( int a = 0, int b = 0):Shape(a, b) { }
int area () {
cout << "Triangle class area :" <<endl;
return (width * height / 2);
}
};
int main() {
Shape *shape;
Rectangle rec(10,7);
Triangle tri(10,5);
shape = &rec;
shape->area();
shape = &tri;
shape->area();
return 0;
}
class Sample
{
private:
int x,y;
public:
void setdata(int a,int b)
{
x=a;
y=b;
}
void showdata()
{
cout<<"x="<<x<<"\ny="<<y;
}
friend void swap(Sample &s);
};
int main()
{
Sample s;
int x,y;
cout<<"Enter x = ";
cin>>x;
cout<<"Enter y = ";
cin>>y;
s.setdata(x,y);
cout<<"\nBefore Swapping\n";
s.showdata();
cout<<"\nAfter Swapping\n";
swap(s);
s.showdata();
return 0;
}
Ans: A virtual function a member function which is declared within base class and is re-
defined (Overriden) by derived class. When you refer to a derived class object using a pointer
or a reference to the base class, you can call a virtual function for that object and execute the
derived class’s version of the function.
▪ Virtual functions ensure that the correct function is called for an object, regardless of the
type of reference (or pointer) used for function call.
▪ They are mainly used to achieve Runtime polymorphism
▪ Functions are declared with a virtual keyword in base class.
▪ The resolving of function call is done at Run-time.
#include<iostream>
using namespace std;
class base
{
public:
virtual void print ()
{ cout<< "print base class" <<endl; }
void show ()
{ cout<< "show base class" <<endl; }
};
void show ()
{ cout<< "show derived class" <<endl; }
};
int main()
{
base *bptr;
derived d;
bptr = &d;
bptr->print();
bptr->show();
}
Pure Virtual Function:
A pure virtual function is implemented by classes which are derived from a Abstract class.
Following is a simple example to demonstrate the same.
#include<iostream>
using namespace std;
class Base
{
int x;
public:
virtual void fun() = 0;
int getX() { return x; }
};
int main(void)
{
Derived d;
d.fun();
return 0;
}
7. Attempt any one part of the following: 7x1=7
(a) Draw UML Timing Diagram for ATM machine.