Winter 2015 - Model Answer PaperDiploma
Winter 2015 - Model Answer PaperDiploma
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
(i) List two memory management operators available in C++ and state its use in one line
(1- Mark for new; 1- Mark for delete)
Ans: There are two types of memory management operators in C++:
New:- use for allocating memory;
Delete:- use for deleting/ free allocated memory space.
(ii) How address of (&) operator is used in pointers, explain with example.
(1- Mark for use of & operator; 1- Mark for example)
Ans : &:- & (The address of operator) is a unary operator that returns the memory
address of its operand. For eg. if var is an integer variable, then &var is its address.
It is used as: ptr=&a; // pointer ptr starts pointing to address of a;
Page 1 of 32
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
Page 2 of 32
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
…..
};
class derived_class_name:visibility-mode base-class-name
{
……. // members of derived class
……. // members of derived class
…….. // members of derived class
};
Page 3 of 32
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
Page 4 of 32
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
integer I1;
integer I2(10,20);
integer I3(I2);
cout<<"object I1";
I1.display();
cout<<"\nobject I2";
I2.display();
cout<<"\nobject I3";
I3.display();
getch();
}
(ii) State different types of inheritances and describe any one.
(List of different types of inheritance - 2 Marks; Description of any one - 2 Marks)
Ans :
Single inheritance: It includes single base class which can allow only one derived
class to inherit its properties.
Base Class
Derive Class
Page 5 of 32
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
2. Multi-level inheritance: A single class can be derived from a single base class. We
can derive a new class from as already derived class. It includes different levels for
defining class. A child class can share properties of its base class (parent class) as
well as grandparent class
Page 6 of 32
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
Page 7 of 32
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
The memory space for object is allocated when they are declared & not when the
class is specified. This statement is partly true. Actually, the member functions are
created &placed in memory space only once when they are defined as a part of a class
definition since all the objects belonging to that class use the same member functions,
no separate space is allocated for member functions when the objects are created
.only space for member variable is allocated separately for each object. Separate
memory locations for the objects are essential because the member variables will hold
different data values for different objects this is shown in fig
Page 8 of 32
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
Page 9 of 32
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
{
public:
void show( )
{
cout<<”\nshow derived”;
}
};
void main( )
{
Base B;
Derived D;
Base *bptr;
bptr=&B
bptr show( );
bptr=&D;
bptr show( );
}
Page 10 of 32
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
f) Write a program to declare class product having data members as product-id and
price. Accept and display data for one object using pointer to the object.
(Creating class - 2 Marks; creating object - 1 Mark; calling functions - 1 Mark)
Ans:
#include<iostream.h>
#include<conio.h>
class product
{
Page 11 of 32
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
private:
int product_id;
float prod_price;
public:
void getdata()
{
cout<<"enetr product id"<<'\n';
cin>>product_id;
cout<<"enter product price"<<'\n';
cin>>prod_price;
}
void display()
{
cout<<" product id is::"<<'\n'<<product_id<<'\n';
cout<<"product price is::"<<prod_price;
}
};
void main()
{
product p;
product *ptr;
clrscr();
ptr=&p;
ptr->getdata();
ptr->display();
getch();
}
Page 12 of 32
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
Ans:
cin = cin statement is use to accept input from user.
Syntax:-
cin>>variable_name
Example:
cin>>i;
cout = cout statement is use to display output processed by computer or to display simple
messages to user.
Syntax:-
cout<<variable_name
cout<<”Message”;
Example:
cout<<”Hello world”;
cout<<x;
Page 13 of 32
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
{
num = x;
cout<<"\nValues from Parameterized Constructor"<<num;
}
void display()
{
cout<<"\nValues of are"<<num;
}
};
void main()
{
clrscr();
demo d1,d2(30);
demo d3=d2; //Copy constructor
d3.display();
getch();
}
In above example class demo has three constructors. Default constructor, Parameterized
constructor and copy constructor. Object d1 invokes default constructor, object d2 invokes
parameterized constructor, whereas d3 invokes copy constructor.
Page 14 of 32
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
Page 15 of 32
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
for(i=0;i<5;i++)
{
cin>>b[i];
}
int_sum = add(a);
flo_sum = add(b);
cout<<"Total of Integer number is "<<int_sum<<endl;
cout<<"Total of Float number is "<<flo_sum;
getch();
}
int add(int x[10])
{
int i,res=0;
for(i=0;i<10;i++)
{
res=res+x[i];
}
return res;
}
float add(float j[5])
{
float res=0;
int i;
for(i=0;i<5;i++)
{
res=res+j[i];
}
return res;
}
Page 16 of 32
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
The concept of arrays is related to that of pointers. In fact, arrays work very much like
pointers to their first elements, and, actually, an array can always be implicitly converted
to the pointer of the proper type. For example, consider these two declarations:
int myarray [20];
int * mypointer;
The following assignment operation would be valid:
mypointer = myarray;
one can also use
mypointer = &myarray[0];
After that, mypointer and myarray would be equivalent and would have very
similar properties. The main difference being that mypointer can be assigned a different
address, whereas myarray can never be assigned anything, and will always represent the
same block of 20 elements of type int. Therefore, the following assignment would not be
valid:
myarray = mypointer;
Let's see an example that mixes arrays and pointers:
#include <iostream>
int main ()
{
int numbers[5];
int * p;
p = numbers;
*p = 10;
p++;
*p = 20;
p = &numbers[2];
*p = 30;
p = numbers + 3;
*p = 40;
p = numbers;
*(p+4) = 50;
for (int n=0; n<5; n++)
cout << numbers[n] << ", ";
return 0;
Page 17 of 32
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
}
Pointers and arrays support the same set of operations, with the same meaning for both.
The main difference being that pointers can be assigned new addresses, while arrays
cannot.
f) How function is defined outside of class, write general syntax and example of same
(Description - 1 Mark; syntax - 1 Mark ; example - 2 Marks)
Ans:
Outside class definition:
Member functions that are declared inside class have to be defined separately outside class.
Their definitions are as simple as normal function definition. They should have function
header & body.
Difference between member function & normal function is that member function
incorporates membership "identity label‟ in header
General syntax of member function definition is as:
class class_name
{
…
…
…
public:
return_type function_name(argument(s));
};
return-type class-name:: function-name(argument(s))
{
function body
}
Membership label class-name:: tell complier that function-name belongs to class
class-name. This is scope of function restricted to class-name specified in header
line. Scope resolution operator (::) is used.
Example:
class demo
{
public:
void print( )‟
};
Page 18 of 32
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
a) Write a program to implement single inheritance from following data. Accept and
display data for one object.
Data ;
Base class_name = Furniture
Data_mem = material , price
Derived class_name = Table
Data-mem = height, surface-area
(Creating base class - 1 Mark; creating Derive class - 1 Mark; creating main function - 2
Marks)
Ans:
#include<iostream.h>
#include<conio.h>
class Furniture
{
char material[10];
int price;
public:
void accept()
{
cout<<"\nEnter Material and Price for Furniture";
cin>>material>>price;
}
void display()
Page 19 of 32
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
{
cout<<"\nValues for Material and Price of Furniture\t"<<material<<"\t"<<price;
}
};
class Table : public Furniture
{
int height,surface_area;
public:
void accept1()
{
cout<<"\nEnter height and surface area for table";
cin>>height>>surface_area;
}
void display1()
{
cout<<"\nValues for height and surface area of Table\t"<<height<<"\t"<<surface_area;
}
};
void main()
{
Table t1;
clrscr();
t1.accept();
t1.accept1();
t1.display();
t1.display1();
getch();
}
b) State any four characteristics of constructor.
(Any four characteristics - 1 Mark for each characteristics)
Ans:
1. Constructors should be declared in the public section.
2. They are invoked automatically when the objects are created.
3. They do not have return type, not even void and therefore they cannot return values.
Page 20 of 32
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
Description:-
1. Include header files
In this section a programmer include all header files which are require to execute given
program. The most important file is iostream.h header file. This file defines most of the C++
statements like cout and cin. Without this file one cannot load C++ program.
2. Declare Class
In this section a programmer declares all classes which are necessary for given program. The
programmer uses general syntax of creating class.
3. Define Member Functions
This section allows programmer to design member functions of a class. The programmer can
have inside declaration of a function or outside declaration of a function.
4. Define Main Functions
This section the programmer creates objects and calls various functions writer within various
class.
Page 21 of 32
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
#include <iostream.h>
class Side
{
protected:
int l;
public:
void set_values (int x)
Page 22 of 32
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
{ l=x;}
};
class Square: public Side
{
public:
int sq()
{ return (l *l); }
};
class Cube: public Side
{
public:
int cub()
{ return (l *l*l); }
};
int main ()
{
Square s;
s.set_values (10);
cout << "The square value is::" << s.sq() << endl;
Cube c;
c.set_values (20);
cout << "The cube value is::" << c.cub() << endl;
return 0;
}
Page 23 of 32
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
clrscr();
cout<<"\nEnter 10 Numbers";
for(i=0;i<10;i++)
{
cin>>x[i];
}
xptr=&x[0];
while(*xptr!='\0')
{
cout<<*xptr<<" ";
xptr++;
}
getch();
}
Page 24 of 32
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
8. Binary operators overloaded through a member function, take one explicit argument and
those which are overloaded through a friend function take two explicit arguments.
9. When using binary operators overloaded through a member function, the left hand
operand must be an object of the relevant class.
10. Binary arithmetic operators such as +,-,* and / must explicitly return a value. They must
not attempt to change their own arguments.
data_type variable n;
};
c) Create class shape. Derive two class triangle and rectangle from class shape .Write
appropriate functions in both classes to accept dimensions and calculate area. Here
make area () function virtual which is common to all and will calculate area of both
rectangle and triangle. Display area of both.
(Declaring class shape, triangle, rectangle with proper functions and data members - 1
Mark each, main functions - 1 Mark)
Ans
Page 25 of 32
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
#include<iostream.h>
#include<conio.h>
class shape
{
public:
int l,b,h;
virtual void area()=0;
};
class triangle:public shape
{
public:
void getdata()
{
cout<<”\n enter dimensions of triangle:\n length:”;
cin>>l;
cout<<”\nheight:”;
cin>>h;
}
void area()
{
int a=0.5*l*h;
cout<<”\n area of triangle:”<<a;
}
};
class rectangle:public shape
{
public:
void getdata()
{
cout<<”\n enter dimensions of rectangle:\n length:”;
cin>>l;
cout<<”\n breadth:”;
cin>>b;
}
Page 26 of 32
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
void area()
{
int a=l*b;
cout<<”area of rectangle:”<<a;
}
};
void main()
{
clrscr();
shape *p;
triangle t;
rectangle r;
p=&t;
t.getdata();
p->area();
p=&r;
r.getdata();
p->area();
getch()
};
Page 27 of 32
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
Initilization Syntax:
Pointer_variable_name= &variable_name
f) What is difference between compile time polymorphism and run time polymorphism?
(Any four points each - 1 Mark)
Ans:
Compile time Polymorphism Runtime Polymorphism
It simply means that an object is bound to its It simply means that selection of appropriate
function call at compile time. function is done at run time
Functions to be called are know well before Function to be called is unknown until
appropriate selection is made.
This does not require use of pointers to objects This requires use of pointers to object
Function calls are faster Function calls execution are slower
Also called as early binding Also called as late binding
e.g. overloaded function call e.g. virtual function
It also referred as Static Binding It also referred as Dynamic Binding
Page 28 of 32
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
#include<string.h>
class staff
{
char name[20],post[5];
public:
void accept()
{
cout<<"\nEnter Name and post";
cin>>name>>post;
}
void display()
{
if(strcmp(post,"HOD")==0)
{
cout<<"\nHOD: "<<name;
}
}
};
void main()
{
staff s[5];
int i;
clrscr();
for(i=0;i<5;i++)
{
s[i].accept();
}
for(i=0;i<5;i++)
{
s[i].display();
}
getch();
}
Page 29 of 32
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
Page 30 of 32
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
private:
int n, m;
public:
gamma(int a, float b, int c, int d):: alpha(a), beta(b)
{
m = c;
n = d;
cout << "\n gamma initialized \n";
}
void show_mn()
{
cout << "\n m = "<<m;
cout << "\n n = "<<n;
}
);
void main()
{
gamma g(5, 7.65, 30, 100);
cout << "\n";
g.show_x();
g.show_y();
g.show_mn();
}
Page 31 of 32
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER-15 EXAMINATION
Model Answer Paper
while(*p!='\0')
{
p++;
}
while(*q!='\0')
{
*p=*q;
p++;
q++;
}
cout<<"\nConcatenated String is:-\t"<<str1;
getch();
}
Page 32 of 32